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));- A. true
- B. false
- C. Compilation error: Integer is not Comparable<Integer>
- 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
- What is printed? cpp struct Base { virtual void greet() const { cout << "Base "; } }; struct Derived : Base { void greet() const override…
- What is printed? cpp class Base { public: Base(const Base&) = delete; Base() = default; virtual void show() { cout << "Base"; } }; class…
- What is printed? java class Vehicle { void move() { System.out.print("Vehicle moves "); } } class Car extends Vehicle { @Override void…
- What is printed? java class Animal { String category() { return "Animal"; } } class Mammal extends Animal { @Override String category() {…
- What is printed? java class Shape { double area() { return 0; } @Override public String toString() { return getClass().getSimpleName() +…
- What is printed? cpp class Base { public: Base() { cout << "Base "; } Base& operator=(const Base&) { cout << "Base= "; return this; } }…