MCQ

Java MCQ – Interfaces – Part 1

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Interfaces”.
 

1. What keyword is used to declare an interface in Java?

A class

B interface

C implements

D abstract

B
The keyword “interface” is used to create an interface in a Java program. Example:

interface A {
}

 

 

2. Java interface can contain ____________

A Final variables

B Abstract methods

C Non-abstract methods

D Final variables and abstract methods

D
An interface can have both final variables and abstract methods. Example:

interface interfaceName{
   // Final or static variables
   datatype variableName = value;
   // Abstract method declarations
   returntype methodName([Parameters...]);
}

 

 

3. What is the correct way to implement an interface?

Example, ‘Operation’ interface implements ‘Add’ class.
 
A class Add implements Operation{}

B class Add extends Operation{}

C class Add import Operation{}

D None of the above

A
Classes always implement interfaces. An interface can inherit from another interface or from multiple interfaces.

 

 

4. Which of the following statements applies to methods of an interface in Java?

A An interface can only contain abstract methods.

B You can define a method in an interface

C Private and protected access modifiers can also be used to declare methods in an interface

D None of the above

A, B
An interface can have methods and variables like in a class, but the methods declared in an interface are abstract by default (only the method signature, no body).
Since Java8, we can implement a static method or a default method. So option B is also correct.

 

 

5. Which is the correct declaration to implement two interfaces?

A class A implements B, implements C {}

B class A implements B, C {}

C class A implements B C {}

D None of the above

B

 

 

6. Can we declare an interface as final?

A Yes

B No

B
No, we cannot declare a final interface. In Java, the final keyword is used to stop inheritance by child classes. But the interface is intended to be used in inheritance. Therefore, we cannot declare an interface as final, because if we declare final no use of that interface. This is why the ‘final’ keyword is illegal for interfaces.

 

 

7. All methods must be implemented in an interface?

A Yes

B No

A
Class must implement all methods in an interface.

 

 

8. What does an interface contain?

A Method definition

B Method declaration

C Declaration and definition of the method

D Method name

B
The interface only contains the method declaration.

 

 

9. What kind of methods does an interface contain by default?

A abstract

B static

C final

D private

A
Methods declared in an interface are abstract by default (only the method signature, no body).

 

 

10. What type of variable can be defined in an interface?

A public static

B private final

C public final

D static final

D
A variable defined in an interface is implicitly final and static. They are usually written in capital letters.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *