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.
Question
Given:
And the commands:
Javac Test.java Java Test 12345 What is the result?
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)- A13% (7)
- B7% (4)
- C4% (2)
- D76% (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.
The output "Number us : 12345" would only occur if the program successfully accessed and printed `args[0]` without an exception, which contradicts the `ArrayIndexOutOfBoundsException`.
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.
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.
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
Community Discussion
No community discussion yet for this question.
