1. int a = 10, b, c, d; a. compute d = b = c = a b. compute d = a / 3 c. compute
ID: 3855393 • Letter: 1
Question
1. int a = 10, b, c, d;
a. compute d = b = c = a
b. compute d = a / 3
c. compute d = a%3
2. int a = 10, b;
a. compute b = a/3
3. What is y displayed in the following code?
#include <iostream> using namespace std;
int main()
{ int x = 1; int y = x = x - 1;
cout << "y is " << y;
return 0;
}
4. int a =10, b = 20; c = 10, d;
a. compute d = (++a) + b – c
b. compute d = a – (b--) + (++c)
5. int a = 10, b = 11, c = 167, d; (Take 8-bit binary representation)
a. compute d = a&b
b. compute d = a|(b&c)
c. compute d = (a==b)
d. compute d = (a>=b)
e. compute d = ((a&b)|c)
f. compute d = (a&(~b))
Explanation / Answer
1. int a = 10, b, c, d;
a. ) compute d = b = c = a
a is initialized with value 10. d = b = c = a ....this statement will assign value to all the variables c,b,d with the same value of a. So after execution of this line the value of d, c and b would be 10. Assignment operator (=) is right associative. So first value of a (which is 10) is assigned to c; then value of c would be assigned to b and then value of b would be assigned to d; Hence in this chaining assignment statement variables c, b and d would be assigned with value 10.
b. ) compute d = a / 3
The value of a is 10. Division operator (/) gives only the integer quotient part if both operands are integers. Here both operands are integer 10 and 3; so 10/3 gives quotient 3 as a result; hence after computation the value of the variable d would be 3.
c. ) compute d = a%3
The value of a is 10. Modulus operator (%) gives the remainder part in dividing a number by another number; 10%3 gives remainder 1; hence after executing of the above said operation the value of the variable d would be 1.
2. int a = 10, b;
a.) compute b = a/3
The value of a is 10. Division operator (/) gives only the integer quotient part if both operands are integers. Here both operands are integer 10 and 3; so 10/3 gives quotient 3 as a result; hence after computation the value of the variable b would be 3.
3. Reference code:
#include <iostream>
using namespace std;
int main()
{ int x = 1;
int y = x = x - 1;
cout << "y is " << y;
return 0;
}
Executing the above code, y will be displayed 0.
Explanation: x was initialized with value 1; in the declaring statement of y it is initialized with the value x which is at that point subtracted by 1. (ref. line of code: int y = x = x - 1;) ; Assignment operator (=) is right associative. So first x=x-1 would execute, then y=x would be executed. Hence at this point the value of x is (1-1) =0; So, y would be initialized with value 0. Hence 0 would be displayed as the value of y.
4. int a =10, b = 20; c = 10, d;
a.) compute d = (++a) + b – c
a=10; (++a) means pre increment operator is acted upon a; i.e. first the value of a is increased by 1 then that increased value would be used in further operation, So the value of a would become 11 now and this value would take part in the computation. b = 20; and c=10;
Hence the expression d = (++a) + b – c = 11 + 20 – 10 = 21; So after computation the result would be d=21.
b.) compute d = a – (b--) + (++c)
a=10;
b=20; (b--) means post decrement operator is acted upon b; i.e. first the initial value of b would take part in the computation then the value of b would be decremented by 1. So the value of b would become 19 after execution of this line but b = 20 would take part in this line of computation.
c=10; (++c) means pre increment operator is acted upon c; i.e. first the value of c is increased by 1 then that increased value would be used in further operation, So the value of c would become 11 now and this value would take part in the computation.
Hence the expression d = a – (b--) + (++c) = 10 - 20 + 11 = 1; So after computation the result would be d=1.
5. int a = 10, b = 11, c = 167, d; (Take 8-bit binary representation)
a.)compute d = a&b
& is Bitwise AND operator.
a = 10 = 00001010 (In Binary)
b = 11 = 00001011 (In Binary)
(a&b) = Bit Operation of 10 and 11 = 00001010 & 00001011 = 00001010 = 10 (In decimal)
After computation d would be = 10;
b. )compute d = a|(b&c)
& is Bitwise AND operator.
b = 11 = 00001011 (In Binary)
c = 167 = 10100111 (In Binary)
(b&c) =Bit Operation of 11 and 167 = 00001011 & 10100111 = 00000011 = 3 (In decimal)
(b&c)=3;
| is Bitwise OR operator.
a = 10 = 00001010 (In Binary)
(b&c) = 3 = 00000011 (In Binary)
a | (b&c) = Bit Operation of 10 and 3 = 00001010 | 00000011 = 00001011 = 11 (In decimal)
a | (b&c) =11;
Hence after computation d = a|(b&c) = 11;
c.) compute d = (a==b)
Equality operator (==) compares its operands whether they are equal or not. If both operands are equal then returns 1 otherwise 0; Here a and b are different value so (a==b) returns 0; Hence d would contain 0. So after computation d = 0;
d.) compute d = (a>=b)
Greater than or equal to operator (>=) compares its operands whether left side operand is greater than or equal to the right side operand or not. If left side operand is greater than or equal to the right side operand then returns 1 otherwise 0; Here a =10 and b =11; so a is not greater than b; so (a>=b) returns 0; Hence d would contain 0. So after computation d = 0;
e.) compute d = ((a&b)|c)
& is Bitwise AND operator.
a = 10 = 00001010 (In Binary)
b = 11 = 00001011 (In Binary)
(a&b) = Bit Operation of 10 and 11 = 00001010 & 00001011 = 00001010 = 10 (In decimal)
| is Bitwise OR operator.
(a&b) = 10 = 00001010 (In Binary)
c = 167 = 10100111 (In Binary)
(a&b) | c = Bit Operation of 10 and 167 = 00001010 | 10100111 = 10101111 = 175 (In decimal)
((a&b) | c) =175;
Hence after computation d = ((a&b) | c) =175;
f. ) compute d = (a&(~b))
a = 10 = 00001010 (In Binary)
b = 11 = 00001011 (In Binary)
~ is Bitwise NOT operator;
(~b) = 11110100 ;
a&(~b) = 00001010 & 11110100 = 00000000 = 0(in decimal)
Hence after computation d = (a&(~b)) = 0;
/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.