Assertions

Q1: 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 An AssertionError is thrown with the message 'assertion failed.'

B finished

C Compiliation fails.

D
An AssertionError is thrown and finished is output.

ANS:A - finished

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



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.