Java MCQ – Interfaces – Part 2
This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Interfaces”.
1. What happens when a constructor is defined for an interface?
A Compilation failure
B An exception is thrown
C The interface compiles successfully
D The class implementing the interface will throw an exception
2. What happen if we provide the implementation of a method in an interface?
A The class implementing this method does not need to provide the implementation for this method.
B An exception is thrown
C Compilation failure
D None of the above
3. What happens when we access the same variable defined in two interfaces implemented by the same class?
A An exception is thrown
B Compilation failure
C JVM is unable to identify the correct variable
D interfaceName.variableName
must be defined
4. What’s wrong with the following code?
interface Vehicle { void start(); void run(); void stop(); } class Car implements Vehicle { public void start() { } }
A Compilation error
B Runtime error
C An exception is thrown
D Source code is correct
5. What is the output of the following code?
interface A { int var = 2; } class B implements A { void show() { var = 3; System.out.println("var = "+var); } } public class Main { public static void main(String[] args) { B obj = new B(); obj.show(); } }
A 2
B 3
C 5
D Compilation error
6. Which statement is correct about interfaces in Java?
A The interface is used to perform multiple inheritance in java.
B We cannot instantiate an interface.
C An interface can inherit from another interface.
D All the answers are true
7. Which statement is wrong about interfaces in Java?
A It is used to achieve abstraction and multiple inheritance in Java.
B It can be instantiated, that means we can create an object from an interface.
C We can only have abstract methods in the interface.
D All the answers are false.
8. What is the output of the following code?
interface A { void show(); } class B implements A { public void show() { System.out.println("Welcome To StackHowTo!"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.show(); } }
A We cannot create the object ‘obj’ from the interface ‘A’.
B Welcome To StackHowTo!
C Compilation error
D All the answers are false.
9. Java interface is used for __________
A Implementing the behavior of multiple inheritance
B Achieving loose coupling
C Achieving abstraction
D All the answers are true
10. Which of the following statements contains only unimplemented methods?
A Class
B Abstract class
C Interface
D None of the above