In: Computer Science
i
What values would be stored in the given variables in each case?
a. int n = 12 % 5;
b. double x = 15 % 11 + 5.3 - 5 / (2.5 - 0.3);
c. float y = 2 / (3.5 + static_cast<int>(3.5));
d. bool z = (6 – 7 <= 2 * 1) && (5 + 4 >= 3) || (6 + 2 != 17 – 3 * 10);
a.
int n = 12 % 5;
Here n contain the reminder of 12 / 5 operation.
Since reminder is 2, n=2
Therefore the value of n is 2.
b.
double x = 15 % 11 + 5.3 - 5 / (2.5 - 0.3);
It is evaluated based on the priority order.
() has highest priority so it is evaluated first.
x = 15 % 11 + 5.3 - 5 / 2.2;
% and / has next priority so evaluate it.
x = 4 + 5.3 - 2.27273;
x = 7.02727
Therefore the value of x is 7.02727
c.
float y = 2 / (3.5 + static_cast<int>(3.5));
static_cast<int>(3.5) // It change the value to integer so the value of 3.5 is changed to 3
float y = 2 / (3.5 + static_cast<int>(3.5));
float y = 2 / (3.5 + 3);
float y = 2 / (6.5);
float y = 0.307692
Therefore the value of y is 0.307692
d.
bool z = (6 - 7 <= 2 * 1) && (5 + 4 >= 3) || (6 + 2 != 17 - 3 * 10);
z = ( - 1 <= 2) && (9>= 3) || (8 != 17 - 30); // evaluate 3*10 first because multiplication has high priority
z = ( - 1 <= 2 * 1) && (9>= 3) || (8 != 13);
z = ( true) && (true) || (true);
z = true
Therefore the value of z is true.