ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
// static generic method of the main class:
static <T extends Comparable<T>> T max(T a, T b) {
    return a.compareTo(b) >= 0 ? a : b;
}
// in main:
System.out.print(max("pear", "apple") + " " + max(3, 7) + " " + max('z', 'a'));
  1. A. apple 7 z
  2. B. pear 3 a
  3. C. pear 7 a
  4. D. pear 7 z

Correct answer: D. pear 7 z

T is inferred as String, Integer and Character in turn; each compares by its natural order: "pear" > "apple" lexicographically, 7 > 3 and 'z' > 'a'.

More OOP with C++ and Java questions

Exams that ask this topic