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 2Answer: 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.
Maruthu said:
3 years ago
Very confusing, somebody please help me.
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.
{
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)
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.
{
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.
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.
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.
Anil said:
6 years ago
I can't understand this. Please, anyone, help me to get this.
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.
args[0]=1
args[1]=3
args[2]=2
args[3]=2
So, option C is the correct answer.
Vijaya said:
7 years ago
I Am not understanding please explain correctly.
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.
$java -Xmx128M -Xms16M Example
-2 -2.0
1 1.0
same results 2 time(s).
Here is a result for above question.
(1)
Palak said:
8 years ago
Thanks, @Ted.
Your explanation is very helpful to understand.
Your explanation is very helpful to understand.
Rakesh kumar said:
9 years ago
Agree @Suneel.
This will generate ArrayIndexOutOfBoundException.
This will generate ArrayIndexOutOfBoundException.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers