In Java, what is the relationship between an interface and a class in terms of implementation?
AA class can implement multiple interfaces but can extend only one class
BA class can extend multiple interfaces and implement one class
CA class can only implement one interface and extend one class
DAn interface and a class cannot be used together in Java
Correct Answer:
A. A class can implement multiple interfaces but can extend only one class
EXPLANATION
Java supports multiple interface implementation but single class inheritance. A class uses 'implements' keyword for interfaces and 'extends' for classes. This is a fundamental OOP concept.
Which of the following statements about Java's String class is TRUE?
AStrings are mutable objects in Java
BThe String class is marked as 'final' to prevent modification of its behavior
CCreating a new String using 'new' keyword always creates a new object in the string pool
DString concatenation using '+' operator is more efficient than StringBuilder for single operations
Correct Answer:
B. The String class is marked as 'final' to prevent modification of its behavior
EXPLANATION
The String class is declared as 'final' in Java, which prevents inheritance and ensures immutability. Strings are immutable, 'new' creates objects in heap not pool, and StringBuilder is generally better for concatenation.
Consider a real-world scenario where you need to implement a logging system. Which access modifier would you use for internal helper methods that should not be accessible outside the class?
Apublic
Bprotected
Cprivate
Ddefault (package-private)
Correct Answer:
C. private
EXPLANATION
'private' access modifier restricts the method to be accessible only within the same class, making it ideal for internal helper methods. This follows encapsulation principles.
Correct Answer:
B. public void display(int x) { } public void display(float x) { }
EXPLANATION
Method overloading requires methods with the same name but different parameter types or number of parameters. Option A has different return types (not sufficient), C has different access modifiers (not overloading), D has different case names (different methods).
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 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).
CConstructors are called automatically when an object is created
DA class can have multiple constructors
Correct Answer:
B. Constructors must return a value
EXPLANATION
Constructors do NOT have a return type, not even void. They automatically initialize objects when created. They can be overloaded and a class can have multiple constructors with different parameters.