Welcome to the Java Interfaces Quiz! Interfaces play a significant role in creating robust and scalable Java applications. If you are just starting with Java or revisiting interfaces, this quiz is perfect for you. Dive in to test your foundational knowledge!
1. What is an interface in Java?
Answer:
Explanation:
An interface in Java is a blueprint that can be used to implement classes. It can have methods and variables, but the methods are abstract by default.
2. Which keyword is used to implement an interface?
Answer:
Explanation:
The implements keyword is used by classes to implement an interface.
3. Can an interface extend another interface in Java?
Answer:
Explanation:
Interfaces can extend other interfaces in Java, allowing an interface to inherit abstract methods from another interface.
4. Can an interface have a constructor?
Answer:
Explanation:
Interfaces cannot have constructors because they cannot be instantiated.
5. Which of the following access modifiers are implicitly applied to variables in an interface?
Answer:
Explanation:
Variables in an interface are implicitly public, static, and final.
6. Is it possible to create an instance of an interface?
Answer:
Explanation:
We cannot instantiate an interface directly. However, we can create reference variables of an interface type.
7. How many interfaces can a Java class implement?
Answer:
Explanation:
A Java class can implement any number of interfaces.
8. Can an interface inherit from a class?
Answer:
Explanation:
An interface cannot inherit from a class. It can only extend other interfaces.
9. Can an interface method be declared as final?
Answer:
Explanation:
Methods in an interface are implicitly abstract, and abstract methods cannot be final.
10. In Java 9, which type of methods can be added to interfaces to share code between methods?
Answer:
Explanation:
Starting from Java 9, interfaces can have private methods, which can help in sharing code between methods without exposing them to external classes.
11. An interface with no methods is known as?
Answer:
Explanation:
An interface with no defined methods is known as a marker interface. It is used to mark classes that support certain capabilities.
12. Which keyword is used to define a default method in an interface?
Answer:
Explanation:
The "default" keyword is used to define a default method in an interface.
13. Are all methods in an interface abstract?
Answer:
Explanation:
Prior to Java 8, all methods in an interface were implicitly abstract. However, with Java 8 and onwards, interfaces can have default and static methods.
14. Which of these can be contained in an interface?
Answer:
Explanation:
An interface can contain abstract methods, constants (public, static, final variables), static methods, and default methods.
15. Which Java feature helps achieve multiple inheritance?
Answer:
Explanation:
In Java, multiple inheritance is achieved through interfaces. A class can implement multiple interfaces, thereby inheriting the abstract methods of all the interfaces.
16. 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.
17. 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.
18. Starting from which Java version can an interface contain method implementations?
Answer:
Explanation:
From Java 8 onwards, interfaces can have default and static method implementations.
❮ Previous Quiz Next Quiz ❯
Comments
Post a Comment
Leave Comment