What is the output of the following code Assume all other ne

What is the output of the following code? Assume all other necessary and relevant C++ code is present in the program, it’s just not shown below.

if( x == 5 ){

cout << “Alpha”; }

else if( x * y == 15 ){ cout << “Bravo”; }

else if( x++ == 3){ cout << “Charlie”; }

else if( ++x == 3 ){ cout << “Delta”; }

else{ cout << “Echo”; }

a. When x = 3, y = 5

b. When x = 3, y = 8

c. When x = 15, y = 2

d. When x = 5, y = 3

e. When x = 2, y = 20

f. When x = 1, y = 5

Solution

a) when x=3 and y=5 then the output is Bravo because for x*y the value is 3*5=15 which obeys the first elseif condition and executes the cout statement which contains Bravo.

b) when x=3 and y=8 then the output is Charlie because in the first elseif condition is checks for x*y = 3*8=24 which doesn’t obeys the first elseif condition and goes to the next elseif condition then in this condition is x++==3 which obeys (x=3) then it executes the block of statements so the output is Charlie.

c) when x=15 and y=2 then the output is Echo because it doesn’t get satisfy any of the conditions so then it goes to the else condition then prints the block of statements contain in else block so it provides the output as Echo.

d)when x=5 and y=3 then the output is Alpha because it satisfies the if condition (x=5 ) then it executes the block of statements present in it so the output is Alpha.

e) when x=2 and y=20 then the output is Echo because it doesn’t get satisfy any of the conditions so then it goes to the else condition then prints the block of statements contain in else block so it provides the output as Echo.

f) when x=1 and y=5 then the output is Delta because it first checks the second elseif condition where x++=3 it is nothing but the post increment value by 1 then the value executes after checking is x=2 then it goes to the next elseif condition where condition is ++x which means pre-increment so now x=2 +1=3; (because previously x is incremented by 1 and now x is pre-incremented) so it obeys the third elseif condition then it executes the block of statements so the output is Delta.

What is the output of the following code? Assume all other necessary and relevant C++ code is present in the program, it’s just not shown below. if( x == 5 ){ c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site