Java Programming - Language Fundamentals - Discussion
Discussion Forum : Language Fundamentals - Finding the output (Q.No. 8)
8.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
String names [] = new String[5];
for (int x=0; x < args.length; x++)
names[x] = args[x];
System.out.println(names[2]);
}
}
and the command line invocation is
> java X a b
Answer: Option
Explanation:
The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.
Discussion:
11 comments Page 2 of 2.
Ramesh said:
8 months ago
@All.
Here I got the Output :
C:\Users\user\OneDrive\Desktop>javac X.java
C:\Users\user\OneDrive\Desktop>java X Java X a b
a
Note : Entering Four Elements then args.length =4 and the loop iterates from o to3. names[0]=args[0];// Java
names[1]=args[1]; // X
names[2]=args[2]; // a
names[3]=args[3]; // b
we are printing names[2]; // a is the output.
Here I got the Output :
C:\Users\user\OneDrive\Desktop>javac X.java
C:\Users\user\OneDrive\Desktop>java X Java X a b
a
Note : Entering Four Elements then args.length =4 and the loop iterates from o to3. names[0]=args[0];// Java
names[1]=args[1]; // X
names[2]=args[2]; // a
names[3]=args[3]; // b
we are printing names[2]; // a is the output.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers