Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - Finding the output (Q.No. 1)
1.
What will be the output of the program?
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]);
}
}
}
and the command-line invocation is
> java CommandArgsThree 1 2 3
Answer: Option
Explanation:
In argCopy[0] = args;, the reference variable argCopy[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.
Discussion:
36 comments Page 1 of 4.
Jeet said:
8 years ago
See the default values for the arrays are like;
String [][] argCopy = {{null,null},{null,null}};
String[] args = {1,2,3};
When we assign argCopy[0] = args;
argCopy[0] = {1,2,3} and
x = argCopy[0].length; which is = 3
Hence the output as
argCopy[0][0] = 1;
argCopy[0][1] = 2;
argCopy[0][2] = 3;
String [][] argCopy = {{null,null},{null,null}};
String[] args = {1,2,3};
When we assign argCopy[0] = args;
argCopy[0] = {1,2,3} and
x = argCopy[0].length; which is = 3
Hence the output as
argCopy[0][0] = 1;
argCopy[0][1] = 2;
argCopy[0][2] = 3;
(14)
Sajid Ali said:
6 years ago
If array is declared as int a[2][2] = new int [2][2];
Then it only take a[0][0], a[0][1] and a[1][0], a[1][1] how could it takes 3 value while it initialise on a[0] that can only hold 2 a[0][0],a[0][1] values, it must throw array index out of bound exception or leave third value.
Then it only take a[0][0], a[0][1] and a[1][0], a[1][1] how could it takes 3 value while it initialise on a[0] that can only hold 2 a[0][0],a[0][1] values, it must throw array index out of bound exception or leave third value.
(7)
Surbhi said:
5 years ago
Thank you so much for explaining. It's very useful.
(2)
Rajgopal bhallamudi said:
8 years ago
Here is my clear explanation of what is happening internally.
argCopy initially contains [ [null,null],[null,null] ]
argCopy[0]=[null,null]
argCopy[1]=[ [null,null]
while args initially contains 1 2 3.
Now after this assignment argCopy[0] = args;,
argCopy changes to,
[ [1,2,3],[null,null] ]
argCopy initially contains [ [null,null],[null,null] ]
argCopy[0]=[null,null]
argCopy[1]=[ [null,null]
while args initially contains 1 2 3.
Now after this assignment argCopy[0] = args;,
argCopy changes to,
[ [1,2,3],[null,null] ]
(2)
John said:
2 years ago
Please explain the answer clearly.
(1)
Naresh said:
1 decade ago
Here:
String[][] argCopy = new String[2][2];
Here argCopy[][] will have argCopy[0][0], argCopy[0][1], argCopy[1][0], argCopy[1][1].
When we assign argCopy[0] = args;
Here argCopy[0] will convert into argCopy[0][3] (since arg=3). That's why it will print the those values.
String[][] argCopy = new String[2][2];
Here argCopy[][] will have argCopy[0][0], argCopy[0][1], argCopy[1][0], argCopy[1][1].
When we assign argCopy[0] = args;
Here argCopy[0] will convert into argCopy[0][3] (since arg=3). That's why it will print the those values.
(1)
Prateek Nanhorya said:
1 decade ago
This is very wrong question it's answer can't be like that its answer will be nothing because values of x is 0;.
(1)
Eric said:
1 decade ago
How do I invoke the command line?
Dima said:
1 decade ago
Where is the first space character?
Eric said:
1 decade ago
How do I get this program to execute?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers