Java Interfaces MCQ - Multiple Choice Questions And Answers
Q. What is an interface in Java?
A. A class that can be instantiatedB. A blueprint of a class
C. A type of loop
D. An array in Java
Q. Can a Java interface have variables?
A. Yes, with a default valueB. No, interfaces cannot have variables
C. Yes, with a final keyword
D. Yes, with a static keyword
Q. Can a Java class implement multiple interfaces?
A. No, Java classes can only implement one interfaceB. Yes, Java classes can implement multiple interfaces
C. Only if the interfaces have the same method signature
D. Only if the interfaces are marked as "extendable"
Q. Which keyword is used to implement an interface in a Java class?
A. useB. extends
C. implements
D. interface
Q. Can methods in an interface have a method body?
A. Yes, all methods in an interface must have a method bodyB. No, methods in an interface cannot have a method body
C. Only static methods can have a method body in an interface
D. Only default methods can have a method body in an interface
Q. Which of the following is a correct statement about interfaces in Java?
A. Interfaces can be instantiated directlyB. Interfaces can have constructors
C. Interfaces can extend multiple classes
D. Interfaces can have abstract methods
Q. What is the purpose of default methods in Java interfaces?
A. To provide default implementation for methodsB. To force classes to override the method
C. To prevent inheritance from interfaces
D. To make methods private
Q. In Java, can an interface extend another interface?
A. Yes, an interface can extend another interfaceB. No, an interface cannot extend another interface
C. Only if the interfaces have the same method signature
D. Only if the interfaces are marked as "extendable"
Q. What is the purpose of the Predicate functional interface in Java 8?
A. To produce resultsB. To consume values
C. To filter elements based on a condition
D. To transform values
Q. In Java, can an interface have private methods?
A. Yes, interfaces can have private methodsB. No, interfaces cannot have private methods
C. Only if the interface is marked as "private"
D. Only if the private methods are static
Q. Which of the following is NOT a valid type of inheritance in Java?
A. Single InheritanceB. Multiple Inheritance
C. Hierarchical Inheritance
D. Multi-level Inheritance
Q. Which keyword is used to prevent method overriding in a Java class?
A. finalB. super
C. override
D. prevent
Q. What is the output of the following Java code snippet?
interface MyInterface {A. Error: Cannot invoke lambda expression
void myMethod();
}
public class MyClass implements MyInterface {
public static void main(String[] args) {
MyInterface obj = () -> System.out.println("Hello");
obj.myMethod();
}}
B. Hello
C. Compilation error
D. Null