Single Choice
Not Set
QWhat 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
ID: #1999
Java Language Fundamental
1,724 views
Question Info
#1999Q ID
Not SetDifficulty
Java Language FundamentalTopic
Your Answer
Choose the Best Option
Click any option to instantly check if you're correct.
Correct Answer
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.
Continue Practice
Share
Share This Question
Challenge a friend or share with your study group.
More from This Topic