Please find out the answer for this program.

Asked By 0 points N/A Posted on -
qa-featured

What will be printed out if this code is run with the following command line?
java myprog good morning
public class myprog{   public static void main(String argv[])   {System.out.println(argv[2]);   }} 1) myprog
2) good
3) morning
4) Exception raised:"java.lang.

ArrayIndexOutOfBoundsException: 2"
SHARE
Answered By 0 points N/A #90361

Please find out the answer for this program.

qa-featured

The answer for your code is #4. Exception raised:java.lang.ArrayIndexOutOfBoundsException: 2

This is because, in your code, you lack some data or no data at all where your statement argv[2] will refer to. Accessing data in an array is through its index number. Lets say you have an array of integers.

                int[] mynum=new int[3];

then we will put values to our array.

mynum[0]=1; — the [0] is the index in our array where the number 1 is stored

mynum[1]=2;

mynum[2]=3;

Now, let say we want to print/show the value that is stored at array index 3. 

System.out.print(mynum[3]);

this code will give an error Exception raised:java.lang.ArrayIndexOutOfBoundsException: 3 this is because, in our array, the maximum number in our array index is only 2  and you want to access the data in array index 3 where theres no available data on it. 

Based in your code, there's no value/data stored in your argv[].

Related Questions