Assertions Questions and Answers

Practice Mode
Showing 10 of 45 questions
Q41
How to enable assertions for a specific class only?
  • A -ea:ClassName
  • B -ea ClassName
  • C -assert ClassName
  • D -enable ClassName
Answer: Option A
Explanation: The -ea:ClassName syntax enables assertions for specific classes.
Q42
What will happen if you use assertions in Java 1.3 without compatibility mode?
  • A Runtime exception
  • B Compilation error
  • C Warning message
  • D No effect
Answer: Option B
Explanation: assert was not a keyword before Java 1.4, so it would cause compilation errors.
Q43
Which of these correctly checks if a list is not null and not empty using assertion?
  • A assert list != null && list.size() > 0;
  • B assert list != null || list.size() > 0;
  • C assert list != null : "List is empty";
  • D assert list.size() > 0;
Answer: Option A
Explanation: The condition should verify both not null and not empty states.
Q44
What is the main advantage of using assertions over System.out.println for debugging?
  • A They execute faster
  • B They provide better error messages
  • C They can be disabled without code changes
  • D They work in all Java versions
Answer: Option C
Explanation: Assertions can be easily disabled in production without removing code.
Q45
Which Java version first introduced assertions?
  • A Java 1.2
  • B Java 1.3
  • C Java 1.4
  • D Java 5.0
Answer: Option C
Explanation: Assertions were officially added in Java 1.4 release.
Questions and Answers for Competitive Exams Various Entrance Test