ProgrammingVery hardProduct-company placement · Java

Which lines fail to compile?

Which lines fail to compile?
class G<T> {
    T[] arr = new T[10];                                    // L1
    T make() { return new T(); }                             // L2
    boolean is(Object o) { return o instanceof G<?>; }       // L3
    static T sv;                                             // L4
}
  1. A. L1 and L2 only
  2. B. L1, L2 and L4
  3. C. All four
  4. D. L2 and L3

Correct answer: B. L1, L2 and L4

With erasure the type T is unknown at run time, so new T[] and new T() are illegal (L1, L2), and a static field cannot use the class's type parameter (L4). instanceof with the unbounded wildcard G<?> is allowed (L3).

More OOP with C++ and Java questions

Exams that ask this topic