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.

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.

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...

Harish said:   7 years ago
'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.

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.

Jay said:   7 years ago
$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.
(1)

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

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

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.

Dave said:   9 years ago
It was really true when we see that program executes that the fourth location must be 2.

Sunil said:   9 years ago
Completely not getting output. Please someone explain it in detail.

Rakesh kumar said:   9 years ago
Agree @Suneel.

This will generate ArrayIndexOutOfBoundException.


Post your comments here:

Your comments will be displayed after verification.