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.
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.
Disha said:
1 decade ago
Please explain me.
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)
Poornima said:
1 decade ago
argCopy[0] = args;
x = argCopy[0].length;(x=3)
for (int y = 0; y < x; y++) i.e. (int y=0;y<3;y++)
argCopy[0][y] i.e. argCopy[0][0],argCopy[0][1],argCopy[0][2]
Invocation command is 1 2 3
argCopy[0][0] = 1
argCopy[0][1] = 2
argCopy[0][2] = 3
So output is 1 2 3.
x = argCopy[0].length;(x=3)
for (int y = 0; y < x; y++) i.e. (int y=0;y<3;y++)
argCopy[0][y] i.e. argCopy[0][0],argCopy[0][1],argCopy[0][2]
Invocation command is 1 2 3
argCopy[0][0] = 1
argCopy[0][1] = 2
argCopy[0][2] = 3
So output is 1 2 3.
P .naresh said:
1 decade ago
What is the basic programming structure of java?
Karthik said:
1 decade ago
public class Simple
{
public static void main(String[] argv)
{
//some program code goes here
}
}
example:
class Welcome
{
// A java program will start from here.
public static void main(String args[])
{
System.out.println(" Welcome to Java-Samples!!! ");
}
}
{
public static void main(String[] argv)
{
//some program code goes here
}
}
example:
class Welcome
{
// A java program will start from here.
public static void main(String args[])
{
System.out.println(" Welcome to Java-Samples!!! ");
}
}
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] ?
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.
Dima said:
1 decade ago
Where is the first space character?
Eric said:
1 decade ago
How do I invoke the command line?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers