Assertions Questions and Answers
Assertions questions with answers in Java programming test a candidate’s ability to debug and validate logic efficiently. The assert keyword is used to verify assumptions during program execution, ensuring code reliability and preventing logical errors. Many placement tests from companies like TCS, Infosys, and Wipro include Java Assertion MCQs to evaluate coding fundamentals. This section offers a collection of Java programming questions and answers with explanations to help you understand the use, syntax, and runtime behavior of assertions. Practice these questions to strengthen your programming logic and prepare effectively for technical interviews and coding tests.
Assertions
Showing 10 of
45 questions
1. What will be the output of the program? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) ? "assertion failed" : "assertion passed" ; System.out.println("finished"); } }
- finished
- Compiliation fails.
- An AssertionError is thrown and finished is output.
- An AssertionError is thrown with the message "assertion failed."
2. public class Test { public void foo() { assert false; /* Line 5 */ assert false; /* Line 6 */ } public void bar() { while(true) { assert false; /* Line 12 */ } assert false; /* Line 14 */ } } What causes compilation to fail?
- Line 5
- Line 6
- Line 12
- Line 14
3. What will be the output of the program? public class Test { public static int y; public static void foo(int x) { System.out.print("foo "); y = x; } public static int bar(int z) { System.out.print("bar "); return y = z; } public static void main(String [] args ) { int t = 0; assert t > 0 : bar(7); assert t > 1 : foo(8); /* Line 18 */ System.out.println("done "); } }
- bar
- bar done
- foo done
- Compilation fails
4. What will be the output of the program (when you run with the -ea option) ? public class Test { public static void main(String[] args) { int x = 0; assert (x > 0) : "assertion failed"; /* Line 6 */ System.out.println("finished"); } }
- finished
- Compilation fails.
- An AssertionError is thrown.
- An AssertionError is thrown and finished is output.
5. public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); } } which line is an example of an inappropriate use of assertions?
- Line 11
- Line 12
- Line 14
- Line 22
6. What is the primary purpose of assertions in Java?
- To handle runtime exceptions
- To improve code performance
- To validate assumptions during development
- To replace exception handling
7. Which keyword is used to declare an assertion in Java?
- assertion
- assert
- Assert
- validate
8. What happens by default when an assertion fails at runtime?
- Compilation error occurs
- AssertionError is thrown
- NullPointerException is thrown
- Program continues execution silently
9. How are assertions enabled at runtime?
- Using -enableassertions or -ea
- Using -enableassertions=true in code
- They are always enabled by default
- Using @EnableAssertions annotation
10. Which of the following is the correct syntax for assert statement?
- assert condition;
- assert(condition);
- Assert condition;
- assert condition : message;