1. class Car
2.{
3.static int milesPerGallon;
4.int index;
5.
6.Car(int mpg) {
7.milesPerGallon = mpg;
8.index = 0;
9.}
10.Car() {
11.}
12.
13.public static void main(
String[] args) {
14.int index;
15.Car c = new Car(25);
16.
17.if (args.length > 0)
18.if (args[index].equals("Hiway"))
19.milesPerGallon*= 2;
20.System.out.println("mpg: " + milesPerGallon);
21.}
22.}
Options
1. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code displays "mpg: 25".
2. The code compiles and displays "mpg: 50" if the command-line argument is "Hiway". If the command-line argument is not "Hiway", the code throws an ArrayIndexOutOfBoundsException.
3. The code does not compile because the automatic variable named index has not been initialized.
4. The code does not compile because milesPerGallon has not been initialized.
5. The code does not compile because the no-args constructor is not written correctly
Correct Answer : 3
why ?
As i think index is initialized in the constructor call at line 8. Then why option c is correct.
Explain.
pankaj shinde