Assertions Questions and Answers

Practice Mode
Showing 10 of 45 questions
Q21
Can assertions be enabled for specific packages or classes?
  • A No, only globally
  • B Yes, using -ea:package or -ea:class
  • C Only through code annotations
  • D Only for system classes
Answer: Option B
Explanation: Yes, using -ea:package or -ea:class syntax.
Q22
Which of these is a valid assert statement?
  • A assert x = 5;
  • B assert x > 0;
  • C assert System.out.println("test");
  • D assert null;
Answer: Option B
Explanation: Assert requires boolean expression, not assignment or method calls returning void.
Q23
What is the inheritance hierarchy of AssertionError?
  • A Object -> Throwable -> Exception -> AssertionError
  • B Object -> Throwable -> Error -> AssertionError
  • C Object -> Error -> AssertionError
  • D Object -> Exception -> AssertionError
Answer: Option B
Explanation: AssertionError extends Error, which extends Throwable.
Q24
Why are assertions disabled by default?
  • A They are experimental feature
  • B To avoid performance overhead in production
  • C They cause security issues
  • D Java specifications require it
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?
  • A Code becomes more robust
  • B Potential bugs may occur if assertions disabled
  • C Compilation warning appears
  • D Automatic conversion to if-check
Answer: Option B
Explanation: Assertions should not replace necessary runtime checks.
Q26
What is the primary purpose of assertions in Java?
  • A To handle runtime exceptions
  • B To improve program performance
  • C To document code for other developers
  • D To verify assumptions during development
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?
  • A assert
  • B assertion
  • C Assert
  • D verify
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?
  • A Compilation error occurs
  • B AssertionError is thrown
  • C Program continues silently
  • D NullPointerException is thrown
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?
  • A assert (condition);
  • B assert condition;
  • C assert condition
  • D assert: condition;
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?
  • A -enableassertions or -ea
  • B -assert
  • C -enable
  • D -assertions
Answer: Option A
Explanation: The -ea flag enables assertions; -da disables them.
Questions and Answers for Competitive Exams Various Entrance Test