Java Programming - Java.lang Class - Discussion

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

[A]. java Myfile 222
[B]. java Myfile 1 2 2 3 4
[C]. java Myfile 1 3 2 2
[D]. java Myfile 0 1 2 3

Answer: Option C

Explanation:

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


Nijin said: (Feb 25, 2011)  
Can anybody help me understanding this?

Sathish said: (Jun 1, 2011)  
I can't understanding this concept please any one help me.

Abhijit said: (Jun 17, 2011)  
args[0]=1,args[1]=3,args[2]=2,args[3]=3 and hence rip=args[3]=3

Akash Gupta said: (Aug 27, 2011)  
This is very cconfusinig,i can't satisfied with your sugesition.

Sreekanth said: (Aug 30, 2011)  
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.

Sundar said: (Sep 22, 2011)  
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!

Asoka said: (Jan 10, 2012)  
I think [B] is correct answer!
The same with Sundar!

Arindam said: (Feb 1, 2012)  
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

Subhan said: (Aug 17, 2012)  
Anybody can explain this code clearly please I can't understand

Anvesh said: (Nov 21, 2012)  
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: (Jun 26, 2013)  
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: (Aug 24, 2013)  
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: (Sep 11, 2013)  
What about option 4 ?

Narendra Prajapati said: (Apr 15, 2014)  
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: (Apr 27, 2014)  
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: (Jun 4, 2014)  
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: (Jul 23, 2014)  
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: (Sep 27, 2014)  
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: (Dec 16, 2014)  
Not getting output.

Suneel said: (Jul 5, 2015)  
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1.

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

Sunil said: (Mar 7, 2016)  
Completely not getting output. Please someone explain it in detail.

Abhishek said: (Oct 25, 2016)  
Why D is not correct?

It also gives output as Arg is 2.

Dave said: (Dec 18, 2016)  
It was really true when we see that program executes that the fourth location must be 2.

Rakesh Kumar said: (Jan 23, 2017)  
Agree @Suneel.

This will generate ArrayIndexOutOfBoundException.

Palak said: (Jul 16, 2017)  
Thanks, @Ted.

Your explanation is very helpful to understand.

Jay said: (Jul 7, 2018)  
$javac Example.java
$java -Xmx128M -Xms16M Example
-2 -2.0
1 1.0
same results 2 time(s).

Here is a result for above question.

Vijaya said: (Dec 13, 2018)  
I Am not understanding please explain correctly.

Harish said: (Feb 4, 2019)  
'java Myfile' is written to run the program after compilation and it's not an argument. Actually arguments start from 1.
args[0]=1
args[1]=3
args[2]=2
args[3]=2

So, option C is the correct answer.

Anil said: (Sep 23, 2019)  
I can't understand this. Please, anyone, help me to get this.

Osama said: (May 6, 2020)  
@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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.