Interfaces QuizS2C Home « Interfaces Quiz

OO Concepts Quiz 10

The quiz below tests your knowledge of the material learnt in OO Concepts - Lesson 10 - Interfaces.

Question 1 : How do we use an interface in our classes?
- We use an interface in our classes by implementing it with the <code>implements</code> keyword in the class definition.
Question 2 : Is the following code snippet valid?

public class B implements C extends A { }
- This code snippet is invalid as the <code>implements</code> keyword must follow the <code>extends</code> keyword when used together.
Question 3 : Two modifiers are implicitly applied to implementable methods within an interface, what are they?
- The <code>public</code> and <code>abstract</code> modifiers are implicitly applied to implementable methods within an interface.
Question 4 : What is the main benefit of interfaces?
- Interfaces allow us to use method arguments and return types polymorphically.
Question 5 : Assuming C, D and E are interfaces is the following code snippet valid?

public class B extends A implements C, D, E { }
- This snippet is perfectly valid as we can implement any number of interfaces.
Question 6 : Assuming the following are in different files is the following code snippet valid?

public interface A { void b(int b); }
public class B implements A { void b(int b) {} }
- Class <code>B</code> won't compile as the <code>b()</code> method is less restrictive. <br>. The complier implicitly applies the <code>public</code> and <code>abstract</code> modifiers to implementable interface methods and <i>override</i> methods can't be less restrictive.
Question 7 : What would be the output of the following code snippet?

public interface A { void b(); }
public class B implements A { public void b() {} }
public class C extends B {
public static void main(String[] args) {
B b = new B();
C c = new C();
if (b instanceof A) { System.out.print("true,"); }
if (c instanceof B) { System.out.print("true,"); }
if (c instanceof A) { System.out.print("true"); }
}
}
- The output will be 'true,true,true' as class <code>C</code> inherits the implementation of interface <code>A</code> through class <code>B</code>
Question 8 : What must we ensure when implementing an interface?
- We must honour the interface contract by ensuring we <i>override</i> all implementable methods in the implementation class or first <i>concrete subclass</i> thereof.
Quiz Progress Bar Please select an answer


What's Next?

In the next quiz we test your knowledge of interface default methods which were introduced in Java8.