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 2 of 4.
Nikhil said:
10 years ago
Please anyone explain code line by line.
USHA said:
10 years ago
Please explain me with simple example.
Jyothi said:
1 decade ago
Can anyone explain this clearly because I don't know java.
I'm trying to analyse using C. Help me out.
I'm trying to analyse using C. Help me out.
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)
Cat said:
1 decade ago
As I don't know anything about java so please explain me clearly. Don't hesitate to help me.
Eric said:
1 decade ago
How do I get this program to execute?
Eric said:
1 decade ago
How do I invoke the command line?
Dima said:
1 decade ago
Where is the first space character?
Yair said:
1 decade ago
args is an array with 3 elements - while initially argCopy[0] is a 2 element array - By inserting args into it:
argCopy[0] = args;
It is re-assigned to a three element array.
argCopy[0] = args;
It is re-assigned to a three element array.
Alexander said:
1 decade ago
String [][] argCopy = new String[2][2];
argCopy is declared with 2 dimensional array. Their size is 2. How can be assign the value to a[0][2] ?
argCopy is declared with 2 dimensional array. Their size is 2. How can be assign the value to a[0][2] ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers