Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 5)
5.
What will be the output of the program?
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);
    }
}
0
1
101
111
Answer: Option
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.

Discussion:
24 comments Page 3 of 3.

Saad said:   1 decade ago
If(b==true) is equivalent to if(b).

But if(b=true) first assigns true value to b and then check the condition.

Sam said:   1 decade ago
int z =5;
if(++z > 5 || ++z >6)
z++;
output : z=7
________________________
int z =5;
if(++z > 5 | ++z >6)
z++;
output : z=8

Chibhulpandey said:   1 decade ago
I think it is OR operator!! but actly or operator should be represented as"||". So please modify it in program.

Isha said:   1 decade ago
What is the meaning of operator | ? please do reply.


Post your comments here:

Your comments will be displayed after verification.