What will be the output of the program?
and the command-line invocation is
public class CommandArgsThree
{
public static void main(String [] args)
{
String [][] argCopy = new String[2][2];
int x;
argCopy[0] = args;
x = argCopy[0].length;
for (int y = 0; y < x; y++)
{
System.out.print(" " + argCopy[0][y]);
}
}
}
> java CommandArgsThree 1 2 3
Answer: D
Correct answer: 1 2 3
Assigning args to argCopy[0] replaces the two-element array that was there with a reference to the command line array itself - array references are assigned, not copied element by element, and the declared inner length of 2 is not a constraint on what may later be stored there. So x becomes 3 and the loop prints all three arguments. The new String[2][2] is a red herring, and the array it created for row 0 is simply discarded.
Please log in or register to report this question.