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

Osama said:   5 years ago
@All.

Here args take the value from the command line i.e command line arguments from the keyboard.

When you compile the program and runs it using java Test .
Along with java Test you have to give the value for an array also like a[0],a[1],a[2] for a[3] give the int value 2 i.e you're passing the value from keyboard to that method here args is a parameter that expects some value from the user so, java Test 1,3,2,2 will be sent from the user keyboard and store it in an array in args so the value of

a[0]=1
a[1]=3
a[2]=2
a[3]=2 remember that a[0] is already been specified.

Kumar said:   4 years ago
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);
}
}


See here;

Select how you would start the program to cause it to print: *Arg is 2*
We have to print *Arg is 2* and array start with *index 0*
So by option, we need 2 at the index of 3.

args[0] note it is not given in program
args[1]
args[2]
at args[3] we need 2 so by option *java Myfile 1 3 2 2* it is correct.

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

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!

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

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.

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.

Shuvo said:   3 years ago
public class Test1
{
public static void main (String[] args)
{
String biz = args[1];
String baz = args[2];
String rip = args[3];
System.out.println("Arg is " + rip);
}
}
run through terminal:
> javac Test1.java
> java Test1 java Myfile 1 2 2 3 4
Arg is 2.

So, option B will be right.
(1)

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.

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.


Post your comments here:

Your comments will be displayed after verification.