Java Inheritance and Polymorphism MCQs

1 min read

Java inheritance and polymorphism form the bedrock of Object-Oriented Programming (OOP), enabling code reuse and flexible runtime behavior. Inheritance allows a subclass to acquire properties and methods of a superclass using the extends keyword, while polymorphism empowers objects to be treated as instances of their parent classes. Through features like method overriding and dynamic method dispatch, Java resolves method calls at runtime rather than compile-time. Mastering these core concepts is vital for designing scalable software architectures and clearing technical certification exams.

1. Which keyword is used by a class to inherit another class in Java?

a) implements
b) inherits
c) extends
d) super
Correct Answer: c) extends
Explanation:
The extends keyword is used in Java to indicate that a class is derived from another class.

2. What is the root class of all classes in Java?

a) java.lang.Class
b) java.lang.Object
c) java.lang.System
d) java.lang.String
Correct Answer: b) java.lang.Object
Explanation:
java.lang.Object is the topmost class in the Java class hierarchy. Every class directly or indirectly inherits from it.

3. Which of the following is NOT a type of inheritance in Java using classes?

a) Single inheritance
b) Multilevel inheritance
c) Hierarchical inheritance
d) Multiple inheritance
Correct Answer: d) Multiple inheritance
Explanation:
Java does not support multiple inheritance of classes to avoid the diamond problem. It is achieved through interfaces.

4. What is the primary benefit of using inheritance in Java?

a) Data Encapsulation
b) Code Reusability
c) Memory Optimization
d) Security
Correct Answer: b) Code Reusability
Explanation:
Inheritance allows a subclass to reuse the properties and methods of its superclass, reducing code duplication.

5. Which keyword is used to refer to the immediate parent class instance variable or method?

a) this
b) parent
c) super
d) base
Correct Answer: c) super
Explanation:
The super keyword is a reference variable used to refer to the immediate parent class object.

6. Are constructors inherited by a subclass in Java?

a) Yes, always
b) No, constructors are not inherited
c) Only default constructors are inherited
d) Only parameterized constructors are inherited
Correct Answer: b) No, constructors are not inherited
Explanation:
Constructors are not inherited by subclasses. However, the superclass constructor is invoked using super().

7. What happens if a class is declared as final?

a) It cannot be instantiated
b) It cannot be subclassed
c) All its methods become abstract
d) All its variables become static
Correct Answer: b) It cannot be subclassed
Explanation:
A final class cannot be extended by any other class. It prevents inheritance.

8. What is method overriding?

a) Creating multiple methods with the same name but different parameters
b) Defining a method in a subclass with the same signature as a method in its superclass
c) Defining multiple methods with the same name in different classes without inheritance
d) Hiding a parent class method using a static keyword
Correct Answer: b) Defining a method in a subclass with the same signature as a method in its superclass
Explanation:
Method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its parent class.

9. Which concept allows Java to implement run-time polymorphism?

a) Method overloading
b) Method overriding
c) Variable hiding
d) Constructor overloading
Correct Answer: b) Method overriding
Explanation:
Run-time polymorphism (Dynamic Method Dispatch) is achieved in Java through method overriding.

10. Which of the following annotations indicates that a method is intended to override a superclass method?

a) @Override
b) @Overload
c) @Super
d) @Inherit
Correct Answer: a) @Override
Explanation:
The @Override annotation tells the compiler that the method is meant to override a method declared in a superclass.

11. Compile-time polymorphism is achieved by which of the following?

a) Method overriding
b) Method overloading
c) Interfaces
d) Abstract classes
Correct Answer: b) Method overloading
Explanation:
Method overloading is a form of compile-time (static) polymorphism because the compiler decides which method to bind based on the arguments.

12. Can we override a static method in Java?

a) Yes, just like instance methods
b) No, it causes a compilation error
c) No, declaring a static method with the same signature in a subclass hides the superclass method
d) Yes, but only if the subclass method is also static
Correct Answer: c) No, declaring a static method with the same signature in a subclass hides the superclass method
Explanation:
Static methods are bound to the class, not the object. Declaring a static method with the same signature in a subclass is known as Method Hiding, not overriding.

13. What is the restriction on the access modifier of an overriding method?

a) It must be the exact same as the overridden method
b) It must not be more restrictive than the overridden method
c) It must not be less restrictive than the overridden method
d) There are no restrictions
Correct Answer: b) It must not be more restrictive than the overridden method
Explanation:
An overriding method can increase the visibility (e.g., protected to public) but cannot decrease it (e.g., public to protected).

14. Which of the following methods CANNOT be overridden?

a) public methods
b) protected methods
c) final methods
d) default methods
Correct Answer: c) final methods
Explanation:
A method declared with the final keyword cannot be overridden by subclasses.

15. Can a private method be overridden in Java?

a) Yes
b) No
c) Only within the same package
d) Only if the subclass is public
Correct Answer: b) No
Explanation:
Private methods are not visible to subclasses and therefore cannot be overridden.

16. What is a covariant return type?

a) Returning a completely unrelated type in an overriding method
b) Returning a primitive type instead of an object type
c) Allowing an overriding method to return a subclass of the return type declared in the overridden method
d) Changing the return type to void
Correct Answer: c) Allowing an overriding method to return a subclass of the return type declared in the overridden method
Explanation:
Since Java 5, it is possible to override a method and change its return type to a subclass of the original return type.

17. If a subclass explicitly calls super(), where must it be placed in the subclass constructor?

a) At the very end of the constructor
b) It can be placed anywhere
c) As the very first statement in the constructor
d) Outside the constructor block
Correct Answer: c) As the very first statement in the constructor
Explanation:
The call to super() must always be the first statement executed inside a subclass constructor.

18. What happens if you do not explicitly call super() in a subclass constructor?

a) A compilation error occurs
b) A runtime exception is thrown
c) The compiler automatically inserts a call to the no-argument super() constructor
d) The parent class is not instantiated
Correct Answer: c) The compiler automatically inserts a call to the no-argument super() constructor
Explanation:
If no constructor is called explicitly using this() or super(), the compiler implicitly inserts a call to the default super() constructor.

19. Can this() and super() be used together in the same constructor?

a) Yes, if super() is first
b) Yes, if this() is first
c) No, they cannot both be the first statement
d) Yes, they can be placed in any order
Correct Answer: c) No, they cannot both be the first statement
Explanation:
Both this() and super() must be the first statement in a constructor, so they cannot be used simultaneously in the same constructor.

20. Which of these operators is used to test if an object is an instance of a specific class or interface?

a) instanceof
b) typeof
c) isTypeOf
d) typecast
Correct Answer: a) instanceof
Explanation:
The instanceof operator is used to test whether an object is an instance of a specified type.

21. What is Upcasting in Java?

a) Casting a subclass reference to a superclass reference
b) Casting a superclass reference to a subclass reference
c) Converting primitive types to wrapper classes
d) Converting wrapper classes to primitive types
Correct Answer: a) Casting a subclass reference to a superclass reference
Explanation:
Upcasting is casting a reference of a subclass to a superclass type. It happens implicitly.

22. What is Downcasting in Java?

a) Casting a subclass reference to a superclass reference
b) Casting a superclass reference to a subclass reference
c) Converting a string to an integer
d) Converting a double to an int
Correct Answer: b) Casting a superclass reference to a subclass reference
Explanation:
Downcasting is manually casting a superclass reference back to a subclass reference. It must be done explicitly.

23. What exception is thrown if Downcasting fails at runtime?

a) ClassCastException
b) NullPointerException
c) IllegalArgumentException
d) TypeMismatchException
Correct Answer: a) ClassCastException
Explanation:
If a superclass reference does not actually point to the specified subclass object, downcasting throws a ClassCastException at runtime.

24. In Java, can a class extend an interface?

a) Yes, using the extends keyword
b) Yes, using the implements keyword
c) No, a class implements interfaces, it does not extend them
d) No, interfaces can only extend other classes
Correct Answer: c) No, a class implements interfaces, it does not extend them
Explanation:
A class implements an interface. An interface extends another interface.

25. Can an interface extend multiple interfaces?

a) Yes
b) No
c) Only up to two interfaces
d) Only if they contain no default methods
Correct Answer: a) Yes
Explanation:
While classes cannot extend multiple classes, an interface is permitted to extend multiple other interfaces.

26. Method overloading relies on which of the following?

a) Method return type
b) Method name and parameters
c) Access modifier
d) Exception list
Correct Answer: b) Method name and parameters
Explanation:
Overloading occurs when methods share the same name but have different parameter lists.

27. Can two methods be overloaded if they differ only in their return type?

a) Yes
b) No
c) Yes, if one of them is static
d) Yes, if the return types are covariant
Correct Answer: b) No
Explanation:
Methods cannot be overloaded based solely on their return type. The parameter list must be different to prevent compilation errors.

28. Which component determines which overridden method gets called during run-time?

a) The type of the reference variable
b) The JVM memory allocation size
c) The actual object type being referred to
d) The compiler
Correct Answer: c) The actual object type being referred to
Explanation:
In dynamic method dispatch, the overriding method is chosen based on the actual object created at run-time, not the reference type.

29. What is meant by the phrase has-a relationship in Java?

a) Inheritance
b) Polymorphism
c) Aggregation/Composition
d) Encapsulation
Correct Answer: c) Aggregation/Composition
Explanation:
The has-a relationship refers to aggregation or composition, where one class uses an instance of another class as a member field.

30. What is meant by the phrase is-a relationship in Java?

a) Composition
b) Aggregation
c) Inheritance
d) Encapsulation
Correct Answer: c) Inheritance
Explanation:
The is-a relationship denotes inheritance.

31. Which feature prevents a variable from being modified after initialization?

a) static
b) final
c) abstract
d) volatile
Correct Answer: b) final
Explanation:
The final keyword applied to a variable makes it a constant, preventing further modification.

32. Can abstract classes have constructors?

a) Yes
b) No
c) Only default constructors
d) Only parameterless constructors
Correct Answer: a) Yes
Explanation:
Abstract classes can have constructors. They are called when a concrete subclass is instantiated.

33. Can you instantiate an abstract class?

a) Yes, using the new keyword
b) No, abstract classes cannot be instantiated
c) Yes, if it has no abstract methods
d) Yes, via a static method
Correct Answer: b) No, abstract classes cannot be instantiated
Explanation:
Abstract classes are meant to be subclassed; therefore, direct instantiation using new is prohibited.

34. If a class contains an abstract method, the class must be declared as:

a) final
b) static
c) abstract
d) public
Correct Answer: c) abstract
Explanation:
If any method in a class is declared abstract, the entire class must also be declared abstract.

35. Can a final class have abstract methods?

a) Yes
b) No
c) Only if the abstract methods are also final
d) Only if the abstract methods are static
Correct Answer: b) No
Explanation:
A final class cannot be extended, and an abstract method requires a subclass to provide implementation. Thus, they are contradictory.

36. When a subclass variable shadows a superclass variable, how can the superclass variable be accessed?

a) Using the this keyword
b) Using the super keyword
c) It cannot be accessed
d) Using a static reference
Correct Answer: b) Using the super keyword
Explanation:
The super keyword can be used to reference shadowed instance variables from the superclass.

37. Are private fields of a superclass inherited by the subclass?

a) Yes, they are inherited directly
b) No, they are not inherited
c) Yes, but they require the super keyword
d) Yes, they become protected in the subclass
Correct Answer: b) No, they are not inherited
Explanation:
Private fields and methods are restricted to the class they are declared in and are not inherited by subclasses.

38. Can we overload the main() method in Java?

a) No, it must always take String[] args
b) Yes, but the JVM will only call the standard main(String[] args)
c) Yes, and the JVM can call any main method
d) No, it causes a compilation error
Correct Answer: b) Yes, but the JVM will only call the standard main(String[] args)
Explanation:
The main method can be overloaded just like any other method, but the JVM strictly looks for public static void main(String[] args) to start execution.

39. Which keyword is used to call a constructor of the same class from another constructor?

a) super()
b) this()
c) class()
d) self()
Correct Answer: b) this()
Explanation:
The this() keyword is used to invoke one constructor from another within the same class.

40. Polymorphism translates literally to:

a) Many names
b) One form
c) Many forms
d) Single behavior
Correct Answer: c) Many forms
Explanation:
The word polymorphism comes from the Greek words poly (many) and morphs (forms).

41. Which of the following is true regarding default methods in Java interfaces?

a) They cannot be overridden
b) They are implicitly abstract
c) They provide a default implementation that implementing classes can use or override
d) They must be static
Correct Answer: c) They provide a default implementation that implementing classes can use or override
Explanation:
Since Java 8, interfaces can have default methods with bodies, which can be used as-is or overridden by the implementing classes.

42. How do you resolve a conflict when a class implements two interfaces containing default methods with the same signature?

a) The compiler chooses the first one declared
b) The compiler throws a compilation error unless the class overrides the method
c) The class automatically inherits both
d) The JVM determines it at runtime
Correct Answer: b) The compiler throws a compilation error unless the class overrides the method
Explanation:
To resolve the ambiguity, the implementing class must override the conflicting method and optionally call a specific interface's default method.

43. Is variable overriding supported in Java?

a) Yes, just like methods
b) No, variables can only be hidden, not overridden
c) Yes, but only for static variables
d) Yes, but only for final variables
Correct Answer: b) No, variables can only be hidden, not overridden
Explanation:
Java supports method overriding, but data members (variables) are not polymorphic. Declaring a variable with the same name in a subclass simply hides the superclass variable.

44. What is the output of upcasting when accessing a hidden variable?

a) The subclass variable is accessed
b) The superclass variable is accessed
c) A runtime exception is thrown
d) A compilation error occurs
Correct Answer: b) The superclass variable is accessed
Explanation:
Since variables do not participate in polymorphism, an upcasted reference will always access the variable defined in the reference type's class.

45. What is constructor chaining?

a) Calling multiple methods inside a constructor
b) Creating multiple objects in a chain
c) The process of calling one constructor from another constructor within the same class or superclass
d) Preventing a constructor from being called
Correct Answer: c) The process of calling one constructor from another constructor within the same class or superclass
Explanation:
Constructor chaining is achieved using this() and super() to reuse initialization logic.

46. Can an interface be marked as final?

a) Yes
b) No
c) Only if it has no abstract methods
d) Only if all its methods are default
Correct Answer: b) No
Explanation:
Interfaces are meant to be implemented by classes. Marking an interface final would prevent implementation, resulting in a compilation error.

47. In a multilevel inheritance structure (A -> B -> C), what happens when a new C object is created?

a) Only constructor C is called
b) Constructors are called in the order C, B, A
c) Constructors are called in the order A, B, C
d) No constructors are called
Correct Answer: c) Constructors are called in the order A, B, C
Explanation:
In Java, constructors are executed from the top of the inheritance hierarchy down to the bottom (Parent -> Child).

48. Can an abstract class implement an interface?

a) Yes, and it must provide implementations for all interface methods
b) Yes, and it can choose to leave some or all methods abstract
c) No, abstract classes cannot implement interfaces
d) No, interfaces can only be implemented by concrete classes
Correct Answer: b) Yes, and it can choose to leave some or all methods abstract
Explanation:
An abstract class can implement an interface without being forced to provide method implementations. The concrete subclasses will bear that responsibility.

49. If class A extends class B, and class B extends class C, which class is at the top of this hierarchy (ignoring Object)?

a) Class A
b) Class B
c) Class C
d) They are all equal
Correct Answer: c) Class C
Explanation:
C is the superclass of B, and B is the superclass of A. Thus, C is at the top of this specific hierarchy.

50. Which of the following is true about protected members?

a) They are accessible anywhere in the program
b) They are accessible only within the same class
c) They are accessible in the same package and by subclasses in other packages
d) They are exactly the same as private members
Correct Answer: c) They are accessible in the same package and by subclasses in other packages
Explanation:
The protected access modifier allows visibility within the same package and to subclasses regardless of the package.

51. If a method throws a checked exception in the superclass, what are the rules for the overriding method?

a) It must throw the exact same exception or a subclass of it, or not throw any checked exception
b) It can throw any exception
c) It must throw a superclass exception
d) It cannot throw any exceptions at all
Correct Answer: a) It must throw the exact same exception or a subclass of it, or not throw any checked exception
Explanation:
An overriding method cannot throw new or broader checked exceptions compared to the overridden method.

52. If a method does NOT throw any checked exception in the superclass, can the overriding method throw one?

a) Yes, any exception
b) No, it cannot throw any checked exception
c) Yes, but only Exception
d) Yes, but only Throwable
Correct Answer: b) No, it cannot throw any checked exception
Explanation:
If the parent method throws no checked exceptions, the overriding method is strictly forbidden from throwing new checked exceptions, though unchecked exceptions are allowed.

53. What happens if a subclass attempts to declare a static method with the same signature as a final instance method in the superclass?

a) It hides the final method
b) It overrides the final method
c) It results in a compile-time error
d) It compiles successfully but throws a runtime exception
Correct Answer: c) It results in a compile-time error
Explanation:
A final method cannot be overridden or hidden. Any attempt to declare a method with the same signature in the subclass will fail to compile.

54. What does a blank final variable in a class require?

a) It must be initialized at the time of declaration
b) It must be initialized in all constructors
c) It can never be assigned
d) It must be initialized in a subclass
Correct Answer: b) It must be initialized in all constructors
Explanation:
A blank final variable is not initialized during declaration. It must be explicitly initialized in all constructors of the class.

55. If you have a reference Parent p = new Child();, which methods can p call directly?

a) Only methods defined in Child
b) All methods in Child and Parent
c) Only methods defined in Parent (or inherited by it)
d) Only methods overridden in Child
Correct Answer: c) Only methods defined in Parent (or inherited by it)
Explanation:
The compiler restricts method access based on the reference type (Parent). To call Child-specific methods, a downcast is required.

56. Can an interface extend an abstract class?

a) Yes
b) No
c) Only if the abstract class has no concrete methods
d) Only if the abstract class implements the interface
Correct Answer: b) No
Explanation:
An interface can only extend other interfaces. It cannot extend classes, whether abstract or concrete.

57. Which of these is a valid declaration of an interface?

a) public abstract interface MyInterface {}
b) public interface MyInterface {}
c) interface MyInterface {}
d) All of the above
Correct Answer: d) All of the above
Explanation:
Interfaces are implicitly abstract. 'public abstract interface' and 'interface' (with package-private access) are both valid.

58. What is the default access modifier for methods declared in an interface (before Java 8)?

a) private
b) protected
c) public abstract
d) package-private
Correct Answer: c) public abstract
Explanation:
By default, all methods in an interface without bodies are implicitly public and abstract.

59. What is the default access modifier for variables declared in an interface?

a) public static final
b) private static
c) protected final
d) default
Correct Answer: a) public static final
Explanation:
Variables declared in an interface are implicitly public, static, and final (i.e., constants).

60. Which class inherits java.lang.Object?

a) Only standard library classes
b) Only custom classes
c) All classes directly or indirectly
d) Only abstract classes
Correct Answer: c) All classes directly or indirectly
Explanation:
Every class in Java is a descendant of the Object class, either directly or indirectly.

61. Which of the following method is NOT a part of the Object class?

a) clone()
b) equals()
c) hashCode()
d) copy()
Correct Answer: d) copy()
Explanation:
The Object class provides clone(), equals(), and hashCode(), but there is no copy() method.

62. Why does Java not support multiple inheritance with classes?

a) To reduce memory consumption
b) To prevent the Diamond Problem
c) To increase compile time
d) To force the use of interfaces
Correct Answer: b) To prevent the Diamond Problem
Explanation:
If a class inherits from two classes that have identical methods, it creates ambiguity on which method to invoke (the Diamond Problem).

63. When can a subclass invoke a superclass constructor using super()?

a) Anytime inside the subclass methods
b) Only from inside a subclass constructor as the first statement
c) Only inside a static block
d) Never
Correct Answer: b) Only from inside a subclass constructor as the first statement
Explanation:
The super() call can only be made inside a constructor, and it must be the very first statement.

64. Can we overload constructors in Java?

a) Yes
b) No
c) Only in abstract classes
d) Only in final classes
Correct Answer: a) Yes
Explanation:
Constructor overloading is perfectly valid and common, allowing classes to be instantiated in different ways.

65. Can we override constructors in Java?

a) Yes
b) No
c) Only default constructors
d) Only parameterized constructors
Correct Answer: b) No
Explanation:
Constructors are not inherited by subclasses. Since they are not inherited, they cannot be overridden.

66. What does a superclass reference variable pointing to a subclass object signify?

a) Method Overloading
b) Upcasting
c) Downcasting
d) Boxing
Correct Answer: b) Upcasting
Explanation:
Assigning a subclass object to a superclass reference is known as upcasting, and it is done implicitly in Java.

67. Which of these errors indicates an incorrect downcast at run-time?

a) ClassNotFoundException
b) IllegalAccessException
c) ClassCastException
d) UnsupportedOperationException
Correct Answer: c) ClassCastException
Explanation:
A ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.

68. If Class B inherits Class A, and Class C inherits Class B, which class can instantiate Class A?

a) Class B
b) Class C
c) Any class
d) Class A only
Correct Answer: c) Any class
Explanation:
Unless Class A is abstract or has a private constructor, any class can instantiate it using the new keyword.

69. What is the result of applying final to a parameter variable in a method?

a) The method cannot be overridden
b) The parameter cannot be passed by reference
c) The value of the parameter cannot be changed inside the method
d) The parameter becomes optional
Correct Answer: c) The value of the parameter cannot be changed inside the method
Explanation:
A final parameter acts as a constant within the method body, preventing any reassignment.

70. How can you prevent a method from being overridden?

a) Make it public
b) Make it final
c) Make it abstract
d) Make it volatile
Correct Answer: b) Make it final
Explanation:
The final keyword prevents subclasses from overriding the specific method.

71. Can an interface contain a static method?

a) No, never
b) Yes, starting from Java 8
c) Yes, but they cannot have a body
d) Only if the interface is abstract
Correct Answer: b) Yes, starting from Java 8
Explanation:
Java 8 introduced static methods in interfaces. They must have a body and are called using the interface name.

72. If an interface has a static method, how is it invoked?

a) Using an object of the implementing class
b) Using the implementing class name
c) Using the interface name
d) Using the super keyword
Correct Answer: c) Using the interface name
Explanation:
Static methods in interfaces are strictly invoked using the interface name. They are not inherited by implementing classes.

73. Which design pattern heavily relies on interfaces and polymorphism?

a) Singleton pattern
b) Strategy pattern
c) Builder pattern
d) All of the above
Correct Answer: b) Strategy pattern
Explanation:
The Strategy pattern allows changing algorithms at runtime, relying heavily on programming to an interface and dynamic polymorphism.

74. When defining an interface in Java, which keyword is omitted for its methods by convention (prior to Java 8)?

a) public abstract
b) static final
c) private protected
d) synchronized strictfp
Correct Answer: a) public abstract
Explanation:
Because interface methods are implicitly public and abstract, developers usually omit these keywords for brevity.

75. What is a marker interface?

a) An interface with exactly one method
b) An interface with no methods or fields
c) An interface used strictly for defining constants
d) An interface that marks a class as abstract
Correct Answer: b) An interface with no methods or fields
Explanation:
A marker interface has no methods or fields and is used to provide meta-data to the JVM.

76. An interface with exactly one abstract method is called:

a) A Single Interface
b) A Marker Interface
c) A Functional Interface
d) An Abstract Interface
Correct Answer: c) A Functional Interface
Explanation:
A functional interface has exactly one abstract method and can be used as the assignment target for a lambda expression.

77. Which annotation is used to enforce that an interface remains functional?

a) @MarkerInterface
b) @SingleMethod
c) @FunctionalInterface
d) @LambdaInterface
Correct Answer: c) @FunctionalInterface
Explanation:
The @FunctionalInterface annotation causes the compiler to generate an error if the interface has more than one abstract method.

78. Can a class extend itself?

a) Yes
b) No
c) Only abstract classes
d) Only inner classes
Correct Answer: b) No
Explanation:
A class cannot extend itself, as it would cause a cyclic inheritance structure which is strictly prohibited by the Java compiler.

79. What does a ClassCircularityError indicate?

a) An infinite loop in a method
b) A circular dependency in the inheritance hierarchy
c) A memory leak
d) Importing the same package twice
Correct Answer: b) A circular dependency in the inheritance hierarchy
Explanation:
The JVM throws a ClassCircularityError if it detects a loop in the superclass hierarchy of a class.

80. Which of the following is true regarding local variables and overriding?

a) Local variables can be overridden
b) Local variables cannot be overridden
c) Local variables can only be hidden
d) Local variables are inherited
Correct Answer: b) Local variables cannot be overridden
Explanation:
Local variables are defined within a method block. They are not part of the class schema, so they cannot be inherited, overridden, or hidden by subclasses.

81. Which OOP concept represents hiding the implementation details and showing only functionality to the user?

a) Polymorphism
b) Inheritance
c) Abstraction
d) Encapsulation
Correct Answer: c) Abstraction
Explanation:
Abstraction hides complex implementation details and exposes only the necessary features of an object.

82. Can we use super in a static method?

a) Yes, freely
b) No, super is tied to an object instance
c) Yes, but only to call static parent methods
d) Yes, but only in static blocks
Correct Answer: b) No, super is tied to an object instance
Explanation:
Static methods belong to the class, not an object instance. The super and this keywords refer to object instances, so they cannot be used in a static context.

83. Can we use this in a static method?

a) Yes
b) No
c) Only if the method is synchronized
d) Only to access static variables
Correct Answer: b) No
Explanation:
Like super, the this keyword refers to the current object instance, which does not exist in a static context.

84. What happens if a parent class constructor throws a checked exception?

a) The subclass constructor must handle it in a try-catch block
b) The subclass constructor must declare the exception (or its supertype) in its throws clause
c) It is automatically ignored
d) The subclass cannot be compiled
Correct Answer: b) The subclass constructor must declare the exception (or its supertype) in its throws clause
Explanation:
Since super() must be the first line, a try-catch cannot surround it. Therefore, the subclass constructor must declare the checked exception in its throws clause.

85. If you override the equals() method, which other method should you generally also override?

a) clone()
b) toString()
c) hashCode()
d) finalize()
Correct Answer: c) hashCode()
Explanation:
The contract between equals() and hashCode() requires that if two objects are equal according to equals(), they must return the same hashCode.

86. What is the access level of the clone() method in the Object class?

a) public
b) protected
c) private
d) default
Correct Answer: b) protected
Explanation:
The clone() method in java.lang.Object is protected, meaning a class must explicitly override it and make it public to allow deep or shallow copies from outside.

87. A class implements two interfaces that both have a default method void show(). The class does NOT override show(). What happens?

a) It runs the first interface's method
b) It runs the second interface's method
c) It causes a compile-time error
d) It causes a ClassCastException at runtime
Correct Answer: c) It causes a compile-time error
Explanation:
Java forces the developer to resolve the multiple-inheritance ambiguity. The class will not compile until it overrides the show() method.

88. What is an inner class in Java?

a) A class that is inherited by another class
b) A class defined within another class
c) A class that implements an interface
d) An anonymous class
Correct Answer: b) A class defined within another class
Explanation:
Inner classes (or nested classes) are classes declared entirely within the body of another class or interface.

89. Can an inner class extend another class?

a) Yes
b) No
c) Only the outer class
d) Only interfaces
Correct Answer: a) Yes
Explanation:
An inner class is a full-fledged class and can extend another class, implement interfaces, etc., just like a normal top-level class.

90. What is an anonymous inner class?

a) A class without a package
b) A class without access modifiers
c) A class without a name that is instantiated simultaneously
d) A class with no methods
Correct Answer: c) A class without a name that is instantiated simultaneously
Explanation:
Anonymous inner classes have no name and are created and instantiated in a single expression, usually to override a specific method of a class or interface.

91. Which of the following is true about polymorphism?

a) It increases coupling
b) It allows a single interface to represent different underlying forms
c) It prevents inheritance
d) It forces methods to be static
Correct Answer: b) It allows a single interface to represent different underlying forms
Explanation:
Polymorphism allows objects of different types to be treated as instances of the same class through a common interface or superclass.

92. If a superclass method throws an unchecked exception, what can the overriding subclass method throw?

a) Only the same unchecked exception
b) Any unchecked exception, or no exception
c) A checked exception
d) No exceptions at all
Correct Answer: b) Any unchecked exception, or no exception
Explanation:
Unchecked exceptions are not strictly checked by the compiler. An overriding method can throw any unchecked exceptions, regardless of what the parent throws.

93. When casting, what happens if you upcast?

a) Data loss might occur
b) You gain access to subclass-specific methods
c) The reference type restricts access to superclass methods only
d) A runtime exception is strictly thrown
Correct Answer: c) The reference type restricts access to superclass methods only
Explanation:
Upcasting restricts visibility to the methods and fields defined in the superclass (or interface), although the overridden methods will still execute.

94. If Object A has a has-a relationship with Object B, what is this called?

a) Polymorphism
b) Inheritance
c) Composition
d) Abstraction
Correct Answer: c) Composition
Explanation:
Has-a signifies Composition or Aggregation, where Object A contains a reference to Object B.

95. Which feature of Java avoids the Diamond Problem of multiple inheritance?

a) Abstract classes
b) Interfaces
c) Final classes
d) Static methods
Correct Answer: b) Interfaces
Explanation:
Interfaces completely avoid the Diamond Problem by abstracting the implementation.

96. Can an object of a superclass be automatically assigned to a subclass reference?

a) Yes
b) No, it requires explicit downcasting
c) Only if the superclass is abstract
d) Only if they share the same package
Correct Answer: b) No, it requires explicit downcasting
Explanation:
A superclass object cannot be assigned to a subclass reference implicitly. It must be explicitly cast, which may throw a ClassCastException if invalid.

97. What is the output of null instanceof Object in Java?

a) true
b) false
c) Compilation error
d) NullPointerException
Correct Answer: b) false
Explanation:
The instanceof operator always returns false if the left operand is null, because null is not an instance of any object.

98. Which of these forms of polymorphism is resolved during compilation?

a) Method Overriding
b) Method Overloading
c) Virtual Method Invocation
d) Dynamic Binding
Correct Answer: b) Method Overloading
Explanation:
Method overloading is resolved at compile-time (static binding), based on the reference type and parameter signatures.

99. What is the core difference between an abstract class and an interface?

a) Abstract classes can have state (instance variables), interfaces cannot
b) Interfaces can have state, abstract classes cannot
c) Interfaces can have constructors, abstract classes cannot
d) Abstract classes support multiple inheritance, interfaces do not
Correct Answer: a) Abstract classes can have state (instance variables), interfaces cannot
Explanation:
Abstract classes can declare instance variables and maintain state. Interfaces can only declare constant variables.

100. Can an abstract class be declared as final?

a) Yes
b) No
c) Only if it has no abstract methods
d) Only if it inherits from Object
Correct Answer: b) No
Explanation:
An abstract class is meant to be subclassed, while a final class cannot be subclassed. The two keywords are mutually exclusive.

101. What does Dynamic Binding refer to?

a) Linking a method call to a method body at compile-time
b) Linking a method call to a method body at run-time
c) Binding a variable to a data type dynamically
d) Garbage collection of unbound objects
Correct Answer: b) Linking a method call to a method body at run-time
Explanation:
Dynamic binding (or late binding) resolves the method call at run-time based on the actual object type. This is how method overriding is executed.

102. What does Static Binding refer to?

a) Resolving a method call at compile-time
b) Resolving a method call at run-time
c) Making all methods static
d) Preventing inheritance
Correct Answer: a) Resolving a method call at compile-time
Explanation:
Static binding (early binding) occurs at compile-time. Overloaded methods, private methods, final methods, and static methods use static binding.

103. If a method is final, does it use static or dynamic binding?

a) Dynamic Binding
b) Static Binding
c) Neither
d) It depends on the access modifier
Correct Answer: b) Static Binding
Explanation:
Because final methods cannot be overridden, the compiler knows exactly which method implementation to call, so it uses static binding.

104. Which keyword prevents an object from being serialized?

a) final
b) static
c) transient
d) volatile
Correct Answer: c) transient
Explanation:
While unrelated to polymorphism directly, transient fields are ignored during Java serialization, distinguishing them from inherited object state.

105. Can you override a method to throw a broader checked exception?

a) Yes
b) No
c) Only if it is IOException
d) Only if the method is abstract
Correct Answer: b) No
Explanation:
An overriding method cannot throw checked exceptions that are new or broader than those declared by the overridden method.

106. If class Dog extends class Animal, and Dog overrides the bark() method. What happens when Animal a = new Dog(); a.bark(); is executed?

a) Animal's bark() is executed
b) Dog's bark() is executed
c) Compilation Error
d) Runtime Exception
Correct Answer: b) Dog's bark() is executed
Explanation:
Due to runtime polymorphism (dynamic method dispatch), the JVM invokes the overridden method of the actual object (Dog) being referred to.

107. What happens if a parent class lacks a no-argument constructor and the child class constructor does not use super(args)?

a) The compiler adds a no-argument constructor to the parent class
b) The child class compiles successfully
c) A compilation error occurs in the child class
d) A runtime exception is thrown
Correct Answer: c) A compilation error occurs in the child class
Explanation:
The child class constructor implicitly calls super(). If the parent has no default constructor, the developer must explicitly call the parameterized super(...) to avoid a compilation error.

108. Which feature in Java provides the ability to take on many forms?

a) Encapsulation
b) Abstraction
c) Polymorphism
d) Multithreading
Correct Answer: c) Polymorphism
Explanation:
Polymorphism literally translates to many forms and allows objects to be treated as instances of their parent class rather than their actual class.

109. When overloading methods, what differentiates one method from another?

a) Method signatures
b) Return types
c) Access modifiers
d) Exceptions thrown
Correct Answer: a) Method signatures
Explanation:
Methods must have unique signatures. A method signature in Java consists of the method name and the parameter list.

110. Is it possible to overload a method by solely changing the parameter names?

a) Yes
b) No
c) Only for abstract methods
d) Only for static methods
Correct Answer: b) No
Explanation:
Parameter names do not matter to the compiler. Overloading requires a change in the type, number, or order of the parameters.

111. Does overriding apply to instance variables in Java?

a) Yes, just like methods
b) No, instance variables are hidden, not overridden
c) Yes, but only if they are public
d) No, variables cannot have identical names
Correct Answer: b) No, instance variables are hidden, not overridden
Explanation:
Java supports method overriding but not variable overriding. Variables are resolved based on the reference type (hiding), not the object type.

112. Which principle suggests that objects of a superclass should be replaceable with objects of its subclasses without affecting the program?

a) Single Responsibility Principle
b) Open-Closed Principle
c) Liskov Substitution Principle
d) Interface Segregation Principle
Correct Answer: c) Liskov Substitution Principle
Explanation:
The Liskov Substitution Principle (part of SOLID) states that derived classes must be substitutable for their base classes.

113. If interface A defines default method run() and interface B defines default method run(), can class C implement both A and B?

a) No, it is strictly prohibited
b) Yes, but C must override run() to resolve the ambiguity
c) Yes, C will randomly select one run()
d) Yes, C will execute both run() methods
Correct Answer: b) Yes, but C must override run() to resolve the ambiguity
Explanation:
Java allows multiple inheritance of interfaces, but if a method signature conflict arises with default methods, the implementing class must override it.

114. Can you cast an object array to a superclass object array?

a) Yes, automatically (Array covariance)
b) No, arrays are strictly invariant
c) Only explicitly
d) Only if the array contains primitive types
Correct Answer: a) Yes, automatically (Array covariance)
Explanation:
Java arrays are covariant, meaning if Sub is a subtype of Super, then Sub[] is a subtype of Super[].

115. What exception might arise due to Array Covariance if you assign a wrong object type at runtime?

a) ClassCastException
b) ArrayStoreException
c) IndexOutOfBoundsException
d) TypeMismatchException
Correct Answer: b) ArrayStoreException
Explanation:
If a Sub[] is referenced by a Super[], and you try to put a different sibling Sub2 into it, Java throws an ArrayStoreException at runtime.

116. Which of these classes cannot be subclassed?

a) java.lang.String
b) java.lang.Object
c) java.util.ArrayList
d) java.lang.Thread
Correct Answer: a) java.lang.String
Explanation:
The String class in Java is declared as final to maintain immutability and security, hence it cannot be subclassed.

117. What is Object Slicing?

a) Removing methods from a subclass
b) Converting an object to an array
c) Assigning a subclass object to a superclass value, losing subclass specifics
d) Splitting a class into two interfaces
Correct Answer: c) Assigning a subclass object to a superclass value, losing subclass specifics
Explanation:
Common in C++, object slicing occurs when assigning a derived object to a base object by value. In Java, objects are managed by reference, so actual object slicing does not happen.

118. How do you call a default method of an interface MyInterface from an implementing class?

a) super.methodName()
b) MyInterface.super.methodName()
c) MyInterface.methodName()
d) this.methodName()
Correct Answer: b) MyInterface.super.methodName()
Explanation:
To invoke a specific interface's default method, the syntax InterfaceName.super.methodName() is used.

119. Is it possible to override a final method?

a) Yes
b) No
c) Only in abstract classes
d) Only if the subclass method is also final
Correct Answer: b) No
Explanation:
The final modifier on a method explicitly prevents any subclass from overriding it.

120. Can you declare an abstract method as private?

a) Yes
b) No
c) Only inside final classes
d) Only inside interfaces
Correct Answer: b) No
Explanation:
Abstract methods must be implemented by subclasses. A private method is not visible to subclasses, making it impossible to implement.

121. Can you declare an abstract method as static?

a) Yes
b) No
c) Only in abstract classes
d) Only in final classes
Correct Answer: b) No
Explanation:
Static methods belong to the class and are resolved at compile time, whereas abstract methods require dynamic resolution via subclasses.

122. Which of these is the correct way to test if object obj is an instance of class MyClass?

a) obj.isInstance(MyClass)
b) obj instanceof MyClass
c) MyClass instanceof obj
d) obj == MyClass
Correct Answer: b) obj instanceof MyClass
Explanation:
The instanceof keyword evaluates if the left-side reference is an instance of the right-side type.

123. If you upcast a subclass object, do you lose the data of the subclass fields?

a) Yes, they are deleted from memory
b) No, they remain in memory but are hidden from the reference
c) Yes, they are set to null
d) No, they are fully accessible
Correct Answer: b) No, they remain in memory but are hidden from the reference
Explanation:
Upcasting changes the reference type, restricting access to subclass-specific members, but the underlying object in memory remains completely intact.

124. What is a sealed class in newer Java versions (Java 15+)?

a) A class that cannot be instantiated
b) A class that restricts which other classes can extend it
c) A synonym for final class
d) A class with no visible fields
Correct Answer: b) A class that restricts which other classes can extend it
Explanation:
Sealed classes (and interfaces) limit inheritance to a known set of subclasses, specified using the permits clause.

125. If a method is overloaded, does the compiler look at the return type to distinguish the methods?

a) Yes
b) No
c) Only if the parameters are the same
d) Only for primitive types
Correct Answer: b) No
Explanation:
The compiler uses the method signature (name and parameter list) to distinguish overloaded methods. The return type is ignored for signature matching.

126. When a method has a covariant return type, the return type in the overriding method must be...

a) A superclass of the return type in the overridden method
b) Exactly the same type
c) A subclass of the return type in the overridden method
d) Void
Correct Answer: c) A subclass of the return type in the overridden method
Explanation:
Covariant return types allow a subclass method to return a more specific type (a subclass) than the method it overrides.
← Previous: Java Functions MCQs
Next →: Java Interfaces and Abstract Classes MCQs
JavaScript This Keyword & Scope

JavaScript This Keyword & Scope MCQs

Variable scoping and declaration keywords (var, let, and const) determine where identifiers are visible and accessible within a JavaScript execution…

By MCQs Generator
Java Interview Questions MCQs

Java Interview Questions MCQs

Preparing for technical interviews requires a deep understanding of object-oriented programming paradigms, memory management, runtime behavior, and modern language enhancements.…

By MCQs Generator
Java Multithreading & Concurrency MCQs

Java Multithreading & Concurrency MCQs

Java multithreading and concurrency are core pillars for building high-performance, responsive applications that execute multiple tasks simultaneously. Utilizing the java.util.concurrent…

By MCQs Generator