ProgrammingModerateProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
class Box<T extends Number> {
    double d(T t) { return t.doubleValue(); }
}
// in main:
Box<Integer> a = new Box<>();   // L1
Box<String>  b = new Box<>();   // L2
Box<Number>  c = new Box<>();   // L3
  1. A. L3 only
  2. B. L2 and L3
  3. C. None
  4. D. L2 only

Correct answer: D. L2 only

The bound T extends Number allows Number and its subclasses and lets the body call Number methods such as doubleValue(). String is not a Number.

More OOP with C++ and Java questions

Exams that ask this topic