nerdexam
Oracle

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.

Java Basics

Question

Given:

What is the result?

Exhibit

1Z0-803 question #206 exhibit

Options

  • ACompilation fails
  • BThe code compiles, but does not execute.
  • CPaildrome
  • DWow
  • EMom

How the community answered

(22 responses)
  • A
    5% (1)
  • B
    86% (19)
  • E
    9% (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.

ACompilation fails

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.

BThe code compiles, but does not execute.Correct

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.

CPaildrome

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.

DWow

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.

EMom

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

#program execution#compilation errors#runtime errors

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice