Multiple choice technology programming languages

4) What is the result of compiling and running the following code? class VarArgOne { public static void printArgs(String s, Integer ... i, String s) { //line 1 for(int j : i) { //line 2 System.out.print(j + " " + s); //line 3 } } public static void main(String ... args) { //line 4 printArgs("exam", 12, 34, "scjp"); //line 5 } }

  1. Compilation fails due to error at line 1.

  2. Compilation fails due to error at line 2.

  3. Compilation fails due to error at line 4.

  4. Compilation fails due to error at both line 1 and line 4.

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

The varargs parameter (Integer ... i) must be the last parameter in the method signature. Line 1 has String s after Integer ... i, which is invalid varargs syntax. This causes compilation error at line 1. Line 2 (enhanced for loop) and line 4 (varargs in main method) are both valid Java syntax.