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.
Consider the following code: int x = 5; x += 3; System.out.println(x); What is the output?
A5
B8
C53
DCompilation error
Correct Answer:
B. 8
EXPLANATION
The += operator is an assignment operator that adds the right operand to the left operand and assigns the result. So x += 3 means x = x + 3, which is 5 + 3 = 8.
Which keyword is used to create a constant variable in Java that cannot be modified after initialization?
Astatic
Bconst
Cfinal
Dimmutable
Correct Answer:
C. final
EXPLANATION
The 'final' keyword is used to declare constants in Java. Once a final variable is assigned a value, it cannot be changed. 'const' is a reserved keyword but not used in Java.