What will be the output of: int x = 5; System.out.println(++x + x++);?
A11
B12
C13
D10
Correct Answer:
B. 12
EXPLANATION
++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.
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); } }
A10 20
B20 10
C20 20
DCompilation Error
Correct Answer:
B. 20 10
EXPLANATION
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.
DObjects are garbage collected as soon as they go out of scope
Correct Answer:
B. Garbage collection is non-deterministic and may never run
EXPLANATION
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.
What is the result of: int x = 5; System.out.println(x++ + ++x);?
A10
B11
C12
D13
Correct Answer:
B. 11
EXPLANATION
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.