Arithmetic Worksheet For each of the following assignment statements, show the v
ID: 3785645 • Letter: A
Question
Arithmetic Worksheet For each of the following assignment statements, show the value that will be stored in the variable on the left hand side. Assume that you are given the following declarations: int num; float val; 1. num = 17 / 2 + 4; 2. val = 17 / 2 + 4; 3. num = 17 / 2.0 + 4; 4. val = 17 / 2.0 + 4; 5. num = 11 % 6 / 2 - 1; 6. val = 11 % 6 / 2.0 - 1; 7. num = 3 - (5 + 10 / (2 * 2)); 8. val = 3 - (5 + 10 / (2 * 3.0)); 9. val = 4.5 * (5 - 3); num = val; 10. num = 15 / 2 % 3 - 1; 11. num = 7 * 2 - 5 / 3; val = num; Assume that total, num1, and num2 are int variables, with total = 2, num1 = 3, and num2 = 4. Show the new values of all variables changed by each of the following: 12. total = ++num1; 13. total = num2++; 14. total *= 5; 15. total += num1 - num2 * 4; 16. total = ++num1 + num2++; 17. total += 3 * num1++ - 5;
Explanation / Answer
order of precedence of arithmatic operators is
multiplication
division
modulas
addition
subtraction
1)12
2)12
3)12
4)12.5
5)1
Reason:
num = 11 % 6 / 2 - 1;
=11%3-1
=2-1
num=1
6)1.5
7)-4
Reason:
num = 3 - (5 + 10 / (2 * 2))
=3 -( 5 + 10 / 4 )
=3 - ( 5 + 2 )
=3 - 7
= - 4
8) - 3.66667
9) 9
10) 0
Reason:
num = 15 / 2 % 3 - 1;
= 7 % 3 - 1
= 1-1
num = 0
11)13
12)4
Reason:
total = ++num1;
here total = ++num1 is preincrementation .First incrementation happens so num1 become 4
after that assignment will happen so 4 is assigned to total.So total=4
13)4
Reason:
total = num2++;
here in total= num2++; Here num2++ is post incrementation.So first assgnment will happen
after that incrementation will happen.
so num2 value(which is 4) is assigned to total.So total=4
14) 10
total*=5 means total=total*5= 2*5=10
so total =10
15) -11
Reason:
total += num1 - num2 * 4;
total=total+num1-num2 * 4
=2 + 3 - 4 * 4
=2 + 3 - 16
=5 - 16
= - 11
16) 8
Reason:
total = ++num1 + num2++;
++num1 is preincrementation so num1 value become 4
num2++ is post incrementation so it will not be incremented initially.so
total = 4 + 4
total=8
17) 6
Reason:
total += 3 * num1++ - 5;
total=total + 3 * num1++ -5
= 2 + 3 * 3 - 5
= 2 + 9 - 5
= 11 - 5
= 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.