ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
enum Op {
    ADD { int apply(int a, int b) { return a + b; } },
    MUL { int apply(int a, int b) { return a * b; } };
    abstract int apply(int a, int b);
}
// in main:
System.out.print(Op.MUL.apply(3, 4) + " " + (Op.ADD.getClass() == Op.class) + " "
    + (Op.ADD.getDeclaringClass() == Op.class));
  1. A. 12 true true
  2. B. 7 false true
  3. C. 12 false true
  4. D. 12 true false

Correct answer: C. 12 false true

An enum constant with a body is an instance of an anonymous subclass of the enum, so getClass() is not Op.class; getDeclaringClass() returns the enum type itself.

More OOP with C++ and Java questions

Exams that ask this topic