Java Interfaces and Abstract Classes MCQs

1 min read

Java interfaces and abstract classes are core constructs used to implement abstraction and enforce design contracts in object-oriented architecture. An abstract class permits partial code implementation, state retention via instance variables, and standard single class inheritance, whereas an interface historically defined pure abstract behavior contracts now enhanced with modern default and static methods. Since Java does not support multiple class inheritance, interfaces provide a safe mechanism to achieve multiple inheritance of behavior. Mastering the structural differences and design application rules of both types is vital for software engineering interviews and certification exams.

1. Which keyword is used to define an abstract class in Java?

a) virtual
b) abstract
c) interface
d) extends
Correct Answer: b) abstract
Explanation:
In Java, the abstract keyword is used to declare a class as abstract, meaning it cannot be instantiated.

2. Can an abstract class be instantiated directly using the new keyword?

a) Yes, always
b) No, never
c) Yes, if it has a constructor
d) Yes, if all its methods are implemented
Correct Answer: b) No, never
Explanation:
Abstract classes are incomplete by design and cannot be instantiated directly. They must be subclassed.

3. What is the primary purpose of an abstract class?

a) To create multiple inheritances
b) To hide all variables
c) To provide a common base for subclasses to build upon
d) To force all methods to be static
Correct Answer: c) To provide a common base for subclasses to build upon
Explanation:
An abstract class serves as a blueprint for other classes, allowing shared code and defining a common template.

4. Which of the following can an abstract class contain?

a) Only abstract methods
b) Only non-abstract methods
c) Both abstract and non-abstract methods
d) Neither abstract nor non-abstract methods
Correct Answer: c) Both abstract and non-abstract methods
Explanation:
Abstract classes can have a mix of abstract methods (without a body) and concrete methods (with a body).

5. Does an abstract class require at least one abstract method?

a) Yes, otherwise it will not compile
b) No, an abstract class can have zero abstract methods
c) Yes, at least one method must be abstract and public
d) No, but it must have at least one final method
Correct Answer: b) No, an abstract class can have zero abstract methods
Explanation:
An abstract class is not required to have any abstract methods. However, if a class has an abstract method, the class itself must be declared abstract.

6. Can a class extend more than one abstract class in Java?

a) Yes
b) No
c) Only if the abstract classes share the same package
d) Only if they do not have conflicting methods
Correct Answer: b) No
Explanation:
Java does not support multiple inheritance for classes. A class can extend only one superclass, whether it is abstract or concrete.

7. Which keyword is used to implement an interface in Java?

a) inherits
b) extends
c) implements
d) interfaces
Correct Answer: c) implements
Explanation:
The implements keyword is used by a class to implement the methods declared in an interface.

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

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

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

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

10. Can a class implement multiple interfaces in Java?

a) No, Java does not support multiple inheritance
b) Yes, by separating interface names with a comma
c) Yes, but only if they have the exact same methods
d) No, interfaces can only extend other interfaces
Correct Answer: b) Yes, by separating interface names with a comma
Explanation:
Java supports multiple inheritance of type through interfaces. A class can implement any number of interfaces.

11. If a concrete class implements an interface, what must it do?

a) Provide implementations for all abstract methods of the interface
b) Declare itself as abstract
c) Implement at least one method of the interface
d) Redeclare the interface variables
Correct Answer: a) Provide implementations for all abstract methods of the interface
Explanation:
A concrete class must provide method bodies for all the abstract methods defined in the interface it implements.

12. What happens if a class implements an interface but does not provide implementations for all its abstract methods?

a) It causes a compilation error unless the class is declared abstract
b) It compiles successfully and the methods are ignored
c) It causes a runtime exception
d) The compiler provides default implementations automatically
Correct Answer: a) It causes a compilation error unless the class is declared abstract
Explanation:
If a class does not implement all abstract methods of its interfaces, the class itself must be declared abstract; otherwise, it will not compile.

13. Can an interface extend another interface?

a) No, interfaces cannot use the extends keyword
b) Yes, using the implements keyword
c) Yes, using the extends keyword
d) No, interfaces are always final
Correct Answer: c) Yes, using the extends keyword
Explanation:
An interface can extend another interface (or multiple interfaces) using the extends keyword.

14. Which feature was introduced in interfaces in Java 8?

a) Private methods
b) Default methods
c) Protected methods
d) Constructors
Correct Answer: b) Default methods
Explanation:
Java 8 introduced default and static methods in interfaces, allowing interfaces to provide method implementations without breaking existing implementations.

15. What is the purpose of a default method in an interface?

a) To provide a default constructor for the interface
b) To prevent classes from overriding the method
c) To allow adding new methods to interfaces without breaking existing implementations
d) To force implementing classes to use the default logic
Correct Answer: c) To allow adding new methods to interfaces without breaking existing implementations
Explanation:
Default methods allow backward compatibility by providing an implementation that existing classes inherit if they do not override it.

16. How do you declare a default method in an interface?

a) Using the default keyword before the return type
b) Using the default access modifier
c) By simply providing a method body without any special keyword
d) Using the abstract default keywords
Correct Answer: a) Using the default keyword before the return type
Explanation:
In an interface, a method with an implementation must be marked with the default keyword (or static).

17. Can an abstract class have a constructor?

a) No, because it cannot be instantiated
b) Yes, and it is called when a concrete subclass is instantiated
c) Yes, but it must be private
d) Yes, but only a default no-argument constructor is allowed
Correct Answer: b) Yes, and it is called when a concrete subclass is instantiated
Explanation:
Abstract classes can have constructors. They are executed during the instantiation of a concrete subclass via the super call.

18. Can an interface have a constructor?

a) Yes
b) No
c) Only in Java 8 and above
d) Only if all methods are default
Correct Answer: b) No
Explanation:
Interfaces cannot have constructors because they cannot maintain state (instance variables) and are not part of the class instantiation chain.

19. Can an abstract class be declared as final?

a) Yes
b) No
c) Only if it has no abstract methods
d) Only if it implements an interface
Correct Answer: b) No
Explanation:
An abstract class is meant to be subclassed, while a final class cannot be subclassed. Using both keywords together causes a compilation error.

20. Can an interface be declared as final?

a) Yes
b) No
c) Only if it has no default methods
d) Only if it is a functional interface
Correct Answer: b) No
Explanation:
An interface is meant to be implemented. A final modifier prevents implementation, so it is illegal for an interface.

21. Which of the following is true about static methods in interfaces (Java 8 and above)?

a) They can be overridden by implementing classes
b) They are called using the interface name
c) They are called using an instance of the implementing class
d) They must be abstract
Correct Answer: b) They are called using the interface name
Explanation:
Static methods in interfaces belong to the interface itself and must be invoked using the interface name (e.g., InterfaceName.methodName()).

22. Can a static method in an interface access instance variables of the interface?

a) Yes, always
b) Yes, if they are public
c) No, because interfaces do not have instance variables
d) No, because static methods cannot access instance members
Correct Answer: c) No, because interfaces do not have instance variables
Explanation:
All variables in an interface are implicitly static and final. Interfaces do not have instance variables.

23. Which feature was introduced in interfaces in Java 9?

a) Default methods
b) Static methods
c) Private methods
d) Abstract methods
Correct Answer: c) Private methods
Explanation:
Java 9 introduced private methods (and private static methods) in interfaces to allow code sharing between default and static methods.

24. What is the primary use of private methods in an interface?

a) To hide API logic from implementing classes
b) To encapsulate helper logic shared among default methods
c) To allow classes to override them privately
d) To replace abstract methods
Correct Answer: b) To encapsulate helper logic shared among default methods
Explanation:
Private methods in interfaces are used to refactor and share common code between default methods without exposing that logic to the public API.

25. If Class A implements Interface B and Interface C, and both interfaces have a default method void display(), what happens?

a) Class A compiles successfully and uses Interface B's method
b) Class A compiles successfully and uses Interface C's method
c) Class A fails to compile unless it overrides the display() method
d) Class A throws a runtime exception
Correct Answer: c) Class A fails to compile unless it overrides the display() method
Explanation:
This causes the Diamond Problem. The compiler forces Class A to override the conflicting default method to resolve the ambiguity.

26. How can Class A invoke Interface B's default display() method if there is a conflict?

a) super.display()
b) B.display()
c) B.super.display()
d) this.display()
Correct Answer: c) B.super.display()
Explanation:
To invoke a specific interface's default method from the overriding method in the class, use the syntax InterfaceName.super.methodName().

27. Can an abstract class have static methods?

a) Yes
b) No
c) Only if they are abstract
d) Only if they are private
Correct Answer: a) Yes
Explanation:
Abstract classes can have static methods, which can be called using the abstract class name.

28. Can an abstract class have final methods?

a) No, because methods in abstract classes must be overridable
b) Yes
c) Only if the class itself is final
d) Only if they are abstract
Correct Answer: b) Yes
Explanation:
An abstract class can have final methods. These methods cannot be overridden by subclasses, ensuring the implementation remains unchanged.

29. Can an abstract method be declared as final?

a) Yes
b) No
c) Only in abstract classes
d) Only in interfaces
Correct Answer: b) No
Explanation:
An abstract method must be overridden by a subclass, while a final method cannot be overridden. These modifiers contradict each other.

30. Can an abstract method be declared as private?

a) Yes
b) No
c) Only in interfaces
d) Only in Java 9+
Correct Answer: b) No
Explanation:
Abstract methods must be implemented by a subclass, meaning they must be visible to the subclass. Private methods are not inherited, so they cannot be abstract.

31. Can an abstract method be declared as static?

a) Yes
b) No
c) Only in interfaces
d) Only in abstract classes
Correct Answer: b) No
Explanation:
Static methods belong to the class and are resolved at compile time, whereas abstract methods rely on dynamic polymorphism (overriding) which is an instance-level concept.

32. What is a Functional Interface in Java?

a) An interface that has no methods
b) An interface that has exactly one abstract method
c) An interface that has only default methods
d) An interface that extends the Function class
Correct Answer: b) An interface that has exactly one abstract method
Explanation:
A functional interface is an interface that contains exactly one abstract method. It can have multiple default or static methods.

33. Which annotation is used to explicitly declare a functional interface?

a) @Function
b) @Functional
c) @FunctionalInterface
d) @LambdaInterface
Correct Answer: c) @FunctionalInterface
Explanation:
The @FunctionalInterface annotation tells the compiler to check that the interface has exactly one abstract method.

34. Is the @FunctionalInterface annotation mandatory for functional interfaces?

a) Yes, the compiler requires it
b) No, it is optional but recommended
c) Yes, if lambda expressions are used
d) No, unless it extends another interface
Correct Answer: b) No, it is optional but recommended
Explanation:
The annotation is optional. Any interface with exactly one abstract method is treated as a functional interface by the compiler, but the annotation prevents accidental addition of abstract methods.

35. Which of the following is a standard functional interface in Java?

a) java.lang.Runnable
b) java.util.List
c) java.io.Serializable
d) java.lang.Cloneable
Correct Answer: a) java.lang.Runnable
Explanation:
Runnable has exactly one abstract method (run()) and is a widely used functional interface.

36. 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 (like Serializable or Cloneable) has no methods or fields. It provides run-time type information to the JVM.

37. Which of the following is an example of a marker interface in Java?

a) Comparable
b) Runnable
c) Serializable
d) Callable
Correct Answer: c) Serializable
Explanation:
java.io.Serializable is a marker interface. It has no methods and tells the JVM that the object can be serialized.

38. What is the key difference between an abstract class and an interface regarding state?

a) Interfaces can maintain state using instance variables, abstract classes cannot
b) Abstract classes can maintain state using instance variables, interfaces cannot
c) Both can maintain state
d) Neither can maintain state
Correct Answer: b) Abstract classes can maintain state using instance variables, interfaces cannot
Explanation:
Abstract classes can have instance variables to store object state. Variables in interfaces are always public static final (constants), meaning they cannot store instance state.

39. When should you use an abstract class instead of an interface?

a) When you want to support multiple inheritance
b) When you want to define a contract for unrelated classes
c) When classes share a common core implementation and state
d) When you only need to define abstract methods
Correct Answer: c) When classes share a common core implementation and state
Explanation:
Abstract classes are best when you have closely related classes that share code and state (fields). Interfaces are better for defining contracts across unrelated classes.

40. Can an abstract class implement an interface?

a) Yes, and it must provide implementations for all interface methods
b) Yes, and it can leave the interface methods abstract
c) No, abstract classes cannot use the implements keyword
d) No, only concrete classes can implement interfaces
Correct Answer: b) Yes, and it can leave the interface methods abstract
Explanation:
An abstract class can implement an interface without implementing its methods. The responsibility passes to the first concrete subclass.

41. If an interface declares a method void doSomething(), what are its implicit modifiers?

a) private abstract
b) public abstract
c) protected abstract
d) default abstract
Correct Answer: b) public abstract
Explanation:
Method declarations in interfaces without a body or explicitly stated modifiers are implicitly public and abstract.

42. Can you declare a method as protected in an interface?

a) Yes
b) No
c) Only default methods
d) Only static methods
Correct Answer: b) No
Explanation:
Interface methods can only be public (abstract, default, static) or private (Java 9 and above). Protected access is not allowed.

43. If a concrete class implements an interface, what access modifier must the implemented methods have?

a) private
b) protected
c) package-private
d) public
Correct Answer: d) public
Explanation:
Since interface methods are implicitly public, the implementing class must declare them as public because Java prevents reducing visibility during overriding.

44. Can an interface contain a nested class?

a) No
b) Yes, and it is implicitly public and static
c) Yes, but it must be private
d) Yes, but it cannot be static
Correct Answer: b) Yes, and it is implicitly public and static
Explanation:
Interfaces can contain nested classes, and they are implicitly public and static.

45. Can an interface contain a nested interface?

a) No
b) Yes, and it is implicitly public and static
c) Yes, but it must be private
d) Yes, but only in abstract classes
Correct Answer: b) Yes, and it is implicitly public and static
Explanation:
Just like nested classes, interfaces can contain nested interfaces, which are implicitly public and static.

46. Does a functional interface allow methods inherited from the Object class?

a) No, it will break the single abstract method rule
b) Yes, overriding Object methods does not count toward the abstract method limit
c) Yes, but they must be declared as default methods
d) No, interfaces cannot mention Object methods
Correct Answer: b) Yes, overriding Object methods does not count toward the abstract method limit
Explanation:
If an interface declares an abstract method overriding one of the public methods of java.lang.Object (like equals or toString), it does not count against the limit of one abstract method.

47. What is the output of declaring int x = 10; inside an interface?

a) It acts as a normal instance variable
b) It is implicitly public, static, and final
c) It causes a compilation error because variables must be explicit constants
d) It creates a thread-local variable
Correct Answer: b) It is implicitly public, static, and final
Explanation:
Any field declared inside an interface is automatically implicitly public, static, and final.

48. Can an abstract class extend a concrete class?

a) Yes
b) No, it can only extend other abstract classes
c) No, it can only implement interfaces
d) Only if the concrete class is final
Correct Answer: a) Yes
Explanation:
An abstract class can extend a concrete class, just like it can extend another abstract class.

49. Which of the following creates an anonymous class?

a) Runnable r = new Runnable();
b) Runnable r = new Runnable() { public void run() {} };
c) Runnable r = class {} implements Runnable;
d) Runnable r = anonymous Runnable();
Correct Answer: b) Runnable r = new Runnable() { public void run() {} };
Explanation:
Anonymous classes are created on the fly by combining instantiation and class definition in a single statement using curly braces.

50. Can you instantiate an interface using an anonymous class?

a) No, interfaces cannot be instantiated at all
b) Yes, by providing implementations for its abstract methods inline
c) Yes, but only if it is a marker interface
d) No, you must create a named concrete class
Correct Answer: b) Yes, by providing implementations for its abstract methods inline
Explanation:
While you cannot instantiate an interface directly, you can create an instance of an anonymous inner class that implements the interface.

51. Is it mandatory to use the @Override annotation when implementing an interface method?

a) Yes, it causes a compilation error otherwise
b) No, it is optional but highly recommended
c) No, it is only allowed for superclass methods, not interfaces
d) Yes, but only for default methods
Correct Answer: b) No, it is optional but highly recommended
Explanation:
The @Override annotation is optional but helps prevent errors (like typos in method names) by instructing the compiler to verify the override.

52. Can a class extend an abstract class and implement multiple interfaces simultaneously?

a) Yes, for example class A extends B implements C, D
b) No, a class can do one or the other, not both
c) Yes, but implements must come before extends
d) No, an abstract class prevents interface implementation
Correct Answer: a) Yes, for example class A extends B implements C, D
Explanation:
A Java class can extend one superclass (abstract or concrete) and implement any number of interfaces simultaneously. The extends keyword must precede implements.

53. If Class A and Interface B both define a variable int X = 10, and Class C extends A and implements B, how does C access B's variable?

a) C.X
b) super.X
c) B.X
d) It cannot access it due to ambiguity
Correct Answer: c) B.X
Explanation:
Interface variables are static, so the conflict is resolved by referring to the interface explicitly using its name: B.X.

54. Can an abstract class define a main method?

a) No, abstract classes cannot execute
b) Yes, and it can be used to run the application
c) Yes, but it must be abstract
d) Yes, but it cannot take arguments
Correct Answer: b) Yes, and it can be used to run the application
Explanation:
An abstract class can contain a valid public static void main(String[] args) method, and the JVM can execute it.

55. Can an interface define a main method?

a) No, interfaces cannot have static methods
b) No, the JVM does not recognize interfaces for execution
c) Yes, since Java 8 it is possible and executable
d) Yes, but it must be a default method
Correct Answer: c) Yes, since Java 8 it is possible and executable
Explanation:
Starting in Java 8, interfaces can have static methods, including a public static void main method, allowing them to serve as the entry point.

56. Which modifier is not allowed for an abstract class?

a) public
b) final
c) strictfp
d) default (package-private)
Correct Answer: b) final
Explanation:
The final modifier prevents subclassing, which contradicts the purpose of an abstract class.

57. If an abstract class A contains an abstract method foo(), can an abstract subclass B inherit A without implementing foo()?

a) Yes
b) No, it must implement it
c) Only if B re-declares foo() as abstract
d) Yes, but B must be an interface
Correct Answer: a) Yes
Explanation:
An abstract subclass does not have to implement the abstract methods inherited from its parent abstract class. The obligation falls to the first concrete subclass.

58. Can a concrete subclass declare a method abstract if it overrides it?

a) No, concrete classes cannot contain abstract methods
b) Yes, if the superclass method is concrete
c) Yes, but it throws a runtime exception
d) Only if the method is private
Correct Answer: a) No, concrete classes cannot contain abstract methods
Explanation:
A concrete class cannot contain or declare abstract methods. Doing so requires the class itself to be marked as abstract.

59. What happens if an abstract class does not provide a constructor?

a) It causes a compilation error
b) The compiler generates a default no-argument constructor
c) Subclasses cannot be instantiated
d) The class becomes a static utility class
Correct Answer: b) The compiler generates a default no-argument constructor
Explanation:
Just like regular classes, if no constructor is defined in an abstract class, the compiler automatically provides a default no-argument constructor.

60. Which interface is used to indicate that an object can be cloned?

a) Clonable
b) java.lang.Cloneable
c) java.util.Cloneable
d) Copyable
Correct Answer: b) java.lang.Cloneable
Explanation:
The java.lang.Cloneable interface is a marker interface used to indicate that the Object.clone() method is legal for instances of that class.

61. What happens if a class implements Cloneable but does not override the clone() method?

a) It causes a compilation error
b) It will throw CloneNotSupportedException at runtime
c) It uses the default Object.clone() shallow copy implementation
d) It creates a deep copy automatically
Correct Answer: c) It uses the default Object.clone() shallow copy implementation
Explanation:
Since Cloneable is a marker interface, it has no methods. Implementing it allows the protected Object.clone() method to create a shallow copy without throwing an exception.

62. Which built-in functional interface takes an argument and returns a boolean?

a) Consumer
b) Supplier
c) Predicate
d) Function
Correct Answer: c) Predicate
Explanation:
java.util.function.Predicate is a functional interface representing a predicate (boolean-valued function) of one argument.

63. Which built-in functional interface takes no arguments and returns a result?

a) Supplier
b) Consumer
c) Runnable
d) Predicate
Correct Answer: a) Supplier
Explanation:
java.util.function.Supplier is a functional interface representing a supplier of results, which takes no arguments.

64. Can an interface be package-private (default access)?

a) No, they must be public
b) Yes, by omitting the access modifier
c) No, they are implicitly public
d) Yes, using the package keyword
Correct Answer: b) Yes, by omitting the access modifier
Explanation:
Interfaces can be package-private (accessible only within the same package) if no access modifier is specified.

65. Can a top-level abstract class be private?

a) Yes
b) No
c) Only if it has no methods
d) Only if it is also static
Correct Answer: b) No
Explanation:
Top-level classes (including abstract classes) can only be public or package-private. They cannot be private or protected.

66. Can an interface variable be reassigned?

a) Yes, inside a static method
b) Yes, inside the implementing class
c) No, because it is implicitly final
d) No, because interfaces cannot have variables
Correct Answer: c) No, because it is implicitly final
Explanation:
Interface variables are implicitly static and final, making them constants. Their values cannot be modified once initialized.

67. Can an abstract class extend a final class?

a) Yes
b) No, because final classes cannot be extended
c) Yes, but it cannot access its private members
d) No, because final classes do not have abstract methods
Correct Answer: b) No, because final classes cannot be extended
Explanation:
A final class explicitly prevents any class (abstract or concrete) from extending it.

68. If you need an API that forces subclasses to override a method but also provides a stateful base implementation, what should you use?

a) An interface with default methods
b) A marker interface
c) An abstract class
d) A functional interface
Correct Answer: c) An abstract class
Explanation:
An abstract class is required because it can hold state (instance variables) while defining abstract methods that force subclass implementation.

69. What is the primary difference between a Java 8 default method and an abstract class concrete method?

a) Default methods cannot access instance state, whereas abstract class methods can
b) Default methods must be overridden, whereas abstract class methods are optional
c) There is no difference
d) Default methods are static, whereas abstract class methods are instance-level
Correct Answer: a) Default methods cannot access instance state, whereas abstract class methods can
Explanation:
Interfaces do not have instance state (fields), so default methods cannot access or mutate object state, unlike concrete methods in abstract classes.

70. Which standard Java interface allows an object to be sorted using Collections.sort()?

a) Sortable
b) Comparable
c) Ordered
d) Sequence
Correct Answer: b) Comparable
Explanation:
The java.lang.Comparable interface imposes a total ordering on the objects of each class that implements it, via the compareTo method.

71. Is java.lang.String an abstract class, a concrete class, or an interface?

a) Abstract class
b) Interface
c) Concrete class (final)
d) Marker interface
Correct Answer: c) Concrete class (final)
Explanation:
String is a final, concrete class in Java.

72. Can an interface specify a static initializer block?

a) Yes
b) No, interfaces cannot have static initialization blocks
c) Only in Java 9+
d) Yes, but it must be default
Correct Answer: b) No, interfaces cannot have static initialization blocks
Explanation:
Interfaces are not allowed to have static initializer blocks. Constants must be initialized at the time of declaration.

73. Can an abstract class have a static initializer block?

a) Yes
b) No
c) Only if it implements an interface
d) Only if it has no constructors
Correct Answer: a) Yes
Explanation:
Abstract classes are standard classes and can contain static initializer blocks, instance initializer blocks, and constructors.

74. What happens if a class defines a method with the same signature as a default method in an interface it implements?

a) A compilation error occurs
b) The default method takes precedence
c) The class method overrides the default method
d) A runtime exception is thrown
Correct Answer: c) The class method overrides the default method
Explanation:
Class implementations always win over interface default implementations. The class method takes precedence.

75. Can a superclass method implementation win over an interface default method?

a) Yes, classes always win over interfaces
b) No, the default method overrides the superclass method
c) It causes a compilation error requiring explicit override
d) Yes, but only if the superclass is abstract
Correct Answer: a) Yes, classes always win over interfaces
Explanation:
According to Java's inheritance rules, a method inherited from a superclass (abstract or concrete) always takes precedence over a default method from an interface.

76. What happens if Interface A and Interface B both extend Interface C which has a default method, and Class D implements A and B?

a) Class D compiles successfully and inherits C's default method
b) Class D fails to compile due to the diamond problem
c) Class D must implement the method to compile
d) The JVM throws a runtime error
Correct Answer: a) Class D compiles successfully and inherits C's default method
Explanation:
Because neither A nor B override C's default method, there is only one implementation (from C), so no ambiguity exists and D compiles successfully.

77. What if Interface A overrides C's default method, B does not, and D implements A and B?

a) D must override the method
b) D inherits A's overridden default method
c) D inherits C's original default method
d) Compilation error
Correct Answer: b) D inherits A's overridden default method
Explanation:
The most specific default-providing interface wins. Since A extends C and provides a more specific implementation, A's method is inherited by D.

78. Which keyword prevents an abstract class from being extended by classes outside its specified allowed list (Java 15 and above)?

a) strictfp
b) sealed
c) final
d) exclusive
Correct Answer: b) sealed
Explanation:
Java 15 introduced sealed classes and interfaces, which restrict which other classes or interfaces may extend or implement them using the permits clause.

79. Can an interface be sealed?

a) Yes
b) No, only classes can be sealed
c) Only if it is a functional interface
d) Only if it contains default methods
Correct Answer: a) Yes
Explanation:
Both classes and interfaces can be sealed in modern Java to restrict their implementation or extension.

80. If an interface is sealed, what must the permitted implementing classes declare?

a) They must be final, sealed, or non-sealed
b) They must be abstract
c) They must be private
d) They must be functional interfaces
Correct Answer: a) They must be final, sealed, or non-sealed
Explanation:
Subclasses or implementers of a sealed class/interface must explicitly declare themselves as final (if a class), sealed, or non-sealed.

81. Which of these design principles aligns with using interfaces in Java?

a) Programming to an implementation
b) Programming to an interface, not an implementation
c) Favoring inheritance over composition
d) Tight coupling
Correct Answer: b) Programming to an interface, not an implementation
Explanation:
Using interfaces promotes loose coupling and abstraction, forming the core of the principle program to an interface, not an implementation.

82. Can an abstract class define a volatile variable?

a) Yes
b) No
c) Only in static blocks
d) Only if the class implements Runnable
Correct Answer: a) Yes
Explanation:
Abstract classes can declare instance variables with standard modifiers, including volatile for thread safety.

83. Can an interface define a volatile variable?

a) Yes
b) No, because interface variables are implicitly final
c) Only if it is a functional interface
d) Yes, but it must be private
Correct Answer: b) No, because interface variables are implicitly final
Explanation:
A variable cannot be both final and volatile in Java. Since interface fields are implicitly final, they cannot be volatile.

84. Which standard Java interface allows iteration over a collection?

a) Iterator
b) Iterable
c) Collection
d) List
Correct Answer: b) Iterable
Explanation:
The java.lang.Iterable interface provides the iterator() method and is the root interface for collection iteration, enabling the enhanced for-loop.

85. Is java.util.List an abstract class or an interface?

a) Abstract class
b) Interface
c) Concrete class
d) Marker interface
Correct Answer: b) Interface
Explanation:
java.util.List is a core interface in the Java Collections Framework.

86. Does an abstract class inherit from java.lang.Object?

a) Yes, always
b) No, it is independent
c) Only if explicitly stated
d) Only concrete classes inherit Object
Correct Answer: a) Yes, always
Explanation:
Every class in Java, whether abstract or concrete, inherits directly or indirectly from java.lang.Object.

87. Do interfaces inherit from java.lang.Object?

a) Yes, interfaces extend Object
b) No, interfaces do not extend Object, but they implicitly declare Object's public methods
c) Yes, they inherit Object's private methods too
d) No, they share no relationship with Object
Correct Answer: b) No, interfaces do not extend Object, but they implicitly declare Object's public methods
Explanation:
Interfaces do not extend java.lang.Object (since they are not classes), but they implicitly declare all public methods of Object (like equals, hashCode).

88. Can an abstract class have a generic type parameter?

a) Yes, for example abstract class MyClass
b) No, generics are only for concrete classes
c) No, generics are only for interfaces
d) Yes, but only a single parameter
Correct Answer: a) Yes, for example abstract class MyClass
Explanation:
Abstract classes support generics exactly like concrete classes and interfaces.

89. Can an interface be generic?

a) Yes, for example interface MyInterface
b) No, interfaces do not support generics
c) Only functional interfaces can be generic
d) Only marker interfaces can be generic
Correct Answer: a) Yes, for example interface MyInterface
Explanation:
Interfaces fully support generics.

90. What happens if a generic interface is implemented without specifying the type parameter?

a) It uses java.lang.Object as the type (raw type)
b) It causes a compilation error
c) It uses java.lang.String by default
d) The implementing class must be declared generic
Correct Answer: a) It uses java.lang.Object as the type (raw type)
Explanation:
If the type parameter is omitted, it becomes a raw type, and the generic type resolves to Object, triggering a compiler warning.

91. In a UML diagram, how is an abstract class typically denoted?

a) With a dashed line
b) With the class name in italics
c) With the keyword <>
d) With a double border
Correct Answer: b) With the class name in italics
Explanation:
In standard UML notation, abstract classes and abstract methods are denoted by writing their names in italics.

92. In a UML diagram, how is an interface typically denoted?

a) Class name in italics
b) With the stereotype <>
c) With a dashed inheritance arrow
d) Both b and c
Correct Answer: d) Both b and c
Explanation:
Interfaces are marked with the stereotype, and implementation is shown with a dashed line with an open arrow.

93. Which pattern provides an abstract interface for creating an object, but allows subclasses to alter the type of objects that will be created?

a) Factory Method
b) Singleton
c) Observer
d) Decorator
Correct Answer: a) Factory Method
Explanation:
The Factory Method pattern relies heavily on abstract classes or interfaces to let subclasses decide which class to instantiate.

94. What is an inner interface in Java?

a) An interface declared inside a class or another interface
b) An interface that has no public methods
c) An interface used strictly for multi-threading
d) A functional interface inside a lambda
Correct Answer: a) An interface declared inside a class or another interface
Explanation:
Nested interfaces (inner interfaces) are declared inside another class or interface and are implicitly static.

95. Are you required to override equals() in an abstract class?

a) Yes
b) No
c) Only if it implements Comparable
d) Only if it is a generic class
Correct Answer: b) No
Explanation:
Overriding equals() is entirely optional. If not overridden, it inherits the implementation from java.lang.Object.

96. Can you declare a field in an interface without initializing it?

a) Yes, it takes the default value
b) No, interface variables are final and must be initialized upon declaration
c) Yes, but only if it is abstract
d) Yes, but it must be initialized in the static block
Correct Answer: b) No, interface variables are final and must be initialized upon declaration
Explanation:
Because all interface variables are implicitly public static final, they must be assigned a value immediately when declared.

97. If you want to use the Template Method design pattern, which structure is necessary?

a) A Marker Interface
b) An Abstract Class
c) A Functional Interface
d) A Java 8 Interface with only static methods
Correct Answer: b) An Abstract Class
Explanation:
The Template Method pattern relies on an abstract class where a concrete method (the template) calls abstract methods that subclasses implement.

98. Which of these statements perfectly describes a pure interface?

a) It defines what to do but not how to do it
b) It defines how to do it but not what to do
c) It defines both what and how
d) It provides state management for subclasses
Correct Answer: a) It defines what to do but not how to do it
Explanation:
A pure interface establishes a contract (the what) leaving the implementation (the how) entirely to the implementing classes.

99. Can an abstract class extend a functional interface?

a) Yes, using the extends keyword
b) No, a class cannot extend an interface, it must implement it
c) Yes, but it must override the single abstract method
d) No, functional interfaces are only for lambda expressions
Correct Answer: b) No, a class cannot extend an interface, it must implement it
Explanation:
Classes (abstract or concrete) always implement interfaces. They do not extend them.

100. If an abstract class implements a functional interface, can it be used as a lambda expression target?

a) Yes
b) No
c) Only if the class is static
d) Only if the lambda explicitly casts it
Correct Answer: b) No
Explanation:
Lambda expressions evaluate to instances of functional interfaces, not abstract classes, even if the abstract class has only one abstract method.

101. Can an interface have a private static method?

a) No, never
b) Yes, starting in Java 9
c) Yes, starting in Java 8
d) Only if it is a functional interface
Correct Answer: b) Yes, starting in Java 9
Explanation:
Java 9 added support for both private instance methods and private static methods in interfaces.

102. Which of these is true about the synchronized keyword in interfaces?

a) Abstract methods can be marked synchronized
b) Default methods can be marked synchronized
c) Neither abstract nor default methods can be marked synchronized
d) Only static methods can be marked synchronized in interfaces
Correct Answer: c) Neither abstract nor default methods can be marked synchronized
Explanation:
The synchronized keyword relates to implementation details (locks). Interface methods (abstract, default, or static) cannot be declared as synchronized.

103. Can an abstract class define synchronized methods?

a) Yes, but only concrete methods can be synchronized
b) Yes, both abstract and concrete methods can be synchronized
c) No, synchronized is not allowed in abstract classes
d) Yes, but it forces subclasses to also synchronize
Correct Answer: a) Yes, but only concrete methods can be synchronized
Explanation:
Concrete methods in an abstract class can be synchronized. Abstract methods cannot be synchronized because synchronization is an implementation detail.

104. What is the result of casting a class instance to an interface type it does not implement at runtime?

a) Compilation Error
b) ClassCastException
c) NullPointerException
d) The cast succeeds but returns null
Correct Answer: b) ClassCastException
Explanation:
While the compiler might allow the cast (since a subclass could potentially implement it), at runtime, it will fail and throw a ClassCastException.

105. How do you define a constant in an interface?

a) final int MAX = 10;
b) static int MAX = 10;
c) int MAX = 10;
d) All of the above
Correct Answer: d) All of the above
Explanation:
Any field declared in an interface is implicitly public static final, so all those declarations mean the exact same thing.

106. Does a class implementing an interface inherit its static methods?

a) Yes, they can be called via the class instance
b) Yes, they can be called via the class name
c) No, interface static methods are not inherited
d) Only if the class overrides them
Correct Answer: c) No, interface static methods are not inherited
Explanation:
Unlike class static methods, interface static methods are not inherited. They can only be accessed using the Interface name directly.

107. If you declare public abstract class Vehicle {}, what is the result?

a) Compilation error because there are no abstract methods
b) Compilation error because it lacks a constructor
c) It compiles successfully
d) It compiles but throws a warning
Correct Answer: c) It compiles successfully
Explanation:
It is perfectly legal to have an abstract class without any abstract methods.

108. Can an abstract class hide (shadow) variables of its superclass?

a) Yes
b) No, variables cannot be shadowed in Java
c) No, abstract classes cannot inherit variables
d) Only static variables can be shadowed
Correct Answer: a) Yes
Explanation:
Abstract classes follow the same variable shadowing rules as normal classes. If it declares a variable with the same name, it shadows the superclass variable.

109. Which class loader loads the standard Java interfaces like java.util.List?

a) Application ClassLoader
b) Extension ClassLoader
c) Bootstrap ClassLoader
d) System ClassLoader
Correct Answer: c) Bootstrap ClassLoader
Explanation:
Standard core Java API classes and interfaces are loaded by the Bootstrap ClassLoader.

110. Which pattern is heavily used when returning an interface from a method without exposing the underlying concrete class?

a) Singleton Pattern
b) Factory Pattern
c) Observer Pattern
d) Builder Pattern
Correct Answer: b) Factory Pattern
Explanation:
The Factory Pattern encapsulates the creation logic and returns an interface or abstract class type, hiding the concrete implementation from the client.

111. Is java.lang.Thread an interface or an abstract class?

a) Interface
b) Abstract class
c) Concrete class
d) Marker interface
Correct Answer: c) Concrete class
Explanation:
Thread is a concrete class. However, it implements the functional interface Runnable.

112. If an interface declares void doSomething() throws IOException;, what must the implementing class do?

a) It must throw IOException
b) It must throw Exception
c) It can throw IOException, any subclass of IOException, or no exception at all
d) It must wrap it in a RuntimeException
Correct Answer: c) It can throw IOException, any subclass of IOException, or no exception at all
Explanation:
An overriding method can choose not to throw the checked exception, or it can throw the same exception or a subclass of it. It cannot throw a broader exception.

113. Can an interface be instantiated using reflection via Class.forName?

a) Yes, it creates a proxy object automatically
b) No, it throws an InstantiationException
c) Yes, but only if it is a marker interface
d) No, it throws a ClassNotFoundException
Correct Answer: b) No, it throws an InstantiationException
Explanation:
Reflection cannot bypass the rule that interfaces and abstract classes cannot be instantiated. It throws InstantiationException.

114. What is a Constant Interface pattern?

a) A highly recommended standard in modern Java
b) A pattern considered an anti-pattern in Java
c) The only way to share constants across packages
d) A requirement for enterprise applications
Correct Answer: b) A pattern considered an anti-pattern in Java
Explanation:
The Constant Interface pattern is considered an anti-pattern because it pollutes the implementing class's public API with implementation details. Static import is the preferred approach.

115. Can a class implement two interfaces that define a static method with the exact same signature?

a) No, it causes a compilation error
b) Yes, there is no conflict because interface static methods are not inherited
c) Yes, but the class must override the static method
d) No, unless one interface extends the other
Correct Answer: b) Yes, there is no conflict because interface static methods are not inherited
Explanation:
Since static methods in interfaces must be invoked using the interface name, there is no ambiguity or conflict for the implementing class.

116. If you serialize a class, what happens to variables defined in the interface it implements?

a) They are serialized along with the class state
b) They throw a NotSerializableException
c) They are not serialized because they are static
d) They are serialized only if the interface extends Serializable
Correct Answer: c) They are not serialized because they are static
Explanation:
Static fields belong to the class/interface, not the object instance. Serialization only saves instance state, so static interface variables are never serialized.

117. In Java, what does programming to an interface primarily achieve?

a) Faster execution speed
b) Reduced memory consumption
c) Loose coupling and easier testability
d) Avoiding garbage collection
Correct Answer: c) Loose coupling and easier testability
Explanation:
By depending on interfaces rather than concrete classes, you can swap implementations easily, facilitating modularity and unit testing.

118. Which statement is true regarding the this reference inside an interface default method?

a) It refers to the interface class object
b) It refers to the instance of the class implementing the interface
c) It is illegal to use this in a default method
d) It evaluates to null
Correct Answer: b) It refers to the instance of the class implementing the interface
Explanation:
In a default method, this refers to the current object instance of the concrete class that implements the interface.

119. Can an abstract class define a strictfp method?

a) Yes, for both abstract and concrete methods
b) Yes, but only for concrete methods
c) No, strictfp cannot be used in abstract classes
d) Only if the class itself is strictfp
Correct Answer: b) Yes, but only for concrete methods
Explanation:
The strictfp modifier ensures consistent floating-point calculations. Since abstract methods have no body, they cannot be marked strictfp. Concrete methods can.

120. Can an interface be declared as strictfp?

a) Yes, and it makes all default methods strictfp
b) No, interfaces cannot be strictfp
c) Yes, but only prior to Java 8
d) Only functional interfaces can be strictfp
Correct Answer: a) Yes, and it makes all default methods strictfp
Explanation:
An interface can be declared strictfp, which applies the strict floating-point rules to all concrete (default and static) methods within it.
← Previous: Java Inheritance and Polymorphism MCQs
Next →: Java Interview Questions 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
C++ Memory Management MCQs

C++ Memory Management MCQs

Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…

By MCQs Generator
Java Inheritance and Polymorphism MCQs

Java Inheritance and Polymorphism MCQs

Java inheritance and polymorphism form the bedrock of Object-Oriented Programming (OOP), enabling code reuse and flexible runtime behavior. Inheritance allows…

By MCQs Generator