Java interviews and campus tests lean heavily on object oriented behaviour rather than API recall. This set covers inheritance and polymorphism, interfaces and abstract classes, the collections framework, exception handling, multithreading, string handling, and JVM basics such as garbage collection. Output based questions are included because they expose the gaps that theory questions hide.
Consider the code: int a = 5; int b = a++; System.out.println(a + " " + b); What is the output?
Answer: B
The post-increment operator (a++) returns the value before incrementing. So b = 5, then a becomes 6. Output: 6 5
Q.2Hard
What will be printed? System.out.println(10 + 20 + "Java");
Answer: A
String concatenation works left to right. 10 + 20 = 30 (integer addition), then 30 + "Java" = "30Java" (string concatenation).
Q.3Hard
What is the scope of a local variable in Java?
Answer: B
Local variables have scope limited to the method or code block in which they are declared. They cannot be accessed outside that block.
Q.4Hard
Consider: int x = 0; System.out.println(x == 0 && x != 1); What is the output?
Answer: A
Both conditions are true: x == 0 (true) and x != 1 (true). The && (AND) operator returns true when both operands are true.
Q.5Hard
What is the result of: int x = 5; System.out.println(x++ + ++x);?
Answer: B
x++ returns 5 (then increments to 6), ++x increments to 7 and returns 7. Sum = 5+7 = 12. Note: Actual result may vary due to JVM optimization but typically 11 or 12.
Q.6Hard
Which statement about Java's garbage collection is true?
Answer: B
Garbage collection in Java is non-deterministic. System.gc() is just a request, not a guarantee. Objects are eligible for GC when unreachable, but actual collection timing varies.
Q.7Hard
What will be the output? class Test { public static void main(String[] args) { int x = 10; { int x = 20; System.out.println(x); } System.out.println(x); } }
Answer: B
A block scope creates a new variable x = 20 which shadows the outer x. First print outputs 20, then the block ends, outer x (10) is printed.
Q.8Hard
Which of the following will throw a NullPointerException? String s = null; System.out.println(s.length());
Answer: C
Calling a method on a null reference throws NullPointerException. s.length() tries to invoke method on null, causing the exception.
Q.9Hard
Consider: class Parent { Parent() { System.out.println("P"); } } class Child extends Parent { Child() { System.out.println("C"); } } public static void main(String[] args) { new Child(); }
Answer: C
Child constructor implicitly calls super() first, executing Parent constructor. Output: Parent prints 'P', then Child prints 'C'.
Q.10Hard
What is the output of: int x = 5; System.out.println(x++ + ++x);
Answer: B
x++ uses current value (5) then increments, ++x increments first then uses value (7). Order of evaluation: 5 + 7 = 12. However, x becomes 7 after ++x.
Q.11Hard
What will be the output of: int x = 5; System.out.println(++x + x++);?
Answer: B
++x increments x to 6 and returns 6 (pre-increment). Then x++ returns 6 and increments x to 7 (post-increment). So 6 + 6 = 12. The order of evaluation in expressions like this depends on operator precedence and associativity.
Q.12Hard
What is the result of: System.out.println('A' + 'B');?
Answer: B
Characters are promoted to their integer ASCII values in arithmetic operations. 'A' has ASCII value 65 and 'B' has ASCII value 66, so 65 + 66 = 131.
Q.13Hard
What will happen in this code: int x = Integer.MAX_VALUE; x++;?
Answer: B
In Java, integer overflow wraps around. When an int exceeds Integer.MAX_VALUE (2147483647), it overflows and wraps to Integer.MIN_VALUE (-2147483648).
Q.14Hard
In the context of Java 2024-25 exam pattern, which statement about the enhanced 'var' keyword (local variable type inference) is INCORRECT?
Answer: B
'var' cannot be used in lambda expression parameters or method parameters. It's restricted to local variables with explicit initialization. Options A and C are correct features of 'var'.
Q.15Hard
What will be the result of executing the following code? boolean result = (5 > 3) && (010 > 5); System.out.println(result);
Answer: C
Although the '&&' operator has short-circuit evaluation, Java evaluates both operands in this case. The first condition (5 > 3) is true, but the second (010) causes ArithmeticException due to division by zero.
Q.16Hard
What will be the output?
interface A { void show(); } class B implements A { public void show() { System.out.println("B"); } } class C extends B { public void show() { System.out.println("C"); } } A obj = new C(); obj.show();
Answer: C
Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.
Q.17Hard
What will be the output?
class Test { int x = 5; { x = 10; } Test() { x = 15; } public static void main(String[] args) { Test t = new Test(); System.out.println(t.x); } }
Answer: C
Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.
Q.18Hard
What is the output?
interface I1 { void method1(); } interface I2 extends I1 { void method2(); } class C implements I2 { public void method1() { System.out.println("M1"); } public void method2() { System.out.println("M2"); } } public class Test { public static void main(String[] args) { I1 obj = new C(); obj.method2(); } }
Answer: C
obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.
Q.19Hard
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
Answer: B
Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.
Q.20Hard
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
Answer: B
To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.