ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class Repo<T> {
    private List<T> items = new ArrayList<>();
    void add(T item) { items.add(item); }
    T get(int i) { return items.get(i); }
}
// in main:
Repo<Integer> repo = new Repo<>();
repo.add(10);
repo.add(20);
int sum = repo.get(0) + repo.get(1);
System.out.println(sum);
  1. A. 30
  2. B. 1020
  3. C. Compilation error: cannot add int to the generic List<T>
  4. D. It throws a ClassCastException at run time

Correct answer: A. 30

At compile time T is bound to Integer, so autoboxing handles add(10)/add(20) and get() returns Integer which unboxes automatically for the + operator (an inserted synthetic cast to Integer after erasure makes this safe at run time too): 10 + 20 = 30.

More OOP with C++ and Java questions

Exams that ask this topic