Assertions Questions and Answers
Practice ModeShowing 10 of 45 questions
Q11
When should assertions typically be used?
Answer: Option C
Explanation: Assertions are for internal invariants and debugging, not for public method parameter validation.
Q12
What is the type of exception thrown by failed assertions?
Answer: Option C
Explanation: AssertionError extends Error, not Exception.
Q13
Which command disables assertions?
Answer: Option A
Explanation: -da or -disableassertions disables assertions.
Q14
What happens if assertions are disabled?
Answer: Option B
Explanation: When disabled, assert statements have no effect at runtime.
Q15
Which Java version introduced assertions?
Answer: Option C
Explanation: Assertions were introduced in Java 1.4.
Q16
What is the main advantage of using assertions over regular if-checks?
Answer: Option B
Explanation: Assertions can be disabled in production, removing performance overhead.
Q17
Which of these is NOT appropriate for assertions?
Answer: Option B
Explanation: User input validation should use regular exception handling, not assertions.
Q18
What will happen if you try to catch AssertionError?
Answer: Option C
Explanation: While technically possible, catching AssertionError is generally not recommended.
Q19
Which package contains AssertionError?
Answer: Option B
Explanation: AssertionError is in java.lang package.
Q20
What is the output of: assert false : "Error message"; when assertions enabled?
Answer: Option B
Explanation: AssertionError with the specified message is thrown.