Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - General Questions (Q.No. 4)
4.
public class Myfile 
{ 
    public static void main (String[] args) 
    {
        String biz = args[1]; 
        String baz = args[2]; 
        String rip = args[3]; 
        System.out.println("Arg is " + rip); 
    } 
}
Select how you would start the program to cause it to print: Arg is 2
java Myfile 222
java Myfile 1 2 2 3 4
java Myfile 1 3 2 2
java Myfile 0 1 2 3
Answer: Option
Explanation:

Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.

Discussion:
33 comments Page 2 of 4.

Aanand said:   1 decade ago
Answer C is right.

args[0]=1;
args[1]=3;
args[2]=2;
args[3]=2;

Please do not get confused with args[0] = java and args[1] = Myfile . This is true in C but not in java. In java , command line arguments till application name (here Myfile) is not the part of array args.

Gaurav singh said:   1 decade ago
public class Test
{
public static void main (String[] args)
{
String biz = args[1];
String baz = args[2];
String rip = args[3];
System.out.println("Arg is " + rip);
}
}
the o/p needed is--> Arg is 2.

i.e. 2 is at the rip place.
And rip is args[3] i.e 4th place.

Because cmd line arg start with 0,1,2,3,4 and so on.
Hence in the option we need 2 at the 4th place.
That's why the ans is 1 3 2 2.

Santhosh said:   1 decade ago
What about option 4 ?

Narendra prajapati said:   1 decade ago
At cmd line args[] array has contain index. Which is start from 0 to no of arg. So if we are giving 11 2 3 56(including space).

So args[] array will take 11 on 0th index and 2 will take on 1st and 3 will take on 2nd and 56 on 3rd.

Sindoori said:   1 decade ago
If command line args starts from 0123 then how args[1] is 3 and args[2] is 2. The answer should be 1232.

Raju said:   1 decade ago
Guys Here question is asked about rip means args[3]. So don't think about args[0], args[1] or args[2]. Only think that in 4th args[] i.e. args[3] comes on which order.

String args[] position
biz = args[1] 0
baz = args[2] 1
rip = args[3] 2

And we can easily say that,

Whatever we will give the sequence no in input the 4th place should be 2nd position and here is only in option.

1 3 2 2

Ted said:   1 decade ago
The answer is right. It is all about the COMMAND LINE ARGUMENTS.

The Java application = Myfile so,
When you write a command-line arguments.
Java Myfile .....
In this case we want the output to be: Args is 2.

Java Myfile 1322.
args[0] is 1.
args[1] is 3 which is biz.
args[2] is 2 which is baz.
args[3] is 2 and since rip is args[3], the answer is C.

Saurabh omer said:   1 decade ago
Command line arguments accept max index definition.....
a=args[4];

Then user must takes 4 values like java Myfile 1 2 3 4...
Another program,
a=args[3];

Then user must takes 3 values like java Myfile 2 3 4...

Punam said:   1 decade ago
Not getting output.

Suneel said:   1 decade ago
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1.

At com.java.TestObj.main(TestObj.java:7).


Post your comments here:

Your comments will be displayed after verification.