Study and learn Interview MCQ Questions and Answers on Java Interface. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.
Go through Java Theory Notes on Interface and Java 8 Default and Static Methods in Interfaces before reading these objective questions.
Yes. If 100% of methods in an abstract class are marked abstract, then it is comparable to an interface in Java.
True. A class and an Interface have different inheritance rules.
interface NAME { //abstract methods }
abstract interface NAME { //abstract methods }
public interface NAME { //abstract methods }
There is no need to explicitly mention ABSTRACT keyword to define an interface.
IMPLEMENTS keyword
interface Bus { void move(); } class ElectricBus implements Bus { public void move() { System.out.println("Implemented move() method."); } } public class InterfaceTest1 { public static void main(String[] args) { new ElectricBus().move(); } }
interface Car { int basePrice=1000; } public class InterfaceTest2 implements Car { void changePrice() { basePrice = 2000; System.out.print(basePrice); } public static void main(String[] args) { new InterfaceTest2().changePrice(); } }
Java Interface treats its variables like constants. So, the classes implementing Interfaces, can not reassign values to the variables.
interface Book { char type='C'; } public class InterfaceTest3 { public static void main(String[] args) { System.out.println(new Book().type); } }
You can not instantiate an Interface in Java. So, using the keyword "new" does not create new objects of an Interface.
Yes, public and final. In other words, these are constants.
Interface automatically marks all its methods as public and abstract. So, you need not add these keywords again while writing the program.
Only a "public" access modifier is allowed.
True.
interface Worm { int teeth=2; } class BookWorm implements Worm { int teeth=4; void show() { teeth= 5; System.out.println("Teeth: " + teeth); } } public class InterfaceTest4 { public static void main(String[] args) { new BookWorm().show(); } }
You can reassign an interface's constant. You can define a variable with the same name in the implementing class.
True. You can define a constructor inside an Interface.
interface Floor { Floor(){ } } public class InterfaceTest5 { public static void main(String[] args) { System.out.print("Floor"); } }
Interfaces can not have constructors.
Yes, static and default methods. Remember that "default" is a keyword.
Forward compatibility means, the implementing classes may be modified to access these static methods. It is optional. It does not throw exceptions. The existing classes still do not utilize these new static methods of the interface. So, it is backward compatible too.
Backward and Forward Compatibility. It means, the existing project-code compiles as it is without asking for overriding the newly added Default method inside the Interface. Without the keyword DEFAULT, the project build fails. All the new Classes start implementing these default methods. It is forward compatibility.
Open Source projects do not know how many organizations or users have been dependent on the project. So, it is advised to take advantage of the DEFAULT methods of an Interface to introduce new features.
Yes. Closed source projects can still introduce new features using the same keyword DEFAULT. Once they complete the implementation of all the DEFAULT methods in the implementing classes, they can completely remove default methods and provide only abstract methods. The end-user of a Closed-Source project is the company itself that developed it.
@Override
Not possible to override the static method. The compiler shows an error.
You can not use the keywords, private, protected, final and abstract, before a static method of an Interface.
interface Linein { void addInput(); } interface Lineout { void addOutput(); } class Speaker implements Linein, Lineout { //MISSING CODE }
class Speaker implements Linein, Lineout { @Override public void addOutput() { } @Override public void addInput() { } }
class Speaker implements Linein, Lineout { @Override public void addOutput() { } }
class Speaker implements Linein, Lineout { @Override public void addInput() { } }
As the Speaker class is implementing two interfaces Linein and Lineout, the abstract methods of all the interfaces have to be implemented by the first concrete class.
interface A { void a(); } abstract class B implements A { abstract void b(); } class C extends B { //Missing methods }
@Override public void a() { } @Override void b() {}
@Override public void a() { }
@Override void b() {}
The first concrete class should implement all the abstract methods of superclasses and interfaces.
True.
You should not use object references to access the static method of an Interface. Just use Interface name and DOT (.) operator directly.