Which of the following is true about method overloading?
AMethods must have different return types
BMethods must have same name but different parameters
CMethods must be in different classes
DOnly constructors can be overloaded
Correct Answer:
B. Methods must have same name but different parameters
EXPLANATION
Method overloading requires methods to have the same name but different parameter lists (number, type, or order of parameters). Return type alone is insufficient.
Which of the following best describes encapsulation?
AHiding implementation details and providing controlled access through methods
BCreating objects from classes
CInheriting properties from parent class
DUsing multiple methods with the same name
Correct Answer:
A. Hiding implementation details and providing controlled access through methods
EXPLANATION
Encapsulation involves bundling data (variables) and methods, hiding implementation details, and providing public methods for access. It protects data integrity.
Which of the following statements about abstract classes is TRUE?
AAbstract classes can be instantiated
BAbstract classes cannot have concrete methods
CAbstract classes can have abstract methods and concrete methods
DAbstract classes must extend another abstract class
Correct Answer:
C. Abstract classes can have abstract methods and concrete methods
EXPLANATION
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.
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);
}
}
A10
B20
CCompilation Error
DRuntime Error
Correct Answer:
A. 10
EXPLANATION
Variable overriding doesn't work in Java like method overriding. obj.x accesses the variable from reference type A, which has value 10.
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).