Tuesday, 2 April 2013

Array - 8

What will happen if you compile/run the following lines of code?


package net.dharmaraj;

public class dev {
public static void main(String[] xy) {
int[] iArray = new int[10];
iArray.length = 15;
System.out.println(iArray.length);

}
}



    1:  int[] iArray = new int[10];
    2:
    3:  iArray.length = 15;
    4:
    5:  System.out.println(iArray.length); 

A) Prints 10.
B) Prints 15.
C) Compilation error, you can't change the length of an array.
D) Runtime exception at line 3.

Ans :-length is a final variable

OutPut:-

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The final field array.length cannot be assigned

at net.dharmaraj.dev.main(dev.java:6)


No comments:

Post a Comment