Asked By
mad4uash1
0 points
N/A
Posted on - 10/09/2011
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"
Please find out the answer for this program.
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[].