1. Which of the following is NOT a principle of OOP?
o A) Encapsulation
o B) Inheritance
o C) Compilation
o D) Polymorphism
✅ Answer: C) Compilation
Explanation: Compilation is the process of converting source code into bytecode.
The four main OOP principles are Encapsulation, Inheritance, Abstraction, and
Polymorphism.
2. What is an object in Java?
o A) A blueprint for creating a class
o B) An instance of a class
o C) A type of variable
o D) A method in Java
✅ Answer: B) An instance of a class
Explanation: An object is a real-world entity that is created based on a class
definition.
3. Which access modifier allows a variable to be accessed only within the same
class?
o A) Public
o B) Private
o C) Protected
o D) Default
✅ Answer: B) Private
Explanation: The private access modifier restricts access to only within the same
class.
4. What is method overloading?
o A) Defining multiple methods with the same name but different parameters
o B) Defining multiple methods with the same name and same parameters
o C) Overriding a method in a subclass
o D) Writing multiple return statements in a method
✅ Answer: A) Defining multiple methods with the same name but different
parameters
Explanation: Method overloading occurs when multiple methods have the same
name but different parameter lists.
5. Which keyword is used to inherit a class in Java?
o A) extend
o B) inherits
o C) super
o D) extends
✅ Answer: D) extends
Explanation: The extends keyword is used for class inheritance in Java.
6. What is the default access modifier in Java if none is specified?
o A) Public
o B) Private
o C) Protected
o D) Default (package-private)
✅ Answer: D) Default (package-private)
Explanation: If no access modifier is specified, the default access level is package-
private, meaning it is accessible only within the same package.
7. Which of the following correctly defines an interface?
o A) class InterfaceName {}
o B) interface InterfaceName {}
o C) public class InterfaceName {}
o D) abstract class InterfaceName {}
✅ Answer: B) interface InterfaceName {}
Explanation: The interface keyword is used to define an interface in Java.
8. Which of the following best defines encapsulation?
o A) Hiding implementation details and exposing only necessary parts
o B) Using inheritance to reuse code
o C) Writing multiple methods with the same name
o D) Using interfaces instead of classes
✅ Answer: A) Hiding implementation details and exposing only necessary parts
Explanation: Encapsulation is achieved by using access modifiers (private,
protected, public) and getter/setter methods.
9. Which method is called automatically when an object is created?
o A) init()
o B) start()
o C) constructor
o D) main()
✅ Answer: C) constructor
Explanation: A constructor is automatically invoked when an object of a class is
created.
10. Can an abstract class have a constructor?
A) Yes
B) No
C) Only if it has abstract methods
D) Only if it is inherited
✅ Answer: A) Yes
Explanation: Abstract classes can have constructors to initialize fields in the superclass.
11. What is method overriding?
A) Writing two methods with the same name but different parameters
B) A subclass redefining a method from its parent class
C) Using an interface to implement methods
D) Writing a method inside a constructor
✅ Answer: B) A subclass redefining a method from its parent class
Explanation: Method overriding allows a subclass to provide a specific implementation of
a method already defined in its superclass.
12. What is the return type of a constructor?
A) void
B) class name
C) int
D) None of the above
✅ Answer: D) None of the above
Explanation: A constructor has no return type, not even void.
13. How do you prevent a class from being inherited?
A) Using private keyword
B) Using static keyword
C) Using final keyword
D) Using protected keyword
✅ Answer: C) Using final keyword
Explanation: Declaring a class final prevents it from being inherited.
14. Can an interface have private methods in Java 9 and later?
A) Yes
B) No
✅ Answer: A) Yes
Explanation: Since Java 9, interfaces can have private methods.
15. What will be the output of the following code?
java
CopyEdit
class Parent {
void show() { [Link]("Parent"); }
}
class Child extends Parent {
void show() { [Link]("Child"); }
}
public class Main {
public static void main(String args[]) {
Parent obj = new Child();
[Link]();
}
}
A) Parent
B) Child
C) Compilation error
D) Runtime error
✅ Answer: B) Child
Explanation: This is an example of runtime polymorphism. The overridden method in
Child is called.
16. Which of the following is true about abstract classes?
A) They can be instantiated.
B) They can have abstract and non-abstract methods.
C) They must contain at least one abstract method.
D) They cannot have constructors.
✅ Answer: B) They can have abstract and non-abstract methods.
Explanation: Abstract classes can contain both abstract (without implementation) and
concrete (implemented) methods.
17. Which of the following is NOT true about interfaces in Java?
A) They can contain static methods.
B) They can contain constructors.
C) They can have default methods.
D) They support multiple inheritance.
✅ Answer: B) They can contain constructors.
Explanation: Interfaces cannot have constructors because they are meant to be
implemented by classes, not instantiated directly.
18. What will be the output of the following code?
java
CopyEdit
interface A {
default void show() { [Link]("Interface A"); }
}
interface B {
default void show() { [Link]("Interface B"); }
}
class C implements A, B {
public void show() { [Link]("Class C"); }
}
public class Main {
public static void main(String args[]) {
C obj = new C();
[Link]();
}
}
A) Interface A
B) Interface B
C) Class C
D) Compilation Error
✅ Answer: D) Compilation Error
Explanation: Since both interfaces A and B have default methods with the same name, Java
will throw an error due to ambiguity.
19. Which of the following best describes polymorphism?
A) A subclass having multiple parent classes
B) The ability of a variable, function, or object to take multiple forms
C) The use of different access modifiers
D) The process of data binding
✅ Answer: B) The ability of a variable, function, or object to take multiple forms.
Explanation: Polymorphism allows method overloading and overriding, enabling the
same method name to perform different tasks.
20. What will be the output of the following code?
java
CopyEdit
class A {
static void display() { [Link]("Static method in A"); }
}
class B extends A {
static void display() { [Link]("Static method in B"); }
}
public class Main {
public static void main(String args[]) {
A obj = new B();
[Link]();
}
}
A) Static method in A
B) Static method in B
C) Compilation Error
D) Runtime Error
✅ Answer: A) Static method in A
Explanation: Static methods are not overridden; they are resolved at compile time based
on the reference type (A obj = new B(); is treated as type A).
21. Which keyword is used to access the superclass constructor?
A) super
B) base
C) parent
D) extends
✅ Answer: A) super
Explanation: The super keyword is used to call the superclass constructor.
22. What will be the output of the following code?
java
CopyEdit
class Parent {
Parent() { [Link]("Parent Constructor"); }
}
class Child extends Parent {
Child() { [Link]("Child Constructor"); }
}
public class Main {
public static void main(String args[]) {
Child obj = new Child();
}
}
A) Parent Constructor
B) Child Constructor
C) Parent Constructor followed by Child Constructor
D) Compilation Error
✅ Answer: C) Parent Constructor followed by Child Constructor
Explanation: When an object of Child is created, the Parent constructor is always called
first before the child constructor.
23. Which of the following is NOT a type of inheritance in Java?
A) Single
B) Multiple
C) Multilevel
D) Hierarchical
✅ Answer: B) Multiple
Explanation: Java does not support multiple inheritance with classes to avoid ambiguity.
Instead, multiple inheritance can be achieved using interfaces.
24. What will be the output of the following code?
java
CopyEdit
class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Main {
public static void main(String args[]) {
A obj = new B();
[Link](obj.x);
}
}
A) 10
B) 20
C) Compilation Error
D) Runtime Error
✅ Answer: A) 10
Explanation: Variable hiding occurs, meaning the reference type (A obj = new B();)
determines which variable is accessed, not the object type.
25. Which of the following statements about constructors is FALSE?
A) A constructor can be overloaded.
B) A constructor has no return type.
C) A constructor must be explicitly defined.
D) A constructor is called automatically when an object is created.
✅ Answer: C) A constructor must be explicitly defined.
Explanation: If no constructor is defined, Java provides a default constructor
automatically.
26. Which of the following correctly describes a singleton class?
A) A class that allows only one object to be created.
B) A class that cannot be inherited.
C) A class that has only static methods.
D) A class that must be abstract.
✅ Answer: A) A class that allows only one object to be created.
Explanation: A singleton class ensures only one instance exists at a time.
27. How do you declare a singleton class in Java?
A) By making the class final
B) By declaring a private static instance and providing a public static method
C) By using the synchronized keyword
D) By declaring the class abstract
✅ Answer: B) By declaring a private static instance and providing a public static method.
Explanation: A singleton class contains a private static instance and a public static
method to access it.
28. Which method should be overridden in Java to compare objects?
A) toString()
B) equals()
C) hashCode()
D) compare()
✅ Answer: B) equals()
Explanation: The equals() method is used to compare object contents instead of
references.
29. Which of the following statements about final methods is true?
A) They can be overridden.
B) They cannot be overridden.
C) They can only be used in abstract classes.
D) They must be static.
✅ Answer: B) They cannot be overridden.
Explanation: A final method cannot be overridden in subclasses.
30. What will be the output of the following code?
java
CopyEdit
class A {
void display() { [Link]("A"); }
}
class B extends A {
void display() { [Link]("B"); }
}
public class Main {
public static void main(String args[]) {
A obj = new B();
[Link]();
}
}
A) A
B) B
C) Compilation Error
D) Runtime Error
✅ Answer: B) B
Explanation: Method overriding is applied, so the method in B is executed.