ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
Runnable r = new Runnable() { public void run() { } };
System.out.print("[" + r.getClass().getSimpleName() + "]" + r.getClass().isAnonymousClass());
  1. A. [Runnable]true
  2. B. [Runnable$1]true
  3. C. []false
  4. D. []true

Correct answer: D. []true

getSimpleName() returns an empty string for an anonymous class; the compiler-generated binary name (like Main$1) is what getName() would show.

More OOP with C++ and Java questions

Exams that ask this topic