I Given the following code segment in C what is the value of
I. Given the following code segment in C+ what is the value of after executing this segment x1 int x1, x2; x 2-3; x2 x1; x1+-+ a. 5 b. 7 c. 3 d. 8 2. Given that both x and y are integers, what is the output of the arithmetic expression x/y? Assume that value of x is 9 and y is 2. a. 4 b. 5 c. 4.5 d. 4 h 3. Given the following code segment in C++, what is the value of x1 after executing this segment: int x1, x2; x1 x1 a. 6 b. 7 c. 3 d. 2
Solution
(a). In the first program:
x1=4, x2=3;
When the expression x2+=x1, it means that x2=x2+x1 i.e., x2=4+3=7.
When the expression says x1++ it means x1=x1+1 i.e., x1=4+1=5
You can check for other integers:
when x1=6,x2=5
When followd above steps, x1=11,x2=5+1=6.
(b). Since, x and y are integers
x=9 and y=2
x/y should result to 4.5.
But, Since x and y are integers x/y is 4, 4.5 is truncated to 4.
(c) In the third Program:
x1=4, x2=3;
When x1+=x2 it means that x1=x1+x2 i.e., x1=4+3=7.
When the next expression is --x1 i.e, x1=x1-1 therefore, x1=7-1=6.
You can check for other expressions and try it out.
