1Z0-811 Java Basics and Syntax Practice Question
A developer is working on a Java application that processes user input. The application reads an integer from the command line using args[0], converts it to an int, and uses it in a loop. When testing, the application throws a NumberFormatException when the user provides an alphabetic string. The developer needs to handle this exception gracefully by prompting the user to enter a valid number and retrying. However, the developer must avoid infinite loops. The current code uses a while loop with a flag. Which approach ensures the code handles the exception, provides feedback, and terminates if the user enters 'quit'? The environment is a standard Java SE 11 application. The developer wants a robust solution without using external libraries.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use a try-catch inside a loop that continues if exception caught, and breaks if input equals 'quit' or conversion succeeds.
It uses a try-catch inside a while loop that continues if an exception is caught, allowing the user to re-enter input. The loop also checks if the input equals 'quit' to break out, ensuring the program can terminate gracefully. This approach provides feedback (prompting the user) and avoids infinite loops. Option B is incorrect because ignoring the exception would not provide feedback or allow retry. Option C is incorrect because it never checks for 'quit', risking an infinite loop. Option D is incorrect because calling System.exit(0) terminates the program abruptly, which is not graceful.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Use a try-catch inside a loop that continues if exception caught, and breaks if input equals 'quit' or conversion succeeds.
Why this is correct
Correct: handles exception, allows retry, and exits on 'quit'.
- ✗
Catch the exception and ignore it; the loop will naturally terminate.
Why it's wrong here
Ignoring exception leaves the input invalid; loop may continue indefinitely.
- ✗
Use a do-while loop that never checks for 'quit'; the exception is caught outside the loop.
Why it's wrong here
No exit condition; endless on invalid input.
- ✗
Catch the exception in the loop and call System.exit(0) immediately.
Why it's wrong here
Exits the entire application, not prompting for retry.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.