MCQ Java Interfaces


Q) Can we declare an interface as final?

  1. YES
  2. NO

Answer: 2
Explanation:
No, we cannot declare an interface as final. In Java final keyword is used to stop extension or inheritance by sub classes. But interface is meant to be used in inheritance. Hence, we can’t, declare an interface as final because if we declare final no use of that Interface. That’s why final is illegal for Interfaces.


Q) Which of these access specifiers can be used for an interface?

  1. Public
  2. Protected
  3. private
  4. Any of the above

Answer: 1
Explanation: Interface in java can have only public modifier. If you don’t declare public before data or methods explicitly then, methods will be available to the same package only in which is declared. If you write public manually, then it will be accessible from anywhere in the project or program.


Q) What is wrong with below source code?

interface IShape {
	void f1();

	void f2();

	void f3();
}

class Circle implements IShape {
	
	public void f1() {
	}
}

  1. Compile time error
  2. Run time error
  3. Source code is OK

Answer: 1
There is compile time error as Circle class has not implemented all methods of the IShape interface.


Q) The fields in an interface are implicitly specified as

  1. Static
  2. Protected
  3. Private
  4. Static and final

Answer: 4
Fields in an interface are by default, static and final.


Q) What is output of the below java code?

interface X
{
    int i = 5;
}
 
class Y implements X 
{
    void f()
    {
    	i = 10;
    	System.out.println("i="+i);
        
    }
}
public class Main {

	public static void main(String[] args) {
		
		Y obj = new Y();
		obj.f();	
	}
}

  1. 0
  2. 5
  3. 10
  4. Compiler error

Answer: 4
Explanation:
Fields in interface are by default static and final and we cannot change their value once they are initialized. In the above code, the value of interface field i is being modified in method f() that is not allowed. Hence, the compiler error.


MCQ Java Interfaces

Scroll to top