Multiple choice technology programming languages

Given: 11. public static void append(List list) { list.add(”0042”); } 12. public static void main(String[] args) { 13. List intList = new ArrayList(); 14. append(intList); 15. System.out.println(intList.get(0)); 16. } ‘What is the result?

  1. 42

  2. 0042

  3. An exception is thrown at runtime

  4. Compilation fails because of an error in line 13

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The method append accepts a raw List and adds a String object to it. Although the generic type of intList is Integer, Java uses type erasure at runtime, allowing the String to be inserted into the list. When intList.get(0) attempts to cast the retrieved element to an Integer, a ClassCastException is thrown at runtime.