1z0-809 Exam QuestionsBrowse all questions from this exam

1z0-809 Exam - Question 102


Given:

Your design requires that:

✑ fuelLevel of Engine must be greater than zero when the start() method is invoked.

✑ The code must terminate if fuelLevel of Engine is less than or equal to zero.

Which code fragment should be added at line n1 to express this invariant condition?

Show Answer
Correct Answer: CD

To ensure the `fuelLevel` of `Engine` is greater than zero when the `start()` method is invoked, and terminate the program if the `fuelLevel` is less than or equal to zero, the correct code fragment to use is: `assert fuelLevel > 0: "Impossible fuel";`. This assertion verifies the condition `fuelLevel > 0` and, if the condition is false, an `AssertionError` is thrown with the specified message, fulfilling the requirement to terminate when `fuelLevel` is less than or equal to zero.

Discussion

5 comments
Sign in to comment
maslacOption: D
Apr 4, 2021

Correct answer is D Condition must evaluate to false for execution of program to terminate

HanenBAOption: D
Feb 13, 2022

the correct code for assertion is assert (condition): "customized string message"; if condition is false then a runtime exception will be thrown with the customized string message correct answer is D

Eden0516Option: D
Mar 14, 2023

Correct answer is (D), tested

steefaandOption: D
Feb 6, 2024

D is correct, as assert takes boolean (eliminating a) and it has to evaluate to false in order for Error to be thrown.

DarGrinOption: D
May 30, 2024

Correct answer D