Java OCA OCP Practice Question 2995

Question

Consider the following program:

interface Printable { }

enum My implements Printable {     // IMPLEMENTS_INTERFACE
    CASE;//from   ww w. j av  a 2  s. c om
}

public class Main {
    public static void main(String []args) {
        if(My.CASE instanceof My) {
            System.out.println("yes, instance of My");
        }
        if(My.CASE instanceof Printable) {
            System.out.println("yes, instance of Printable");
        }
        if(My.CASE instanceof Enum) {   // THIRD_CHECK
            System.out.println("yes, instance of Enum");
        }
    }
}

Which one of the following options is correct?

a)   this program results in a compiler error in the line marked with comment IMPLEMENTS_INTERFACE

b)   this program results in a compiler in the line marked with comment THIRD_CHECK

c)   When executed, this program prints the following: 
     yes, instance of My//from w  w w .j av a  2s .c  o m

d)   When executed, this program prints the following:

     yes, instance of My
     yes, instance of Printable

e)   When executed, this program prints the following:

     yes, instance of My
     yes, instance of Printable
     yes, instance of Enum


e)

Note

an enumeration can implement an interface (but cannot extend a class, or cannot be a base class).

each enumeration constant is an object of its enumeration type.

an enumeration automatically extends the abstract class java.util.Enum.

hence, all the three instanceof checks succeed.

this program compiles cleanly and hence options a) and b) are wrong.

options c) and d) do not provide the complete output of the program and hence they are also incorrect.




PreviousNext

Related