Assertions Questions and Answers
Practice ModeShowing 10 of 45 questions
Q31
What is the two-form syntax of assert statement?
Answer: Option C
Explanation: Assert can have just a boolean expression or boolean expression followed by a message.
Q32
When should assertions typically be used?
Answer: Option C
Explanation: Assertions are for internal invariants and should not replace regular error handling.
Q33
Which of these is NOT a valid use of assertions?
Answer: Option C
Explanation: Assertions should not be used for argument checking in public methods.
Q34
What happens if assertions are disabled?
Answer: Option C
Explanation: When disabled, assert statements have no effect and are essentially skipped.
Q35
Which command enables assertions for all classes except those in com.example package?
Answer: Option C
Explanation: The -ea:-com.example... syntax disables assertions for specific packages.
Q36
What type of exception is thrown when an assertion fails?
Answer: Option C
Explanation: AssertionError extends Error, not Exception.
Q37
Which of these is a correct assertion statement with message?
Answer: Option A
Explanation: The message follows the boolean expression after a colon.
Q38
Why should assertions not have side effects?
Answer: Option B
Explanation: Since assertions can be disabled, side effects would create inconsistent behavior.
Q39
What is the default state of assertions when running a Java program?
Answer: Option B
Explanation: Assertions are disabled by default for backward compatibility.
Q40
Which package contains the AssertionError class?
Answer: Option B
Explanation: AssertionError is in java.lang package like other fundamental classes.