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());- A. [Runnable]true
- B. [Runnable$1]true
- C. []false
- 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
- Which line fails to compile? java class P { final void f(int x) { } } class C extends P { void f(long x) { } // L1 void f(int y) { } // L2 }
- What is printed? java class P { int x; P(int x) { this.x = x; } public boolean equals(Object o) { return o != null && getClass() ==…
- What is printed? java Boolean b1 = true, b2 = true; Character c1 = 'a', c2 = 'a'; Short s1 = 100, s2 = 100; System.out.print((b1 == b2) +…
- What is printed? java List<Integer> l = Arrays.asList(3, 1, 2); Collections.sort(l); System.out.print(l + " "); l.clear();
- What is printed? java List<String> l = new ArrayList<>(List.of("a", "b", "c")); ListIterator<String> it = l.listIterator(); while…
- What is printed? java System.out.print("a.b.c".split(".").length + " " + "a.b.c".split(".").length);