Multiple choice technology programming languages

What is the output for the below code ? public class Test { public static void main(String... args) { ArrayList list = new ArrayList(); list.add(1); list.add(2); list.add(3); for(int i:list) System.out.println(i); } }

  1. 1 2 3

  2. Compile error , can't add primitive type in ArrayList

  3. Compile error on for(int i:list) , Incorrect Syntax

  4. 0 0 0

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

Java autoboxing automatically converts primitive int values to Integer objects when adding to ArrayList. The enhanced for loop iterates through each element and prints it on a separate line. Options B and C are incorrect because autoboxing and the for-each syntax are both valid in Java.

AI explanation

Java's autoboxing automatically converts the primitive int literals 1, 2, 3 into Integer objects when added to the ArrayList, and the enhanced for-loop (for(int i:list)) auto-unboxes each Integer back to int when read. So the loop simply prints the elements in insertion order: 1, 2, 3 — there's no compile error since autoboxing/unboxing handles the primitive-to-wrapper conversion transparently.