In this blog post, we present multiple-choice questions to test your knowledge of Java interfaces (including Java 8 static and default methods in an interface). Interfaces in Java provide a powerful mechanism for achieving abstraction and multiple inheritance. As of Java 8, they've become even more powerful, allowing for both static and default methods.
What are Interfaces?
In Java, an interface is a reference type, similar to a class, which can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain constructors (as it can’t be instantiated on its own).
Key Points:
Abstraction: Interfaces are used to achieve full abstraction, ensuring that all the methods in an interface are abstract (without implementation).
Multiple Inheritance: Java doesn't support multiple inheritances in classes due to the "diamond problem". However, a single class can implement multiple interfaces, offering a solution to this limitation.
Default Methods: Introduced in Java 8, default methods have a method body and provide a default implementation. They can be overridden by the implementing class.
Static Methods: Introduced in Java 8. The static methods belong to the interface itself (we should call static methods using the interface name as they belong to the interface), rather than to instances of implementing classes.
Java Interfaces MCQ (Multiple Choice Questions)
1. What is the primary purpose of interfaces in Java?
Answer:
Explanation:
Interfaces are used to achieve full abstraction in Java, ensuring that a class adheres to a certain contract without enforcing how that contract is fulfilled.
2. Which keyword is used to implement an interface in a class?
Answer:
Explanation:
The implements keyword is used when a class wants to adhere to the contract defined by an interface.
3. What is the default access modifier of a method in an interface in Java?
Answer:
Explanation:
Interface methods are public by default since the idea is for them to be implemented by other classes.
4. How many interfaces can a single class implement?
Answer:
Explanation:
A class can implement any number of interfaces in Java.
5. Can an interface contain a constructor?
Answer:
Explanation:
Interfaces cannot be instantiated directly, so they cannot have constructors.
6. What was the significant change to interfaces in Java 8?
Answer:
Explanation:
Java 8 allowed interfaces to have method bodies in the form of static and default methods.
7. Why were default methods introduced in Java 8 interfaces?
Answer:
Explanation:
Default methods allow developers to add new methods to interfaces with an implementation without affecting classes that already use this interface.
8. Which of these is not allowed in an interface?
Answer:
Explanation:
Interfaces can have static final variables (constants) but not instance variables.
9. How do you access a static method of an interface?
Answer:
Explanation:
Static methods in an interface are accessed using the interface name, similar to static methods in classes.
10. What happens if a class implements two interfaces with default methods of the same name?
Answer:
Explanation:
If a class implements two interfaces with default methods of the same name, it has to override the method to resolve the conflict.
11. Static methods in interfaces...
Answer:
Explanation:
Static methods belong to the interface itself, not to the implementing class. Hence, they cannot be overridden.
12. The "diamond problem" in programming refers to...
Answer:
Explanation:
The diamond problem occurs when a class inherits from two classes that have a method with the same name, leading to ambiguity. Java avoids this problem by not supporting multiple inheritance of classes.
13. Which of the following is a benefit of using interfaces?
Answer:
Explanation:
Interfaces set a clear contract that classes must follow if they implement the interface, ensuring that certain methods are implemented in the class.
Comments
Post a Comment
Leave Comment