Java Interfaces Quiz - MCQ - Multiple Choice Questions

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?

a) A class
b) A data type
c) A blueprint for a class
d) A data structure

Answer:

c) A blueprint for a class

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?

a) extends
b) new
c) interface
d) implements

Answer:

d) implements

Explanation:

The implements keyword is used by classes to implement an interface.

3. Can an interface extend another interface in Java?

a) No
b) Yes

Answer:

b) Yes

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?

a) Yes
b) No

Answer:

b) No

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?

a) private
b) protected
c) public
d) default

Answer:

c) public

Explanation:

Variables in an interface are implicitly public, static, and final.

6. Is it possible to create an instance of an interface?

a) Yes
b) No

Answer:

b) No

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?

a) None
b) Only one
c) Two
d) As many as needed

Answer:

d) As many as needed

Explanation:

A Java class can implement any number of interfaces.

8. Can an interface inherit from a class?

a) Yes
b) No

Answer:

b) No

Explanation:

An interface cannot inherit from a class. It can only extend other interfaces.

9. Can an interface method be declared as final?

a) Yes
b) No

Answer:

b) No

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?

a) Static
b) Private
c) Final
d) Protected

Answer:

b) Private

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?

a) Abstract Interface
b) Marker Interface
c) Empty Interface
d) Functional Interface

Answer:

b) Marker Interface

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?

a) static
b) default
c) final
d) abstract

Answer:

b) default

Explanation:

The "default" keyword is used to define a default method in an interface.

13. Are all methods in an interface abstract?

a) Yes
b) No

Answer:

b) No

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?

a) Abstract methods
b) Constants
c) Default and static methods
d) All of the above

Answer:

d) All of the above

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?

a) Abstract classes
b) Static methods
c) Interfaces
d) Enums

Answer:

c) Interfaces

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?

a) private
b) protected
c) public
d) None of the above

Answer:

c) public.

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?

a) To provide multiple inheritance
b) To add utility functions
c) To provide backward compatibility with older interface versions
d) For better performance

Answer:

c) To provide backward compatibility with older interface versions.

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?

a) Java 5
b) Java 7
c) Java 8
d) Java 9

Answer:

c) Java 8

Explanation:

From Java 8 onwards, interfaces can have default and static method implementations.


Comments