Assertions Questions and Answers

Practice Mode
Showing 10 of 45 questions
Q31
What is the two-form syntax of assert statement?
  • A Simple and complex
  • B Basic and advanced
  • C With and without message
  • D Conditional and unconditional
Answer: Option C
Explanation: Assert can have just a boolean expression or boolean expression followed by a message.
Q32
When should assertions typically be used?
  • A For validating user input
  • B For checking method arguments in public methods
  • C For internal invariants and debugging
  • D For handling file I/O errors
Answer: Option C
Explanation: Assertions are for internal invariants and should not replace regular error handling.
Q33
Which of these is NOT a valid use of assertions?
  • A Checking internal class invariants
  • B Validating input parameters in private methods
  • C Checking method arguments in public methods
  • D Verifying postconditions after method execution
Answer: Option C
Explanation: Assertions should not be used for argument checking in public methods.
Q34
What happens if assertions are disabled?
  • A They throw exceptions
  • B They are evaluated but ignored
  • C They have no effect
  • D They cause compilation warnings
Answer: Option C
Explanation: When disabled, assert statements have no effect and are essentially skipped.
Q35
Which command enables assertions for all classes except those in com.example package?
  • A -ea -da:com.example...
  • B -ea:* -da:com.example...
  • C -ea:-com.example...
  • D -enable -disable:com.example...
Answer: Option C
Explanation: The -ea:-com.example... syntax disables assertions for specific packages.
Q36
What type of exception is thrown when an assertion fails?
  • A AssertionException
  • B RuntimeException
  • C AssertionError
  • D AssertFailure
Answer: Option C
Explanation: AssertionError extends Error, not Exception.
Q37
Which of these is a correct assertion statement with message?
  • A assert condition : "message";
  • B assert condition "message";
  • C assert ("message") condition;
  • D assert : condition "message";
Answer: Option A
Explanation: The message follows the boolean expression after a colon.
Q38
Why should assertions not have side effects?
  • A They make code run slower
  • B They can cause different behavior when assertions are disabled
  • C They are illegal in Java
  • D They improve performance
Answer: Option B
Explanation: Since assertions can be disabled, side effects would create inconsistent behavior.
Q39
What is the default state of assertions when running a Java program?
  • A Enabled for all classes
  • B Disabled for all classes
  • C Enabled only for system classes
  • D Enabled only for user classes
Answer: Option B
Explanation: Assertions are disabled by default for backward compatibility.
Q40
Which package contains the AssertionError class?
  • A java.util
  • B java.lang
  • C java.assert
  • D java.error
Answer: Option B
Explanation: AssertionError is in java.lang package like other fundamental classes.
Questions and Answers for Competitive Exams Various Entrance Test