Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 226

Given the code fragment:

What is the result?

    Correct Answer: C

    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.

Discussion
jackymakOption: C

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] + ":"); } }

amit_lad88Option: C

The Correct answer is C. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 //line no 14

Drift_KingOption: C

Size of array is 5 and array indexing starts from 0. So it wil throw ArrayIndexOutOfBoundsException at line 14.

EricausdresdenOption: C

if this compiles it should be C as there is no more Space in the Array after Slot [4]...