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.

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)

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)

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

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.

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

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

Abhishek said:   9 years ago
Why D is not correct?

It also gives output as Arg is 2.

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

Rakesh kumar said:   9 years ago
Agree @Suneel.

This will generate ArrayIndexOutOfBoundException.


Post your comments here:

Your comments will be displayed after verification.