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));- A. 12 true true
- B. 7 false true
- C. 12 false true
- 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
- What is printed? java enum Day { MON, TUE, WED } // in main: Map<Day, Integer> m = new EnumMap<>(Day.class); m.put(Day.WED, 3)…
- Which line fails to compile? java List<String>[] a = new List<String>[2]; // L1 List<?>[] b = new List<?>[2]; // L2 List<String>[] c = new…
- Which line fails to compile? java class Box<T extends Number> { double d(T t) { return t.doubleValue(); } } // in main: Box<Integer> a =…
- Which class compiles? java class K1 { static { throw new RuntimeException(); } } class K2 { static { if (true) throw new…
- What happens? java class W { { if (true) throw new java.io.IOException("io"); } W() throws java.io.IOException { } }
- Which line fails to compile? cpp // C++17 struct S { int v; S(int x) : v(x) { } bool operator==(const S& o) const { return v == o.v; } }…