Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 6)
6.
What will be the output of the program?
public class CommandArgsTwo 
{
    public static void main(String [] argh) 
    {
        int x;
        x = argh.length;
        for (int y = 1; y <= x; y++) 
        {
            System.out.print(" " + argh[y]);
        }
    }
}

and the command-line invocation is

> java CommandArgsTwo 1 2 3

0 1 2
1 2 3
0 0 0
An exception is thrown at runtime
Answer: Option
Explanation:

An exception is thrown because at some point in (System.out.print(" " + argh[y]);), the value of x will be equal to y, resulting in an attempt to access an index out of bounds for the array. Remember that you can access only as far as length - 1, so loop logical tests should use x < someArray.length as opposed to x < = someArray.length.

Discussion:
10 comments Page 1 of 1.

Omkar said:   7 years ago
argh[0]=1;
argh[1]=2;
argh[2]=3;

And in program arhg[y] is starts from 1. ie argh[1], so when y=3, it is trying to access the argh[3] which is not present. so it will give the exception arrayindexoutofbound.

Gopi said:   1 decade ago
x[0] = new int[4][];
x[1] = new int[2][];
x[2] = new int[5][];
for (i = 0; i < 3; i++)
{
for (j = 0; j < x[0].length; j++)
{
x[i][j] = new int [i + j + 1];
System.out.println("size = " + x[i][j].length);
}
}

output
x[0].length = 4;
x[1].length = 2;
x[2].length = 5;
--
11

Vijaya said:   1 decade ago
In above for loop if we remove y<=x, and kept y<x then print 123 as output.

Sushil said:   1 decade ago
When I run this program. The program got run successfully without any error and while dubuggin I found that the value of x is getting as 0. So it making the for loop condition false.

Geetha said:   1 decade ago
x = argh.length; /output = 0.

for (int y = 1; y <= x; y++) // loop starts from 1 and length = 0.
{
System.out.print(" " + argh[y]);
}

So no output.

SB MOHAN said:   1 decade ago
argh[0] = 1;
argh[1] = 2;
argh[2] = 3;

but, in for loop, when y=3, the call to argh[3] is trying to access the value which is not entered from command line arguments,
So it's ArrayIndexOutOfBounds error

Sundar said:   1 decade ago
The following is the error occurred at run-time.

D:\Java>javac CommandArgsTwo.java

D:\Java>java CommandArgsTwo.java
Exception in thread "main" java.lang.NoClassDefFoundError: CommandArgsTwo/java
Caused by: java.lang.ClassNotFoundException: CommandArgsTwo.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: CommandArgsTwo.java. Program will exit.

Rajkumar said:   1 decade ago
Value of x and why are different right?.

Value of x is length of array of argh and value of why is initialized in for loop. Then how x and why will be equal. Will you please clear the doubt please.

Jillpdkt said:   1 decade ago
@madhu here y is assigned to 1, so it start to print from argh[1].

Madhu said:   1 decade ago
How come number format exception we get in above program?. Plzzz explain.

Post your comments here:

Your comments will be displayed after verification.