Casting and Range of Variables MCQ Practice

Question 1 of 10

A cafeteria kiosk splits a bill stored in whole cents. What value does share hold after this code runs?

int cents = 1750;
int people = 4;
double share = (double) cents / people;
  1. A. 437
  2. B. 437.5
  3. C. 438.0
  4. D. 4.375

Question 2 of 10

A weather station records a gust of -9.75 meters per second and stores a truncated int for a display that cannot show a fraction. What value does shown hold?

double gust = -9.75;
int shown = (int) gust;
  1. A. -10
  2. B. -9
  3. C. 9
  4. D. -9.75

Question 3 of 10

A lab program groups sample vials and then stores the group size as a double. What value does leftover hold?

int raw = 17;
int bins = 5;
double leftover = (double) (raw / bins);
  1. A. 3.4
  2. B. 3
  3. C. 3.0
  4. D. 4.0

Question 4 of 10

A shipping scale reads 12.4 kilograms. The warehouse stores the nearest whole kilogram for this nonnegative reading. What value does rounded hold?

double mass = 12.4;
int rounded = (int) (mass + 0.5);
  1. A. 12
  2. B. 13
  3. C. 12.4
  4. D. 12.9

Question 5 of 10

A warehouse estimates leftover inventory with this fragment. The intended mathematical value of leftover is 1000000000, which fits in an int. What is true of the Java code?

int packs = 60000;
int each = 50000;
int leftover = packs * each / 3;
  1. A. leftover is 1000000000 because the final result fits in an int.
  2. B. leftover is not 1000000000 because packs * each overflows before the division.
  3. C. Java automatically evaluates the entire expression using long.
  4. D. The program throws ArithmeticException.

Question 6 of 10

Which row's mathematical result is outside the inclusive int range?

Row Expression
P Integer.MIN_VALUE + 8
Q Integer.MAX_VALUE - 8
R Integer.MIN_VALUE - 8
  1. A. Row P only
  2. B. Row Q only
  3. C. Row R only
  4. D. Rows P and Q

Question 7 of 10

A scoreboard stores a running total in an int. What is the largest value it can hold without overflow?

  1. A. 2147483648
  2. B. 2147483647
  3. C. 4294967295
  4. D. There is no largest int value.

Question 8 of 10

A fuel gauge stored as an int is already at the maximum. What value does fuel hold after this update?

int fuel = Integer.MAX_VALUE;
fuel += 3;
  1. A. 2147483650
  2. B. 2147483647
  3. C. -2147483646
  4. D. The program throws ArithmeticException.

Question 9 of 10

A chemistry logger adds 0.1 mL ten times as double and then compares the total to 1.0 using ==. Why might that comparison be false even though the intended total is exactly 1.0 mL?

  1. A. Adding a double ten times always overflows int.
  2. B. Some decimal fractions cannot be stored exactly in a double, so the accumulated total can differ slightly from 1.0.
  3. C. The == operator cannot compare double values at all.
  4. D. Java converts 0.1 to 0 before each addition.

Question 10 of 10

A race clock needs exact tenths of a second. Every stored time and every intermediate total will fit in an int. Which row avoids fractional double round-off for addition?

Row Representation
W double seconds
X int tenths
Y double minutes
Z int hashed from the printed time string
  1. A. Row W
  2. B. Row X
  3. C. Row Y
  4. D. Row Z