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.
Q.42Medium
Which of the following correctly represents variable declaration and initialization in Java?
Answer: C
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).
Q.43Medium
Which of the following will compile and run without errors?
Answer: B
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.
Q.44Medium
Which method is called automatically when an object is garbage collected?
Answer: A
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.
Q.45Medium
What will be the output of the following code?
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
Answer: B
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.
Advertisement
Q.46Medium
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?
Answer: B
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.
Q.47Medium
Which of the following correctly demonstrates method overloading in Java?
Answer: B
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).
Q.48Medium
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?
Answer: C
'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.
Q.49Medium
Which of the following statements about Java's String class is TRUE?
Answer: B
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.
Q.50Medium
In Java, what is the relationship between an interface and a class in terms of implementation?
Answer: A
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.
Q.51Medium
What is the output of the following code?
class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Test {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}
Answer: A
Variable overriding doesn't work in Java like method overriding. obj.x accesses the variable from reference type A, which has value 10.
Q.52Medium
Which of the following statements about abstract classes is TRUE?
Answer: C
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.
Q.53Medium
What will happen when you try to instantiate an interface in Java?
Answer: C
Interfaces cannot be instantiated directly. You must create a class that implements the interface. Attempting this causes a compilation error.
Q.54Medium
Which of the following best describes encapsulation?
Answer: A
Encapsulation involves bundling data (variables) and methods, hiding implementation details, and providing public methods for access. It protects data integrity.
Q.55Medium
Which of the following is true about method overloading?
Answer: B
Method overloading requires methods to have the same name but different parameter lists (number, type, or order of parameters). Return type alone is insufficient.
Q.56Medium
What is the correct way to prevent a class from being inherited in Java?
Answer: B
The 'final' keyword prevents a class from being extended. String, Integer, and other wrapper classes are marked final for security and immutability.
Q.57Medium
What is the output?
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super();
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
new B();
}
}
Answer: B
super() calls parent constructor first. Parent constructor A prints 'A', then child constructor B prints 'B'.
Q.58Medium
Which of the following statements about 'instanceof' operator is correct?
Answer: B
The 'instanceof' operator checks whether an object is an instance of a specific class or implements a specific interface.
Q.59Medium
Which of the following statements about inheritance in Java is correct?
Answer: D
Java supports single inheritance for classes but a class can implement multiple interfaces. An interface can extend multiple interfaces through multiple inheritance.
Q.60Medium
Which of the following correctly describes method overriding in Java?
Answer: B
Method overriding occurs when a child class provides a specific implementation of a method already defined in the parent class with the same signature. The @Override annotation is optional but recommended.