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.