Java Interface MCQ Questions and Answers

0

 Java Interface Interview MCQ Questions and Answers



Java Interface  MCQ Questions and AnswersJava Interface  MCQ Questions and Answers
Java Interface  MCQ






Text Example

1) An interface in Java is like a 100% ____.

A) abstract class

B) public class

C) inner class

D) anonymous class

Answer [=]  A

Explanation:  Yes. If 100% of methods in an abstract class are marked abstract, then it is comparable to an interface in Java.



2) A Java Interface is not considered a class. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]  A

Explanation: True. A class and an Interface have different inheritance rules.



3) Choose the correct syntax below for defining an Interface in Java.

A)

interface NAME

{

//abstract methods

}

B)

abstract interface NAME

{

//abstract methods

}

C)

public interface NAME

{

//abstract methods

}

D) All the above

Answer [=]  D

Explanation:  There is no need to explicitly mention ABSTRACT keyword to define an interface.




4) Choose a correct statement about Java Interfaces?

A) Interface contains only abstract methods by default.

B) A Java class can implement multiple interfaces

C) An Interface can extend or inherit another Interface.

D) All the above

Answer [=]   D



5) A Java Class inherits Constants and Methods of an Interface using ____ keyword.

A) INTERFACE

B) IMPLEMENTS

C) EXTENDS

D) All the above

Answer [=]  B

Explanation:  IMPLEMENTS keyword



6) What is the output of the below Java program with an Interface?

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();

}

}

A) No output

B) Implemented move() method.

C) Compiler error

D) None of the above

Answer [=]  B



7) What is the output of the below Java program with an Interface?

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();

}

}

A) 1000

B) 2000

C) Compiler error

D) None of the above

Answer [=]  C

Explanation:  Java Interface treats its variables like constants. So, the classes implementing Interfaces, can not reassign values to the variables.





8) What is the output of the below Java program with an Interface?

interface Book

{

char type='C';

}

public class InterfaceTest3

{

public static void main(String[] args)

{

System.out.println(new Book().type);

}

}

A) C

B) No output

C) Compiler error

D) None of the above

Answer [=]  C

Explanation:   You can not instantiate an Interface in Java. So, using the keyword "new" does not create new objects of an Interface.



9) All Interface variables are ___ by default in Java.

A) public

B) final

C) public and final

D) None

Answer [=]

C

Explanation:

Yes, public and final. In other words, these are constants.

10) All Interface methods in Java are ____ by default.

A) public

B) abstract

C) public and abstract

D) None of the above

Answer [=]  C

Explanation:  Interface automatically marks all its methods as public and abstract. So, you need not add these keywords again while writing the program.



11) A Class implementing an Interface can use ____ access modifier before the implemented methods.

A) private

B) protected

C) public

D) All the above



Answer [=]  C

Explanation:  Only a "public" access modifier is allowed.



12) A Java Class implementing an Interface can define a variable with the same name as that of the Interface constant. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -



Answer [=]  A


13) What is the output of the below Java program with an Interface?

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();

}

}

A) Teeth: 4

B) Teeth: 5

C) Compiler error as teeth is a constant in Worm interface.

D) None of the above

Answer [=]  B

Explanation:  You can reassign an interface's constant. You can define a variable with the same name in the implementing class.



14) A Java Interface can not declare constructors. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]  A

Explanation:  True. You can define a constructor inside an Interface.



15) What is the output of the below Java program with an Interface?

interface Floor

{

Floor(){ }

}

public class InterfaceTest5

{

public static void main(String[] args)

{

System.out.print("Floor");

}

}

A) Floor

B) No output

C) Compiler error

D) None

Answer [=]  C

Explanation:  Interfaces can not have constructors.



16) Java 8 (Java 1.8) introduced the ___ feature.

A) Default methods

B) Static methods

C) Default and Static methods

D) None of the above

Answer [=]  C

Explanation:  Yes, static and default methods. Remember that "default" is a keyword.



17) Java Interface static methods have ___ compatibility with the existing project code.

A) Forward

B) Backward

C) Both Forward and Backward

D) -

Answer [=]  C

Explanation:  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.



18) Java Interface DEFAULT methods have ___ compatibility with the existing project code.

A) Forward

B) Backward

C) Backward and Forward

D) -

Answer [=]  C

Explanation:  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.




19) The DEFAULT methods of an Interface are suitable mostly for ___ type of projects.

A) Open Source (Public Repositories)

B) Closed Source (Private Repositories)

C) -

D) -

Answer [=]   A

Explanation:  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.




20) Is it possible to remove the keyword DEFAULT and make the method abstract again in an Interface, if the Interface belongs to a Closed-Source project?

A) Yes

B) No

C) -

D) -

Answer [=]  A

Explanation:  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.



21) The annotation used in Java to override the method of a super-class or interface by the subclass or implementing class is ___.

A) @override

B) @Override

C) @super

D) @subclass

Answer [=]  B

Explanation:  @Override




22) It is ___ to override the static method of an Interface in Java.

A) possible

B) not possible

C) -

D) -

Answer [=]  B

Explanation:  Not possible to override the static method. The compiler shows an error.



23) A Java static method can not be ___.

A) private or protected

B) final

C) abstract

D) All the above

Answer [=]  D

Explanation:  You can not use the keywords, private, protected, final and abstract, before a static method of an Interface.



24) Which is the missing java code in the class implementing an Interface below?

interface Linein

{ void addInput(); }

interface Lineout

{ void addOutput(); }

class Speaker implements Linein, Lineout

{

//MISSING CODE

}

A)

class Speaker implements Linein, Lineout

{

@Override

public void addOutput() { }

@Override

public void addInput() { }

}

B)

class Speaker implements Linein, Lineout

{

@Override

public void addOutput() { }

}

C)

class Speaker implements Linein, Lineout

{

@Override

public void addInput() { }

}

D) All the above

Answer [=]  A

Explanation:  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.



25) Which is the missing code to successfully compile the below Java program with abstract classes and Interfaces?

interface A

{ void a(); }

abstract class B implements A

{ abstract void b(); }

class C extends B

{

//Missing methods

}

A)

@Override

public void a() { }

@Override

void b() {}

B)

@Override

public void a() { }

C)

@Override

void b() {}

D) All the above

Answer [=]   A

Explanation:  The first concrete class should implement all the abstract methods of superclasses and interfaces.




26) A Superinterface is comparable to a Superclass. State TRUE or FALSE.

A) TRUE

B) FALSE

C) -

D) -

Answer [=]   A



27) A Static method of an Interface should be accessed with _____ and a DOT operator.

A) Class Name

B) Interface Name

C) An object of a concrete class

D) None of the above

Answer [=]   B

Explanation:  You should not use object references to access the static method of an Interface. Just use Interface name and DOT (.) operator directly.




interface possibly one of the most boring words in the human language if you didn't want to jump out a window right there 


let's call it fun and then your curly braces the reason we're creating an interface outside of like the main method like the main method is usually where we write code since it gets run when we click the green run-button well an interface is kind of like a class where we write the code outside any object your class in Java has attributes and things that can do so to create an outline for that we just need to list out attributes and things that can do a tributo unrealistic something like color and we'll set a default color and there's this water bottle here so let's I guess go with that theme what can water bottle do well we can fill it up fill up and I'll just rename this interface to water bottle since we're doing this on the fly and I call it interface just so we know the difference so now we have this outline we've got a color blue and we can fill it up we could also add some more attributes or more things that can do with methods but here's a simple example if we wanted to make a water bottle of our own but we're a little confused on how to start we could just be like hey I know that there's a water bottle interface there's this outline why don't I just use the outline so we can just do that by saying implements the water bottle interface I think it's a little bigger cool now we get some red underlines if we hover over it it's basically saying that since we said that this class implements the water bottle interface 


            which is just an outline it needs to have the things in the outline so it needs to have a color and it needs to be able to be filled up so we can just hover over it and click Add unimplemented methods and then this generated right below the main method if we save this and sort of add something to the main method so we can see what's going on and we print out color and we also create the example next step fill up and we implement it here then we run it they receive blue and it is filled now I know that's a lot I kind of scrambled to finish things in the end but let's go over absolutely everything so you know exactly what's going on if we look at this file we see that we have an interface and we have a class an interface is an outline for a class so this interface has a color it's set to blue and can be filled up next 



                we go to the class and it turned out that we wanted to create a water bottle but we didn't know how we would just wanted to use an outline that someone made before so we can say implements the water bottle interface use all the methods and attributes inside of the interface as our own we have a main method here which doesn't really matter it just helps us run code and we found that we had to add the fill-up method because it's in the interface everything in the interface has to be in the class so if we added another method here say I pour out or something then we get red underlines because we need to add that extra method so add unimplemented methods and then we see pour out because everything inside the interface has to be here there's this override tag that just means implements 



                    whatever's up here it just helps Java and you keep track of what you are implementing from the interface so in the main method when we print color since this implements water bottle interface it knows all of this and so it knows the color blue and that's why you see the color blue print out here next we create a class of itself X then we can do X dot fill up which goes inside of here interfaces are helpful because it lets you keep track of what you need but a major drawback of interfaces is that you have to have every method from the interface so it can get kind of cumbersome I hope 


Post a Comment

0 Comments
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !