Java OCA OCP Practice Question 3108

Question

Consider the following program:

import java.util.*;

public class Main {
        public static void main(String[] args) {
               List list1 = new ArrayList<>(Arrays.asList(1, "two", 3.0));  // ONE
               List list2 = new LinkedList<>
                       (Arrays.asList(new Integer(1), new Float(2.0F), new Double(3.0))); // TWO
               list1 = list2;  // THREE
               for(Object element : list1) {
                       System.out.print(element + " ");
               }/*from   w  w  w .  jav a2 s  . c  o m*/
        }
}

Which one of the following describes the expected behavior of this program?

  • a) The program results in compiler error in line marked with comment ONE.
  • b) The program results in compiler error in line marked with comment TWO.
  • c) The program results in compiler error in line marked with comment THREE.
  • d) When executed, the program prints 1 2.0 3.0.
  • e) When executed, this program throws a ClassCastException.


d)

Note

The List is a generic type that is used here in raw form; hence it allows us to put different types of values in list2.

Therefore, it prints the following: 1 2.0 3.0.




PreviousNext

Related