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);
}
}
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 1 of 3.
Harshad said:
4 years ago
Consider both if ( !b1 ) /* Line 7 */ if ( !b2 ) /* Line 9 */ are true.
Then b1 = true; gets executed //line11
And x++ makes x = 1 //initially x was 0;
alright 5 > 6 is false that's not going to execute anywhere,
Control will come at line no 17...if ( !b1 ) not b1 means false we have already consider b1 is true
then b1 not going to execute.
Yes, else if ( b2 = true ) bcoz we already consider b2 is true x = x + 100; gets executed 101
if one else if executed then control will not transfer to the 2nd else if.
That's why 101 gets printed!.
Then b1 = true; gets executed //line11
And x++ makes x = 1 //initially x was 0;
alright 5 > 6 is false that's not going to execute anywhere,
Control will come at line no 17...if ( !b1 ) not b1 means false we have already consider b1 is true
then b1 not going to execute.
Yes, else if ( b2 = true ) bcoz we already consider b2 is true x = x + 100; gets executed 101
if one else if executed then control will not transfer to the 2nd else if.
That's why 101 gets printed!.
(1)
Sujala said:
10 years ago
If(!b2) returns true.
It is similar to !(false).
In condition if (b2 = true) at this moment true is assigned to b2 and returns true value.
But we can't able to assign if (i = 5) //it is error.
At any instance if condition returns either true or false only.
So the statements.
x = x+100; //x = 1+100.
x = 101 is the right answer.
It is similar to !(false).
In condition if (b2 = true) at this moment true is assigned to b2 and returns true value.
But we can't able to assign if (i = 5) //it is error.
At any instance if condition returns either true or false only.
So the statements.
x = x+100; //x = 1+100.
x = 101 is the right answer.
(1)
Rakesh said:
1 decade ago
|| 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.
| is simple OR operator it will check its both the operands always.
Kunal said:
1 decade ago
Note the difference between b2= true and b==true. The first one assigns b2 to be true and evaluates as true, the second one compares b2 with true.
As the condition is true@ if(b2=true) it will executes the statement x=x+100; and skip the next statement.
As the condition is true@ if(b2=true) it will executes the statement x=x+100; and skip the next statement.
Devyansh Singla said:
10 years ago
@Boopathy according to my understanding.
If (5 > 6) false 5 is less than 6 so it will move to next if where b1 is already true above so it will move to last loop.
{
x++;
}
if ( !b1 )
x = x + 10;
If (5 > 6) false 5 is less than 6 so it will move to next if where b1 is already true above so it will move to last loop.
{
x++;
}
if ( !b1 )
x = x + 10;
Raman said:
1 decade ago
If (!b2) //If I change b2 to b1 then why execute this loop? Please
x = x+10;
else if (b1 = true) /*Line 19*/.
x = x + 100;
else if (b1 | b2) /*Line 21*/
x = x + 1000;
x = x+10;
else if (b1 = true) /*Line 19*/.
x = x + 100;
else if (b1 | b2) /*Line 21*/
x = x + 1000;
Rakesh Kumar said:
1 decade ago
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.
Because there must be an else statement after if{} else if{}.
Please some one do reply if I am wrong.
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
if(++z > 5 || ++z >6)
z++;
output : z=7
________________________
int z =5;
if(++z > 5 | ++z >6)
z++;
output : z=8
Udesha said:
10 years ago
What is the meaning of "if (!b1)"? What does it do?
Don't we have to use a condition inside those parentheses of the if condition?
Don't we have to use a condition inside those parentheses of the if condition?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers