Java OCA OCP Practice Question 3009

Question

Which one of the following class definitions will compile without any errors?

a)  class P<T> {
        static T s_mem;
    }//from  www .  j a va  2 s .  co m

b)  class Q<T> {
        T mem;
        public Q(T arg) {
            mem = arg;
        }
    }

c)  class R<T> {
        T mem;
        public R() {
            mem = new T();
        }
    }

d)  class S<T> {
        T []arr;
        public S() {
            arr = new T[10];
        }
    }


b)

Note

option a) You cannot make a static reference of type T in a generic class.

option c) and d) You cannot instantiate the type T or T[] using new operator in a generic class.




PreviousNext

Related