MCQ

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

A
We can’t define a constructor in an interface because the objects cannot be instantiated.

 

 

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

C
Methods of interfaces are always abstract. They only provide the method definition.

 

 

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

D
Java Virtual Machine (JVM) must know distinctly which variable value to use. To avoid confusion, “interfaceName.variableName” is required.

 

 

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

A
There is a compilation error because the Car class has not implemented all the methods of the Vehicle interface.

 

 

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

D
The variables in the interface are by default static and final and we cannot change their value once they are initialized. In the above code, the value of the variable ‘var’ is changed in show() method which is not allowed. Therefore, the compilation error is displayed.

 

 

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

D

 

 

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.

B
We cannot create an object from an interface.

 

 

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.

B
Don’t get confused with this instruction A obj = new B();. You’re right, we cannot create an object from an interface. But at this declaration, the object is instanciated from B class and not from the interface A. The object ‘obj’ is only a reference of A. So the program is correct and displays “Welcome To StackHowTo!”.

 

 

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

D
Java interface is used for:

  • Implementing the behavior of multiple inheritance
  • Achieving loose coupling
  • Achieving abstraction

 

 

10. Which of the following statements contains only unimplemented methods?

A Class

B Abstract class

C Interface

D None of the above

C
Java interfaces only contain unimplemented abstract methods. All the methods are implemented by the class that implements the interface.

 

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 *