Precise rules, easy marks
Output questions check caching, initialisation order, overload and override resolution, and Java's pass-by-value model. Explain the rule, not just the result.
- ValuesInteger cache, overflow and char arithmetic.
- ClassesStatic vs instance blocks, hiding vs overriding.
- CallsOverload resolution, finally and pass-by-value.
Written tests for Java roles are full of “what is the output?” questions. Each one checks a precise language rule — reference vs value comparison, initialisation order, overload resolution. Learn the rule and these become easy marks.
1. What is printed? (Integer caching)
true, then false. Autoboxing uses Integer.valueOf, which caches values from −128 to 127, so 127 is the same object but 128 creates two objects. Compare wrapper values with equals().
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b);
System.out.println(c == d);2. What is printed? (initialisation order)
static block, main, instance block, constructor. Static blocks run once when the class loads; instance initialiser blocks run before the constructor body each time an object is created.
class Demo {
static { System.out.println("static block"); }
{ System.out.println("instance block"); }
Demo() { System.out.println("constructor"); }
public static void main(String[] args) {
System.out.println("main");
new Demo();
}
}3. What is printed? (overload resolution with null)
String. With null, Java picks the most specific applicable overload; String is more specific than Object. If you added an overload for Integer too, the call would be ambiguous and fail to compile.
static void print(Object o) { System.out.println("Object"); }
static void print(String s) { System.out.println("String"); }
public static void main(String[] args) {
print(null);
}4. What is printed? (finally with return)
2. The return value is evaluated in try (1 is stored), but a return in finally replaces it. Returning from finally also swallows exceptions — avoid it.
static int test() {
try {
return 1;
} finally {
return 2;
}
}
System.out.println(test());5. What is printed? (char arithmetic and string concatenation)
195, then ab, then 97b. 'a' + 'b' adds the char codes (97 + 98 = 195). "" + 'a' + 'b' concatenates left to right, giving "ab". 'a' + 0 + "b" first adds numbers (97) then concatenates: "97b".
System.out.println('a' + 'b');
System.out.println("" + 'a' + 'b');
System.out.println('a' + 0 + "b");6. What is printed? (static method hiding)
Parent static and Child instance. Static methods are chosen by the reference type at compile time (hiding); instance methods are chosen by the actual object at runtime (overriding).
class Parent {
static void s() { System.out.println("Parent static"); }
void i() { System.out.println("Parent instance"); }
}
class Child extends Parent {
static void s() { System.out.println("Child static"); }
void i() { System.out.println("Child instance"); }
}
Parent p = new Child();
p.s();
p.i();7. What is printed? (pass by value with objects)
Ravi. Java passes a copy of the reference: the method can change the object's fields through it, but reassigning the parameter to a new object does not affect the caller's variable.
static void update(Person p) {
p.name = "Ravi"; // changes the shared object
p = new Person("Meera"); // only changes the local copy of the reference
}
Person person = new Person("Asha");
update(person);
System.out.println(person.name);8. What is printed? (integer overflow and division)
-2147483648, 2 and 2.5. Integer arithmetic silently wraps around on overflow; integer division truncates; making one operand a double gives a fractional result. Use Math.addExact to detect overflow.
System.out.println(Integer.MAX_VALUE + 1);
System.out.println(5 / 2);
System.out.println(5 / 2.0);Remember
Key points
- Cache: −128 to 127
Compare wrappers with equals().
- Static first, then instance
Blocks run before constructors.
- Statics are hidden
Chosen by the reference type.
- Java is pass-by-value
Even object references are copied.
Interview drill
Follow-up questions
What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.
-
1 What does System.out.println(0.1 + 0.2 == 0.3) print? Basic
false— floating-point rounding. Use BigDecimal for money and a tolerance for comparisons. -
2 What does "Java".substring(1, 3) return? Basic
"av"— start inclusive, end exclusive. -
3 What is printed by: int i = 5; i = i++ + ++i; System.out.println(i);? Intermediate
12:i++yields 5 (i becomes 6),++imakes i 7 and yields 7; 5 + 7 = 12 is assigned to i. -
4 What happens: List<Integer> list = List.of(1, 2); list.add(3);? Basic
UnsupportedOperationException—List.ofreturns an immutable list. -
5 What does Math.round(-2.5) return? Intermediate
-2— Math.round adds 0.5 and floors, so −2.5 becomes −2. -
6 Can main be overloaded? Basic
Yes, but the JVM only starts the program from
public static void main(String[] args).