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 3 of 4.
Lachya S said:
1 decade ago
For me code compile successfully but when I was giving 1 2 3 as an command argument it is throwing error like 1 is not recognised as an internal or externalcommand. what should i do? Please give me solution for that issue.
Santosh kumar said:
1 decade ago
Argcopy declared with in two dimensional array but args initialised to argcopy[0]. Is it correct. Please explain.
Santoshkumar said:
1 decade ago
Args is an array declared in main method which will takes the command line arguments and store in an array. That will be stored in argval[0]. So here array will be stored in another element of array so now that element will acts as two dimensional array. That thing we are printing.
Arun kumar said:
1 decade ago
Here argCopy[0](reference of one dimensional array) is referring args. This means, argCopy[0] is pointing the memory location of args. Thats why it prints the values which are stored in args.
Anita said:
1 decade ago
Here argcopy declared within 2 dimensional array.
Tajinderpal singh said:
1 decade ago
Here argcopy is declared as a two dimensional array when argcopy[0] is assigned args which is an array holding command line argument args[0],args[1],args[2].Now argcopy[0] is the single row i.e the first row of the two dimensional array without any coloumn specified .when args is assigned to argcopy[0],argscopy behaves as two dimensional array b'coz now it has column specified
So,
argcopy[0][0]=1
argcopy[0][1]=2
argcopy[0][2]=3
which is been printed in the for loop.
So,
argcopy[0][0]=1
argcopy[0][1]=2
argcopy[0][2]=3
which is been printed in the for loop.
Ravindra said:
1 decade ago
How it is possible ?
Sonal Bhutada said:
1 decade ago
I m not able to understand. Please explain it in a much easier way.
Abhijit said:
1 decade ago
Can anyone explain how is it come?
Shiwam pandey said:
1 decade ago
Yes Rochu is correct and if we replace System.out.print (" " + argCopy[0][y]) ; with System.out.print (" " + argCopy[1][1]) ;.
Then it will print three times null.
Since default value of string is null.
Then it will print three times null.
Since default value of string is null.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers