OO Concepts Quiz 14
The quiz below tests your knowledge of the material learnt in OO Concepts - Lesson 14 - Lambda Expressions.
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 41 : 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 40 : Two modifiers are implicitly applied to methods within an interface, what are they?
- The <code>public</code> and <code>abstract</code> modifiers are implicitly applied to methods within an interface.
Question 42 : What is the main benefit of interfaces?
- Interfaces allow us to use method arguments and return types polymorphically.
Question 43 : 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 44 : 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 interface methods and <i>overrides</i> methods can't be less restrictive.
Question 45 : 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 46 : What must we ensure when implementing an interface?
- We must honour the interface contract by ensuring we <i>override</i> all methods in the implementation class or first <i>concrete subclass</em> thereof.
Question 7 : What is output from the following code snippet?
package info.java8;
public class S {
int i = 1;
public static void main(String[] args) {
S s = new S(1);
}
S() {
System.out.print("i = " + 1);
}
S(int i) {
super();
this();
System.out.print("i = " + 1);
}
}
- The code won't compile as we cannot use <code>super()</code> and <code>this()</code> together.
Quiz Progress Bar Please select an answer
What's Next?
In the next quiz we test your knowledge of method references, which were introduced in Java8, their syntax and how to use them.