ProgrammingHardProduct company OA

What is printed?

What is printed?
class Node<T> {
    T data;
    static int count;
    { count++; }
    Node(T d) { data = d; }
}
// in main:
new Node<Integer>(1);
new Node<String>("a");
new Node<Double>(3.0);
System.out.println(Node.count);
  1. A. 3
  2. B. 1
  3. C. 0
  4. D. Compilation error: count cannot be shared across different type parameterizations

Correct answer: A. 3

Generics are a compile-time-only concept: at run time there is exactly one Node class (type erasure), so its single static field count is shared across every parameterization T. Each `new Node<...>()` call runs the instance initializer block once, incrementing the same shared counter to 3.

More OOP with C++ and Java questions

Exams that ask this topic