Java OCA OCP Practice Question 2348

Question

What is the output of the following code? (Choose all that apply.).

interface Printable {}                           // line1
class Main {
    static void admit(Printable adm) {
        System.out.println("admission complete");
    }
    public static void main(String args[]) {
        admit(new Printable(){});               // line2
    }
}
a  The class prints admission complete.
b  The class doesn't display any output.
c  The class fails to compile.
d  Main will print  admission  complete if code 
   at (#2) is changed to the following:/* w w w  .ja v a  2s .c om*/

admit(new Printable());

e  Class Main instantiates an anonymous inner class.
f  If Printable is defined as a class, as follows, the result 
   of the preceding code will remain the same:

class Printable {}


a, e, f

Note

Options (b) and (c) are incorrect because the class compiles successfully and prints a value.

Option (d) is incorrect because it tries to instantiate the interface Printable and not an instance of the anonymous inner class that implements Printable.

Option (e) is correct because code at (#2) instantiates an anonymous inner class, which implements the interface Printable.

Because the interface Printable doesn't define any methods, code at (#2) doesn't need to implement any methods.

Option (f) is correct.

If Printable is defined as a class, the anonymous inner class at (#2) will subclass it.

Because Printable doesn't define any abstract methods, there aren't any added complexities.




PreviousNext

Related