ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class Box<T> {
    T value;
}
// in main:
Box<String> b1 = new Box<>();
Box<Integer> b2 = new Box<>();
System.out.println(b1.getClass().equals(b2.getClass()));
  1. A. false
  2. B. true
  3. C. Compilation error
  4. D. Depends on insertion order

Correct answer: B. true

T is erased to Object in the compiled Box class, so there is only one Box.class at run time regardless of how many type arguments are used at compile time. Both b1 and b2 share that single Class object, so the comparison is true.

More OOP with C++ and Java questions

Exams that ask this topic