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

names
null
Compilation fails
An exception is thrown at runtime
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.

Vaibhav Gupta said:   1 decade ago
What about the X. I think the value of X should be assigned to element 0, a and b be assigned respectively to 1st and 2nd element of the names array.


Post your comments here:

Your comments will be displayed after verification.