Unit 1 Progress Check: MCQ Part B
double d = 0.25; int i = 3; double diff = d - i; System.out.print((int)diff - 0.5);
-2.5
double p = 10.6; double n = -0.2; System.out.println((int) (p + 0.5)); System.out.print((int) (n - 0.5));
11 0
int j = 10; int k = 8; j += 2; k += j; System.out.print(j); System.out.print(" "); System.out.println(k);
12 20
double a = 7; int b = (int) (a / 2); double c = (double) b / 2; System.out.print(b); System.out.print(" "); System.out.print(c);
3 1.5
int num = 5; num *= 2; num %= 6;
4
int x = 0; x++; x += 1; x = x + 1; x -= -1; System.out.println(x);
4
int x = 4; int y = 6; x -= y; y += x;
Both the value of x and the value of y have been decreased.
double avg = 15 + 20; avg /= 2;
C The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2.
double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product);
Either the numerator or the denominator of the fraction 1 / 2 should be cast as double.
int x = /* initial value not shown */; int y = /* initial value not shown */; int z = x; z /= y; z += 2;
It sets z to (x / y) + 2.
double len1; double len2; double len3; double total = len1 + len2 + len3; int minLength = (int) (total + 0.5); System.out.print(minLength);
The code segment works as intended but only when the sum of the three lengths is an integer or the decimal part of the sum of the three lengths is greater than or equal to 0.5.
int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans);
The code should have cast either num1 or num2 in the expression num1 / num2 to double.