nerdexam
Oracle

1Z0-803 · Question #201

Given: And the commands: Javac Test.java Java Test 12345 What is the result?

The correct answer is D. AnArrayIndexOutOfBoundException is thrown at runtime.. The program attempts to access an index beyond the bounds of the args array when executed with a single command-line argument, resulting in a runtime exception.

Creating and Using Arrays

Question

Given:

And the commands:

Javac Test.java Java Test 12345 What is the result?

Exhibit

1Z0-803 question #201 exhibit

Options

  • ANumber us : 12345
  • BA NullPointerException is thrown at runtime
  • CA NumberFormatException is thrown at runtime
  • DAnArrayIndexOutOfBoundException is thrown at runtime.

How the community answered

(55 responses)
  • A
    13% (7)
  • B
    7% (4)
  • C
    4% (2)
  • D
    76% (42)

Why each option

The program attempts to access an index beyond the bounds of the `args` array when executed with a single command-line argument, resulting in a runtime exception.

ANumber us : 12345

The output "Number us : 12345" would only occur if the program successfully accessed and printed `args[0]` without an exception, which contradicts the `ArrayIndexOutOfBoundsException`.

BA NullPointerException is thrown at runtime

A `NullPointerException` typically occurs when attempting to use an object reference that is `null`; it is not the specific exception thrown when accessing an array out of its defined bounds.

CA NumberFormatException is thrown at runtime

A `NumberFormatException` occurs when attempting to convert a string to a numeric type, but the string cannot be parsed; this is not the primary issue when accessing an array out of bounds.

DAnArrayIndexOutOfBoundException is thrown at runtime.Correct

When `Java Test 12345` is executed, the `args` array in the `main` method will contain one element, `args[0]` with the value "12345". If the program then attempts to access `args[1]` or any index greater than or equal to `args.length` (which is 1), an `ArrayIndexOutOfBoundsException` is thrown at runtime because the index is out of the valid range.

Concept tested: Java command-line arguments array access

Source: https://docs.oracle.com/javase/tutorial/getStarted/application/arguments.html

Topics

#command-line arguments#main method#array access#ArrayIndexOutOfBoundsException

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice