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");
  1. A. OS
  2. B. SS
  3. C. OO
  4. 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

Exams that ask this topic