MCQ Java Interfaces


Q) Which is true statements about an interface in java?

  1. An interface can extend another interface
  2. We can create object of an interface in java
  3. An interface can have constructor
  4. None

Answer: 1
An interface can extend another interface or multiple interfaces.


Q) What is output of below java program?

interface X
{   
	void f();
}
interface Y extends X
{
	void g();
}
 
class C implements Y 
{	
	public void f() {	
		System.out.println("Hello");
	}	
	public void g() {		
		System.out.println("World");
	}   
}

public class Main {

	public static void main(String[] args) {
		
		C o = new C();
		o.f();
		o.g();
	}
}

Output:
A)
Hello
World

B) Hello

C) World

D Compiler error

Answer: A
Confusion point: Can an interface extends another interface? Answer is yes, and just a class can implement first interface. The extended interface methods will be available.


Q) Which of the following contains only unimplemented methods?

  1. Class
  2. Abstract class
  3. Interface
  4. None

Answer: 3
Java interfaces only contains unimplemented abstract methods. These all methods are implemented by the class who implements the interface.


Q) A class inherits an interface using which keyword?

  1. Extends
  2. Implements
  3. Inherit
  4. None

Answer: 2
A class inherits an interface using implements keyword. For example,
class Y implements X, where X is an interface.


Related Posts