Which of the following statements about Java's garbage collection is TRUE?
AGarbage collection is guaranteed to run at specific time intervals
BThe programmer must explicitly call System.gc() to free memory
CGarbage collection automatically reclaims memory used by unreferenced objects
DJava does not have automatic garbage collection mechanism
Correct Answer:
C. Garbage collection automatically reclaims memory used by unreferenced objects
EXPLANATION
Java's garbage collector automatically identifies and reclaims memory of objects that are no longer referenced. While System.gc() can be suggested, it's not guaranteed to execute immediately.
Consider the following code snippet. What is the primary purpose of the 'final' keyword when applied to a class in Java?
APrevents the class from being instantiated
BPrevents the class from being inherited by other classes
CMakes all methods in the class abstract
DAllows the class to be accessed globally without import
Correct Answer:
B. Prevents the class from being inherited by other classes
EXPLANATION
The 'final' keyword when applied to a class prevents it from being extended or inherited. This is a fundamental OOP concept in Java used to restrict inheritance.
Which method is called automatically when an object is garbage collected?
Afinalize()
Bdestroy()
Cdelete()
Dcleanup()
Correct Answer:
A. finalize()
EXPLANATION
The finalize() method is called by the garbage collector before an object is destroyed. It can be used to perform cleanup operations, though it's generally not recommended in modern Java.
Which of the following will compile and run without errors?
Apublic static void main() { }
Bpublic static void main(String[] args) { }
Cpublic void main(String[] args) { }
Dstatic void main(String[] args) { }
Correct Answer:
B. public static void main(String[] args) { }
EXPLANATION
The correct signature for the main method is 'public static void main(String[] args)'. It must be public, static, void, named 'main', and accept a String array parameter. Any deviation will not be recognized as the entry point.
Which of the following correctly represents variable declaration and initialization in Java?
Aint x; x = 5.5;
Bdouble y = 10;
Cboolean flag = true;
DString name = 123;
Correct Answer:
C. boolean flag = true;
EXPLANATION
Option C is correct. Option A causes a compilation error (cannot assign double to int). Option B works but there's implicit conversion. Option D causes a compilation error (cannot assign int to String).
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.