Java OCA OCP Practice Question 2501

Question

What is the result of the following class?

1:   public class Box<T> { 
2:      T value; /*from   ww w . j  a v a2s .c  o  m*/
3: 
4:      public Box(T value) { 
5:         this.value = value; 
6:      } 
7:      public T getValue() { 
8:         return value; 
9:      } 
10:     public static void main(String[] args) { 
11:        Box<String> one = new Box<String>("a string"); 
12:        Box<Integer> two = new Box<>(123); 
13:        System.out.print(one.getValue()); 
14:        System.out.print(two.getValue()); 
15:     } 
16:  } 
  • A. Compiler error on line 1.
  • B. Compiler error on line 2.
  • C. Compiler error on line 11.
  • D. Compiler error on line 12.
  • E. a string123
  • F. An exception is thrown.


E.

Note

This class is a proper use of generics.

Box uses a generic type named T.

On line 11, the generic type is String.

On line 12 the generic type is Integer.

Line 12 also uses the diamond operator.




PreviousNext

Related