Skip to content
QuiznExam

Java Assertions Quiz – 20 Questions with Answers

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.

20
Questions
15 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which keyword is used to create an assertion in Java?

    • A. assert
    • B. assertion
    • C. check
    • D. verify
    Show answer

    A. assert

    Java uses the assert keyword to perform assertion checks.

  2. 2. Which syntax is valid for a Java assertion without a detail message?

    • A. assert condition;
    • B. assert(condition):
    • C. assert = condition;
    • D. check condition;
    Show answer

    A. assert condition;

    The basic Java assertion syntax is assert condition;.

  3. 3. Which syntax allows an assertion to include a detail message?

    • A. assert condition : message;
    • B. assert condition => message;
    • C. assert condition, message;
    • D. assert message : condition;
    Show answer

    A. assert condition : message;

    Java supports the form assert condition : expression;, where the expression provides a detail message if the assertion fails.

  4. 4. What happens when a Java assertion fails while assertions are enabled?

    • A. An AssertionError is thrown
    • B. A NullPointerException is always thrown
    • C. The assertion is silently ignored
    • D. The JVM automatically retries the assertion
    Show answer

    A. An AssertionError is thrown

    If an enabled assertion's condition evaluates to false, the JVM throws an AssertionError.

  5. 5. Are Java assertions enabled by default?

    • A. Yes, always
    • B. No, they are disabled by default
    • C. Only in development environments
    • D. Only when using an IDE
    Show answer

    B. No, they are disabled by default

    Java assertions are disabled by default and must be explicitly enabled when launching the JVM.

  6. 6. Which JVM option enables assertions when running a Java application?

    • A. -enableassertions
    • B. -ea
    • C. -assert
    • D. -enable
    Show answer

    B. -ea

    The -ea option is the short form of -enableassertions and enables assertions.

  7. 7. Which command-line option disables assertions?

    • A. -disableassertions
    • B. -da
    • C. -noassert
    • D. -disable
    Show answer

    B. -da

    The -da option, short for -disableassertions, disables assertions.

  8. 8. What is the type of the exception thrown when an enabled assertion fails?

    • A. RuntimeException
    • B. Exception
    • C. AssertionError
    • D. IllegalArgumentException
    Show answer

    C. AssertionError

    A failed enabled assertion results in an AssertionError.

  9. 9. Which statement is generally appropriate for an assertion?

    • A. assert age >= 0;
    • B. if (file.exists()) deleteFile();
    • C. if (user != null) loginUser();
    • D. while (connection.isOpen()) sendData();
    Show answer

    A. assert age >= 0;

    Assertions are intended for checking conditions that the programmer expects to always be true, such as an internal invariant.

  10. 10. Why should assertions generally not be used for validating required user input?

    • A. Assertions can be disabled
    • B. Assertions cannot contain Boolean expressions
    • C. Assertions only work with strings
    • D. Assertions always terminate the operating system
    Show answer

    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.

  11. 11. What value must the expression in an assert condition produce?

    • A. boolean
    • B. int
    • C. String
    • D. double
    Show answer

    A. boolean

    The condition used by the assert statement must be a boolean expression.

  12. 12. Consider: assert x > 0 : "x must be positive";. When does the message expression matter?

    • A. When the assertion condition is true
    • B. When the assertion condition is false and assertions are enabled
    • C. Only when assertions are disabled
    • D. Every time the statement executes
    Show answer

    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.

  13. 13. Which command enables assertions for a specific package and its subpackages?

    • A. -ea:com.example...
    • B. -ea com.example
    • C. -enable:com.example
    • D. -assert:com.example...
    Show answer

    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.

  14. 14. Which command enables assertions for a specific class?

    • A. -ea:com.example.TestClass
    • B. -ea com.example.TestClass
    • C. -assertclass com.example.TestClass
    • D. -enableclass com.example.TestClass
    Show answer

    A. -ea:com.example.TestClass

    The JVM supports enabling assertions for a specific class using the -ea:class-name form.

  15. 15. What happens when assertions are disabled and an assert statement is executed?

    • A. The condition is treated as false
    • B. The assertion has no effect
    • C. An AssertionError is always thrown
    • D. The program cannot compile
    Show answer

    B. The assertion has no effect

    When assertions are disabled, the assert statement is inactive and does not perform the assertion check.

  16. 16. Which statement about the assert keyword is correct?

    • A. It can be used as an identifier in all Java source versions
    • B. It is a Java keyword
    • C. It is a class in java.lang
    • D. It is a method of Object
    Show answer

    B. It is a Java keyword

    assert is a Java language keyword introduced for assertion support.

  17. 17. Which of the following is a suitable use of an assertion?

    • A. Checking an internal class invariant
    • B. Checking whether a required file exists before every production operation
    • C. Validating a password entered by a user
    • D. Checking whether a network request succeeded
    Show answer

    A. Checking an internal class invariant

    Assertions are well suited to checking internal assumptions and invariants that should hold during normal program execution.

  18. 18. What does the following assertion check? assert count >= 0;

    • A. That count is negative
    • B. That count is zero or positive
    • C. That count is exactly zero
    • D. That count is greater than zero
    Show answer

    B. That count is zero or positive

    The expression count >= 0 evaluates to true when count is zero or any positive value.

  19. 19. Which is a potential problem with placing a method call that has side effects inside an assertion?

    • A. The method may not execute when assertions are disabled
    • B. The method is always called twice
    • C. The method cannot return a value
    • D. The compiler converts it into a constructor
    Show answer

    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.

  20. 20. Which statement best describes the intended role of Java assertions?

    • A. To replace all exception handling
    • B. To document and check programmer assumptions during development and testing
    • C. To validate every external input
    • D. To guarantee that an application never fails
    Show answer

    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.