Which of the following is a valid declaration of a two-dimensional array in Java that can store 3 rows and 4 columns of integers?
Aint arr[][] = new int[3][4];
Bint[] arr[] = new int[3][4];
Cint[][] arr = new int[3][4];
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
All three declarations are syntactically valid in Java and represent the same 2D array structure. The position of brackets doesn't matter for 2D array declaration - they all create a 3x4 integer array.
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 of the following is true about the 'break' statement?
AIt terminates the entire program
BIt skips the current iteration of a loop
CIt exits the current loop or switch statement
DIt jumps to the next iteration
Correct Answer:
C. It exits the current loop or switch statement
EXPLANATION
'break' statement exits the innermost loop or switch statement. It doesn't skip the current iteration (that's 'continue') and doesn't terminate the entire program.
What is the output of: System.out.println(10 / 3);?
A3.333...
B3.33
C3
D4
Correct Answer:
C. 3
EXPLANATION
When dividing two integers, Java performs integer division and returns an integer result. 10 / 3 = 3 (remainder is discarded). To get decimal result, at least one operand should be a float or double.
In Java, the 'long' data type is always 64 bits, regardless of the platform. This is one of Java's strengths - its size specifications are platform-independent.