Java fundamentals interviewers expect
Basics questions are short but they reveal how well you understand the platform. Clear, confident answers here buy you goodwill for the harder questions later.
- PlatformJDK, JRE, JVM and bytecode.
- TypesPrimitives, wrappers, autoboxing and casting.
- Class basicsstatic, this, super, constructors and access modifiers.
These are the first questions in most Java interviews, usually asked in the first ten minutes to check your foundation. Answer them confidently and briefly — they set the tone for the rest of the round.
1. What is the difference between JDK, JRE and JVM?
The JVM (Java Virtual Machine) runs Java bytecode and makes Java platform-independent. The JRE (Java Runtime Environment) is the JVM plus the standard libraries needed to run programs. The JDK (Java Development Kit) is the JRE plus development tools such as the compiler javac and debugger. Developers install the JDK; earlier, users only needed a JRE.
2. Why is Java platform-independent?
Java source code is compiled into bytecode (.class files), not machine code. Each operating system has its own JVM that understands the same bytecode, so the same compiled program runs on Windows, Linux or macOS — “write once, run anywhere”. The JVM itself is platform-specific.
3. What are the primitive data types in Java?
Java has eight primitives: byte (1 byte), short (2), int (4), long (8), float (4), double (8), char (2, Unicode) and boolean. Everything else is an object. Primitives store values directly and are faster; objects are accessed through references.
int age = 21;
long population = 1_400_000_000L;
double price = 99.50;
char grade = 'A';
boolean placed = true;4. What are wrapper classes and what is autoboxing?
Wrapper classes (Integer, Double, Boolean…) wrap primitives in objects so they can be used in collections and generics. Autoboxing converts a primitive to its wrapper automatically; unboxing does the reverse.
List<Integer> marks = new ArrayList<>();
marks.add(85); // autoboxing int -> Integer
int first = marks.get(0); // unboxing Integer -> int5. What does the static keyword mean?
A static member belongs to the class rather than to each object. Static variables are shared by all objects; static methods can be called without creating an object and cannot use this or instance variables directly. A static block runs once when the class is loaded.
class Counter {
static int count = 0; // shared by all objects
Counter() { count++; }
}
new Counter(); new Counter();
System.out.println(Counter.count); // 26. What is the difference between this and super?
this refers to the current object — used to distinguish fields from parameters or to call another constructor of the same class. super refers to the parent class — used to call the parent constructor or a parent method that has been overridden.
class Employee {
String name;
Employee(String name) { this.name = name; }
}
class Manager extends Employee {
Manager(String name) { super(name); }
}7. What is a constructor? Can it be overloaded?
A constructor initialises a new object. It has the same name as the class and no return type. If you write none, Java adds a default no-argument constructor. Constructors can be overloaded with different parameter lists, and one constructor can call another using this(...).
8. Explain the access modifiers in Java.
private: only inside the class. Default (no keyword): inside the same package. protected: same package and subclasses in other packages. public: everywhere. A good habit is to keep fields private and expose only what is needed.
9. What is the difference between break and continue?
break exits the loop completely. continue skips the rest of the current iteration and moves to the next one.
for (int i = 1; i <= 5; i++) {
if (i == 2) continue; // skip 2
if (i == 4) break; // stop at 4
System.out.print(i + " ");
}
// Output: 1 3Remember
Key points
- Bytecode
javac compiles to bytecode; the JVM runs it on any platform.
- Eight primitives
byte, short, int, long, float, double, char, boolean.
- static = class level
Shared by all objects and usable without one.
Interview drill
Follow-up questions
What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.
-
1 Is Java purely object-oriented? Basic
No. Java has primitive types that are not objects, and static members can be used without objects. It is strongly object-oriented, but not “pure” like Smalltalk.
Key insight: Primitives and static members are the two reasons.
-
2 What is the default value of a local variable? Basic
Local variables have no default value; the compiler forces you to initialise them before use. Instance and static fields get defaults: 0, 0.0, false, \u0000 or null.
void test() { int x; // System.out.println(x); // compile error: might not be initialized }Key insight: Fields get defaults; locals do not.
-
3 What is the output of Integer a = 127, b = 127; a == b? And for 128? Intermediate
truefor 127 andfalsefor 128. Java cachesIntegerobjects from -128 to 127, so autoboxing reuses the same object in that range. Outside it, new objects are created and==compares references. Always useequals()for wrapper objects.Integer a = 127, b = 127; System.out.println(a == b); // true Integer c = 128, d = 128; System.out.println(c == d); // falseKey insight: Compare wrapper objects with equals(), never ==.
-
4 Can we have multiple main methods in a class? Intermediate
Yes, by overloading
mainwith different parameters. But the JVM only starts the program frompublic static void main(String[] args).Key insight: Overloading is allowed; the entry point signature is fixed.
-
5 What is type casting? What is the risk of narrowing? Basic
Widening (e.g.
inttolong) is automatic and safe. Narrowing (e.g.doubletoint) needs an explicit cast and can lose data — decimals are truncated and large values can overflow.double d = 9.99; int i = (int) d; // 9 int big = 130; byte b = (byte) big; // -126 (overflow)Key insight: Narrowing casts can silently lose information.