Discussion
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.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development