ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
// static methods of the main class:
static void p(Object o) { System.out.print("O"); }
static void p(String s) { System.out.print("S"); }
static <T> void call(T t) { p(t); }
// in main:
call("x");
p("x");- A. OS
- B. SS
- C. OO
- D. SO
Correct answer: A. OS
Overloads are chosen at compile time from static types. Inside call, t has type T whose bound is Object, so p(Object) is chosen once for every T; the direct call with a String literal picks p(String).
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2)); for (Map.Entry<String, Integer> e : m.entrySet())…
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2, "c", 3)); m.keySet().remove("b")…
- Which statement about the protected modifier is true in Java but NOT in C++?
- What is printed? java // file a/P.java package a; public class P { void f() { System.out.print("P"); } // package-private public void…
- What is printed? java String r = Stream.of(3, 1, 3, 2).distinct().sorted() .map(String::valueOf).collect(Collectors.joining("-"))…
- What is printed? java System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ")…