Showing posts with label Assertions. Show all posts
Showing posts with label Assertions. Show all posts

Thursday, 9 February 2012

Frequently Asked Questions in Technical Round at MNCs like TCS, WIPRO, INFOSYS,..etc – 8

Predict the output or error(s) for the following:


1)

int i,j;

for(i=0;i<=10;i++)

{

j+=5;


assert(i<5);


}

Answer:

Runtime error: Abnormal program termination.


assert failed (i<5), <file name>,<line number>


Explanation:

Saturday, 5 November 2011

Sun Certified Java Programmer (SCJP)-7

Assertions


An assertion is a statement containing a boolean expression that is assumed to be true when the statement is executed. The system reports an AssertionError if the expression evaluates to false. It is used for debugging purposes:

assert(a > 0); // throws an AssertionError if a <= 0



Assertions can be in two forms:


assert Expression1 ;
assert Expression1 : Expression2 ;


Expression1 should always result in a boolean value.

Expression2 can be anything that results in a value. The value is