Given the code fragment:
What is the result?
Given the code fragment:
What is the result?
The code creates an array of size 5, which means valid indices are from 0 to 4. Trying to assign a value to codes[5] exceeds the array boundary and will cause an ArrayIndexOutOfBoundsException. Therefore, the correct answer is that an ArrayIndexOutOfBoundsException is thrown at line 14.
public static void main(String[] args) { int[] codes = new int[5]; codes[1] = 10; codes[2] = 20; codes[3] = 30; codes[4] = 40; codes[5] = 50; for (int i = 1; i < codes.length; i++) { System.out.println(codes[i] + ":"); } }
The Correct answer is C. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 //line no 14
Size of array is 5 and array indexing starts from 0. So it wil throw ArrayIndexOutOfBoundsException at line 14.
if this compiles it should be C as there is no more Space in the Array after Slot [4]...