public class If2
{
static boolean b1, b2;
public static void main(String [] args)
{
int x = 0;
if ( !b1 ) /* Line 7 */
{
if ( !b2 ) /* Line 9 */
{
b1 = true;
x++;
if ( 5 > 6 )
{
x++;
}
if ( !b1 )
x = x + 10;
else if ( b2 = true ) /* Line 19 */
x = x + 100;
else if ( b1 | b2 ) /* Line 21 */
x = x + 1000;
}
}
System.out.println(x);
}
}
[A].
0
[B].
1
[C].
101
[D].
111
Answer: Option D
Explanation:
As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped.
I think it is OR operator!! but actly or operator should be represented as"||". So please modify it in program.
Sam said:
(Sun, Nov 11, 2012 01:12:20 PM)
int z =5;
if(++z > 5 || ++z >6)
z++;
output : z=7
________________________
int z =5;
if(++z > 5 | ++z >6)
z++;
output : z=8
Saad said:
(Wed, Jan 30, 2013 11:03:48 PM)
If(b==true) is equivalent to if(b).
But if(b=true) first assigns true value to b and then check the condition.
Rakesh Kumar said:
(Tue, Feb 5, 2013 12:39:45 PM)
How it became possible for this code to run.
Because there must be an else statement after if{} else if{}.
Please some one do reply if I am wrong.
Rakesh said:
(Tue, Feb 5, 2013 12:44:52 PM)
|| is called as short circuit OR operator, it will check its 2nd operand only if the first operand is false and if the first operand is true then it will not care about whether 2nd operand is true or false.
| is simple OR operator it will check its both the operands always.