Java Programming - Language Fundamentals - Discussion

Discussion :: 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

[A]. 0 0
[B]. 1 2
[C]. 0 0 0
[D]. 1 2 3

Answer: Option D

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.

Abhijit said: (Jun 17, 2011)  
Can anyone explain how is it come?

Lachya S said: (Jun 30, 2011)  
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: (Jul 23, 2011)  
Argcopy declared with in two dimensional array but args initialised to argcopy[0]. Is it correct. Please explain.

Santoshkumar said: (Jul 23, 2011)  
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: (Aug 1, 2011)  
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: (Aug 10, 2011)  
Here argcopy declared within 2 dimensional array.

Tajinderpal Singh said: (Aug 14, 2011)  
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.

Ravindra said: (Jan 19, 2012)  
How it is possible ?

Sonal Bhutada said: (Mar 1, 2012)  
I m not able to understand. Please explain it in a much easier way.

Rochu said: (Mar 6, 2012)  
Tajinderpal Singh is wrong because argcopy is an 2D array . it only have argcopy[0][0],argcopy[0][1],argcopy[1][0],argcopy[1][1].
it does not have memory location like argcopy[0][2]=3.

Shiwam Pandey said: (Jun 18, 2012)  
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.

Disha said: (Aug 19, 2012)  
Please explain me.

Prateek Nanhorya said: (Sep 2, 2012)  
This is very wrong question it's answer can't be like that its answer will be nothing because values of x is 0;.

Poornima said: (Apr 3, 2013)  
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.

P .Naresh said: (Jun 9, 2013)  
What is the basic programming structure of java?

Karthik said: (Jun 11, 2013)  
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!!! ");
}
}

Alexander said: (Aug 24, 2014)  
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] ?

Yair said: (Dec 15, 2014)  
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.

Dima said: (Jan 28, 2015)  
Where is the first space character?

Eric said: (Apr 21, 2015)  
How do I invoke the command line?

Eric said: (Apr 21, 2015)  
How do I get this program to execute?

Cat said: (May 29, 2015)  
As I don't know anything about java so please explain me clearly. Don't hesitate to help me.

Naresh said: (Jun 3, 2015)  
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.

Jyothi said: (Aug 8, 2015)  
Can anyone explain this clearly because I don't know java.

I'm trying to analyse using C. Help me out.

Usha said: (Oct 26, 2015)  
Please explain me with simple example.

Nikhil said: (Oct 29, 2015)  
Please anyone explain code line by line.

Kiran said: (Nov 8, 2015)  
argCopy[0] = args;

Because of this line argCopy[0] will point to args's base address, suppose 100, so the values are like at 100 ->1, at next address means base+size of the string (length) i.e.101->2, 103->3.

So when we access argCopy[0][0] it will point to base+0 i.e 100 address location that have value 1.

argCopy[0][1] it will point to base+1 i.e 101 address location that have value 2.

argCopy[0][2] it will point to base+2 i.e 102 address location that have value 3.

So it will print 1 2 3, irrespective of declaration[2][2].

Niroshkumar B said: (Nov 13, 2015)  
args[0]=1.
args[1]=2.
args[2]=3.

In this statement: argcopy[0] = args; -> [[0, 1], [0, 1]] = [1, 2, 3].

0 1 0 //after that.
0 1 2.

-> [[1, 2, 3], [0, 1]] //assign like this.
0 1.

-> So length y is 3 -> [1, 2, 3].
y.

-> argcopy[0][0] = 1.
argcopy[0][1] = 2.
argcopy[0][2] = 3.

Mohammed Salman said: (Apr 26, 2017)  
Hello, Everyone.

1. Initially, argcopy is a reference to a 2D array object.
2. When argcopy[0]=args, that is the "argscopy" reference will be pointing to an array of "args".
3. Hence it prints the value inside the "args" array.
4. Finally, the 2D array becomes an anonymous object.

Hope everyone understands.

Rajgopal Bhallamudi said: (Aug 12, 2017)  
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] ]

Sharma said: (Sep 18, 2017)  
I can't understand. How output will 1, 2, 3?

Jeet said: (Sep 21, 2017)  
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;

Priyanka said: (Feb 3, 2018)  
String [][] argCopy = new String[2][2];

argCopy is declared with 2-dimensional array. Their size is 2. How can be assigned the value to a[0][2]?

Sajid Ali said: (Nov 11, 2019)  
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.

Surbhi said: (Apr 10, 2020)  
Thank you so much for explaining. It's very useful.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.