Java Programming - Assertions

Why should I learn to solve Java Programming questions and answers section on "Assertions"?

Learn and practise solving Java Programming questions and answers section on "Assertions" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.

Where can I get the Java Programming questions and answers section on "Assertions"?

IndiaBIX provides you with numerous Java Programming questions and answers based on "Assertions" along with fully solved examples and detailed explanations that will be easy to understand.

Where can I get the Java Programming section on "Assertions" MCQ-type interview questions and answers (objective type, multiple choice)?

Here you can find multiple-choice Java Programming questions and answers based on "Assertions" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.

How do I download the Java Programming questions and answers section on "Assertions" in PDF format?

You can download the Java Programming quiz questions and answers section on "Assertions" as PDF files or eBooks.

How do I solve Java Programming quiz problems based on "Assertions"?

You can easily solve Java Programming quiz problems based on "Assertions" by practising the given exercises, including shortcuts and tricks.

Exercise : Assertions - Finding the output
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."
Answer: Option
Explanation:

Compilation Fails. You can't use the Assert statement in a similar way to the ternary operator. Don't confuse.


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
Answer: Option
Explanation:

Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert.

Examine the following:

A while statement can complete normally if and only if at least one of the following is true:

- The while statement is reachable and the condition expression is not a constant expression with value true.

-There is a reachable break statement that exits the while statement.

The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code:

public class Test80 
{ 
    public void foo() 
    {
        assert false; 
        assert false; 
    } 
    public void bar()
    {
        while(true)
        {
            assert false; 
            break; 
        } 
        assert false;  
    } 
}


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
Answer: Option
Explanation:

The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.


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.
Answer: Option
Explanation:

An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option)

Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.


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
Answer: Option
Explanation:

Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false.

Option A is fine; a second expression in an assert statement is not required.

Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement.

Option C is fine because it is proper to call an assert statement conditionally.