Consider a scenario where you need to create a variable that can hold references to objects of any type. Which of the following approaches would be most appropriate in Java 2024 standards?
AUse raw type: List myList = new ArrayList();
BUse generics: List myList = new ArrayList();
CUse Object type: Object obj = new Object();
DUse var keyword: var myList = new ArrayList();
Correct Answer:
B. Use generics: List myList = new ArrayList();
EXPLANATION
Using generics with wildcards (List<?>) is the safest and most type-safe approach to handle objects of any type in modern Java. The 'var' keyword is also acceptable but wildcards provide better type checking.
What will be the output of the following code?
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
Atrue
Bfalse
Cnull
DCompilation error
Correct Answer:
B. false
EXPLANATION
The '==' operator compares object references, not content. str1 refers to the string pool while str2 is a new object in heap memory, hence they have different references.
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.