1 Show the output produced by the following A int x 2 y2 if
1) Show the output produced by the following:
A: int x = 2, y=2;
if( x++>2 || ++y < 3)
cout << x-y ;
else cout << -- y ;
A:This code segment will display a value of: ___
B: int x = 2, y=4;
if( ++x >3 || y++ <= 3)
cout << x*y ;
else cout<< y-x ;
B:This code segment will display a value of: __
C: int x = 2, y=2;
if( ++x >= 3 && y++ <= 3)
cout <
else cout << ++x ;
C: This code segment will display a value of: __
2) What value will the variable sum have after execution of the following code?
int x=3, y=-7, sum =1;
if (( y / x >) > -2 ) sum= x/y ; else sum =y/++x;
3) What value will the variable total have after execution of the following code?
int i = 1, total = 1,x=4 ;
while ( i < 4 ) {
total *= i +x;
--x;
i ++ ; }
Solution
1)Question A:
int x = 2, y=2;
if( x++>2 || ++y < 3)
cout << x-y ;
else cout << --y ;
Output:
2
why?
in the if condition ( x++>2 || ++y < 3) //( 3 > 2 || 3 < 3)
the condition fails and go to else statement and print --y; // 2
Question B:
int x = 2, y=4;
if( ++x >3 || y++ <= 3)
cout << x*y ;
else cout<< y-x ;
Output:
2
why?
in the if condition ( ++x >3 || y++ <= 3) //( 3 > 2 || 5 <= 3)
the condition fails and go to else statement and print y - x; // 5 - 3 => 2
Question c:
int x = 2, y=2;
if( ++x >= 3 && y++ <= 3)
cout < //incomplete code
else cout << ++x ;
Output:
error
This code is incomple. what ever it might be.
this code pass the if condtion ( ++x >= 3 && y++ <= 3) //( 3 >= 3 || 3 <= 3)
Question 2:
int x=3, y=-7, sum =1;
if (( y / x >) > -2 ) // error at here
sum= x/y ;
else
sum =y/++x;
I think you need to change this code if (( y / x >) > -2 ) to if (( y / x) > -2 )
Output:
-1
why?
The condition fails the if condition (( y / x) > -2 ) // (-7/3) > 2
executes the else sum = y/++x; // -7/4 => -1
Question 3:
this while loop execute 3 time . each time
1 * 5 = 5
5 * 5 = 25
25 * 5 = 125
Output:
125


