Java Programming - Assertions

Why Java Programming Assertions?

In this section you can learn and practice Java Programming Questions based on "Assertions" and improve your skills in order to face the interview, competitive examination and various entrance test (CAT, GATE, GRE, MAT, Bank Exam, Railway Exam etc.) with full confidence.

Where can I get Java Programming Assertions questions and answers with explanation?

IndiaBIX provides you lots of fully solved Java Programming (Assertions) questions and answers with Explanation. Solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Java Programming Assertions quiz questions with answers as PDF files and eBooks.

Where can I get Java Programming Assertions Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type Java Programming Assertions questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve Java Programming Assertions problems?

You can easily solve all kind of Java Programming questions based on Assertions by practicing the objective type exercises given below, also get shortcut methods to solve Java Programming Assertions problems.

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");  
    } 
}

A. finished
B. Compiliation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message "assertion failed."

Answer: Option B

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?

A. Line 5
B. Line 6
C. Line 12
D. Line 14

Answer: Option D

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 ");
    }
}

A. bar
B. bar done
C. foo done
D. Compilation fails

Answer: Option D

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"); 
    } 
}

A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is output.

Answer: Option C

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?

A. Line 11
B. Line 12
C. Line 14
D. Line 22

Answer: Option D

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.



Have a question?

Ask your question now !

Create your own website ?

Web Hosting Services in India