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 3 of 4.

Santhosh said:   1 decade ago
What about option 4 ?

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.

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.

Anvesh said:   1 decade ago
Yes dude,actually this is command line args, so normally arrays will be starting with zero, so given that answer c is correct, how means

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

So this args[3] value will be coming with we are using command line args. First you learn command line args carefully.

Subhan said:   1 decade ago
Anybody can explain this code clearly please I can't understand

Arindam said:   1 decade ago
When we want to run a java program then we always use
"java main_class_name"

But you use something more then that are stored in args[] array, which is also known as command line argument....

For ex- i write "java main_class_name Arindam Ghosh"
then program will run but 'Arindam' will stored in args[0] and 'Ghosh' will stored in args[1];

That's why here rip=args[3]=2

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

Asoka said:   1 decade ago
I think [B] is correct answer!
The same with Sundar!

Sundar said:   1 decade ago
The given answer is correct. I have tested the same.

D:\Siva\Java>type Test.java
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);
}
}
D:\Siva\Java>javac Test.java

D:\Siva\Java>java Test 1 3 2 2
Arg is 2

D:\Siva\Java>


Hope this will help you. Have a nice day!

Sreekanth said:   1 decade ago
Actually if you check here as explained by args[0] = java args[1] = Myfile args[2] = 1 and args[3] = 3.

So the answer is not a correct one.

Akash Gupta said:   1 decade ago
This is very cconfusinig,i can't satisfied with your sugesition.


Post your comments here:

Your comments will be displayed after verification.