Exceptions in Java Quiz – 15 Questions with Answers
15 questions · 12 min · Medium
Test your knowledge of assertions in Java with 20 questions covering assert syntax, enabling and disabling assertions, AssertionError, command-line options, class-level assertions, conditional checks, and common use cases.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. assert
Java uses the assert keyword to perform assertion checks.
A. assert condition;
The basic Java assertion syntax is assert condition;.
A. assert condition : message;
Java supports the form assert condition : expression;, where the expression provides a detail message if the assertion fails.
A. An AssertionError is thrown
If an enabled assertion's condition evaluates to false, the JVM throws an AssertionError.
B. No, they are disabled by default
Java assertions are disabled by default and must be explicitly enabled when launching the JVM.
B. -ea
The -ea option is the short form of -enableassertions and enables assertions.
B. -da
The -da option, short for -disableassertions, disables assertions.
C. AssertionError
A failed enabled assertion results in an AssertionError.
A. assert age >= 0;
Assertions are intended for checking conditions that the programmer expects to always be true, such as an internal invariant.
A. Assertions can be disabled
Because assertions are disabled by default and can be disabled at runtime, they should not replace normal input validation or required application checks.
A. boolean
The condition used by the assert statement must be a boolean expression.
B. When the assertion condition is false and assertions are enabled
The detail expression is evaluated when the assertion fails while assertions are enabled and its value is used in the resulting AssertionError.
A. -ea:com.example...
The -ea option can be followed by a package name and three dots to enable assertions for that package and its subpackages.
A. -ea:com.example.TestClass
The JVM supports enabling assertions for a specific class using the -ea:class-name form.
B. The assertion has no effect
When assertions are disabled, the assert statement is inactive and does not perform the assertion check.
B. It is a Java keyword
assert is a Java language keyword introduced for assertion support.
A. Checking an internal class invariant
Assertions are well suited to checking internal assumptions and invariants that should hold during normal program execution.
B. That count is zero or positive
The expression count >= 0 evaluates to true when count is zero or any positive value.
A. The method may not execute when assertions are disabled
Code inside an assertion should not be relied upon for required side effects because the assertion may be disabled.
B. To document and check programmer assumptions during development and testing
Assertions are intended primarily to check assumptions and invariants made by programmers, especially during development and testing.