Assertions Questions and Answers
Practice ModeShowing 10 of 45 questions
Q21
Can assertions be enabled for specific packages or classes?
Answer: Option B
Explanation: Yes, using -ea:package or -ea:class syntax.
Q22
Which of these is a valid assert statement?
Answer: Option B
Explanation: Assert requires boolean expression, not assignment or method calls returning void.
Q23
What is the inheritance hierarchy of AssertionError?
Answer: Option B
Explanation: AssertionError extends Error, which extends Throwable.
Q24
Why are assertions disabled by default?
Answer: Option B
Explanation: Assertions are disabled to avoid performance overhead in production.
Q25
What happens if you use assertions in a place that should always be checked?
Answer: Option B
Explanation: Assertions should not replace necessary runtime checks.
Q26
What is the primary purpose of assertions in Java?
Answer: Option D
Explanation: Assertions are used for debugging and testing during development to verify assumptions about program state.
Q27
Which keyword is used to declare an assertion in Java?
Answer: Option A
Explanation: The assert keyword was introduced in Java 1.4 specifically for assertions.
Q28
What happens by default when an assertion fails at runtime?
Answer: Option B
Explanation: A failed assertion throws an AssertionError, which is unchecked and typically terminates the program.
Q29
Which of the following is the correct syntax for a simple assertion?
Answer: Option B
Explanation: The assert keyword followed by a boolean expression is the basic syntax.
Q30
How do you enable assertions when running a Java program?
Answer: Option A
Explanation: The -ea flag enables assertions; -da disables them.