1Z0-803 · Question #206
Given: What is the result?
The correct answer is B. The code compiles, but does not execute.. The code will compile successfully because array index bounds are not checked at compile time, but it will throw an ArrayIndexOutOfBoundsException at runtime when attempting to access strs[-1]. This prevents normal program execution.
Question
Given:
What is the result?
Exhibit
Options
- ACompilation fails
- BThe code compiles, but does not execute.
- CPaildrome
- DWow
- EMom
How the community answered
(22 responses)- A5% (1)
- B86% (19)
- E9% (2)
Why each option
The code will compile successfully because array index bounds are not checked at compile time, but it will throw an `ArrayIndexOutOfBoundsException` at runtime when attempting to access `strs[-1]`. This prevents normal program execution.
Compilation does not fail because `strs[-1]` is syntactically valid Java code, and index out-of-bounds is a runtime error, not a compile-time error.
Java compilers do not perform bounds checking on array accesses during compilation, allowing `strs[-1]` to compile successfully. However, at runtime, attempting to access an array with a negative index like -1 immediately triggers an `ArrayIndexOutOfBoundsException`, causing the program to terminate before any meaningful output.
This option implies a specific string output, but the program will crash with an `ArrayIndexOutOfBoundsException` before reaching any logic that could produce such an output.
This option implies a specific string output, but the program will crash with an `ArrayIndexOutOfBoundsException` before reaching any logic that could produce such an output.
This option implies a specific string output, but the program will crash with an `ArrayIndexOutOfBoundsException` before reaching any logic that could produce such an output.
Concept tested: Java array index bounds checking and runtime exceptions
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Topics
Community Discussion
No community discussion yet for this question.
