ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class Node<T extends Comparable<T>> {
    T value;
    Node(T v) { value = v; }
    boolean isGreaterThan(Node<T> other) { return value.compareTo(other.value) > 0; }
}
// in main:
Node<Integer> a = new Node<>(10);
Node<Integer> b = new Node<>(5);
System.out.println(a.isGreaterThan(b));
  1. A. true
  2. B. false
  3. C. Compilation error: Integer is not Comparable<Integer>
  4. D. Compilation error: isGreaterThan needs an explicit cast

Correct answer: A. true

Integer implements Comparable<Integer>, satisfying T extends Comparable<T>. a.value.compareTo(b.value) is 10.compareTo(5), which is positive, so isGreaterThan returns true.

More OOP with C++ and Java questions

Exams that ask this topic