AP Computer Science A • Unit 1.5

Casting and Range of Values

Detailed answer explanations for the Unit 1.5 multiple-choice practice questions.

Q1B
Q2D
Q3B
Q4C
Q5C
Q6D
Q7A
Q8B
Q9A
Q10C
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

int a = 7;
int b = 2;
double x = a / b;
double y = (double) a / b;
System.out.println(x);
System.out.println(y);
A3 followed by 3
B3.0 followed by 3.5
C3.5 followed by 3.5
D4.0 followed by 3.5
Correct answer: B. Integer division makes x print 3.0, while the cast on a makes the second division floating-point.
Explanation

Division uses the operand types present when / is evaluated. In a / b both operands are int, so the fractional part is discarded and the whole-number result is then widened. Casting a first converts one operand before the division.

  • A is not correct: both variables are declared as double, so each printed value carries a fractional part rather than appearing as a whole number.
  • C is not correct: the first division has two int operands, so the fraction is discarded before the result is widened, and a later widening cannot restore it.
  • D is not correct: casting and widening never round upward; integer division truncates toward zero, so the first statement keeps only the whole-number part.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

double value = 8.9;
int result = (int) value;
System.out.println(result);
A9
B8.0
CThe code does not compile.
D8
Correct answer: D. Casting a double to an int truncates toward zero, so the fractional part of 8.9 is discarded.
Explanation

A cast from double to int does not find the nearest whole number. It discards the fractional part, moving the value toward zero, and the remaining whole-number part is what the int variable stores.

  • A is not correct: casting alone never rounds; reaching the nearest whole number would require adding 0.5 before the cast, which this segment does not do.
  • B is not correct: the variable result is declared as an int, so it stores a whole number and prints without a fractional part.
  • C is not correct: the cast makes the narrowing conversion explicit, which is exactly what Java requires when a double value is stored in an int.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

double x = -3.75;
int y = (int) (x - 0.5);
System.out.println(y);
A-3
B-4
C3
DThe code does not compile.
Correct answer: B. Subtracting 0.5 first moves the negative value across the correct integer boundary, and the cast then truncates toward zero.
Explanation

For a negative value, rounding to the nearest integer means subtracting 0.5 before casting. The subtraction shifts the value across the correct boundary, and the cast then discards the fractional part by moving the value toward zero.

  • A is not correct: that result would come from casting x on its own; the segment subtracts 0.5 first, which shifts the value further from zero.
  • C is not correct: a cast does not remove the sign, since truncation only discards the fractional part and leaves a negative value negative.
  • D is not correct: the explicit cast is what Java requires for a narrowing conversion from double to int, so the segment compiles.
Q4

Consider the following code segment. What is printed as a result of executing the code segment?

int total = 10;
int count = 4;
double average = (double) (total / count);
System.out.println(average);
A2.5
B3.0
C2.0
DThe code does not compile.
Correct answer: C. The parentheses make total / count an integer division, and the cast converts only the whole-number result that division produced.
Explanation

The position of a cast matters. Inside the parentheses the division happens first with two int operands, so the fractional part is discarded. Converting the finished result afterwards cannot recover a fraction that is already gone.

  • A is not correct: that value would require one operand to be converted before the division, in the way that (double) total / 3 converts total first.
  • B is not correct: casting after integer division never rounds upward; the division discards the fractional part and leaves the smaller whole number behind.
  • D is not correct: the cast makes the conversion explicit and the assignment is compatible, so the segment compiles and prints a value with a fractional part.
Q5

Consider the following code segment. What is printed as a result of executing the code segment?

double value = 8.6;
int rounded = (int) (value + 0.5);
System.out.println(rounded);
A8
B8.5
C9
DThe code does not compile.
Correct answer: C. Adding 0.5 to a nonnegative value before the cast shifts it across the boundary, so 9 is printed.
Explanation

Casting alone never rounds. For a nonnegative value, adding 0.5 first moves the value across the correct integer boundary, and the cast then truncates toward zero, which leaves the nearest whole number.

  • A is not correct: that value would result from casting value directly; the segment adds 0.5 first, which pushes the value past the next integer boundary.
  • B is not correct: rounded is declared as an int, so it stores a whole number and cannot keep any fractional part of the sum.
  • D is not correct: the narrowing conversion is written with an explicit cast, which is what Java requires, so the segment compiles and runs.
Q6

Consider the following code segment. Which of the following best describes the result?

int x = Integer.MAX_VALUE;
System.out.println(x + 1);
AIt prints Integer.MAX_VALUE.
BIt prints a larger int value.
CThe code does not compile.
DInteger overflow occurs.
Correct answer: D. The mathematical result lies outside the int range, so overflow occurs and the value wraps to the smallest int.
Explanation

A Java int uses a fixed amount of memory, 4 bytes, so it stores only a limited range of values. Java does not switch to a larger numeric type when int arithmetic moves outside that range.

  • A is not correct: the stored value changes rather than staying the same, because adding 1 moves past the largest allowed value instead of holding it.
  • B is not correct: no larger int exists above the maximum, and Java does not switch to a wider numeric type when the range is exceeded.
  • C is not correct: adding 1 to an int is ordinary arithmetic, so the segment compiles; the surprise appears in the value produced when it runs.
Q7

Consider the following code segment. What is printed as a result of executing the code segment?

int points = 7;
double score = points;
System.out.println(score);
A7.0
B7
CThe code does not compile.
D7.00
Correct answer: A. Every int value can be represented exactly as a double, so Java widens automatically and prints 7.0.
Explanation

A widening conversion moves from a type with a narrower numeric range to one with a wider range. No cast is required because no information from the int value is lost, and the variable receiving the value is a double.

  • B is not correct: score is declared as a double, so the stored value is printed with a fractional part rather than as a whole number.
  • C is not correct: the assignment is a widening conversion that Java performs automatically, so no cast is needed and the segment compiles.
  • D is not correct: the stored double value is printed without extra padding digits, so no second decimal place appears in the output.
Q8

Which statement about floating-point values is most accurate?

AA double always represents values exactly.
BA double can sometimes store only an approximation.
CA double cannot be used in arithmetic expressions.
DA double always causes integer overflow.
Correct answer: B. Computer memory is finite, so a double stores the nearest representable value rather than every real number exactly.
Explanation

A value that falls between the available marks must be recorded using the nearest mark. Java stores the nearest representable double, and arithmetic can then reveal a small round-off effect in the displayed result.

  • A is not correct: a finite representation cannot store every decimal fraction exactly, which is why 0.1 + 0.2 prints a result carrying a tiny extra amount.
  • C is not correct: double values are used in arithmetic throughout this topic, including division and the additions performed just before a cast.
  • D is not correct: overflow is described for int arithmetic that moves outside the fixed 4-byte range, not for floating-point approximation.
Q9

Which statement best explains why integer overflow can occur?

AAn int uses a fixed amount of memory and has a limited range.
BJava rounds every integer before storing it.
CCasting always changes the original variable.
DA double occupies fewer bits than an int.
Correct answer: A. An int uses a fixed amount of memory, 4 bytes, so its range is limited and a calculation can leave it.
Explanation

When an int calculation moves outside the allowed range, overflow occurs. The result is still an int inside that range, but it may not be the mathematical answer that was expected.

  • B is not correct: rounding is not applied to integers before they are stored; overflow comes from the finite range, and a cast truncates rather than rounds.
  • C is not correct: a cast converts the single value or grouped expression that follows it, and it does not change the original variable.
  • D is not correct: a double is the wider type here, since every int value can be represented exactly as a double.
Q10

Which statement about the cast in (double)(a / b) is correct?

AIt converts a before division.
BIt converts b before division.
CIt converts the result produced by a / b.
DIt permanently changes both variables to double.
Correct answer: C. The parentheses group the division, so the cast converts the single value that a / b has already produced.
Explanation

A cast is a unary operator, because it converts the single value or grouped expression that follows it. When that group is a completed division, the conversion happens afterwards and cannot restore a discarded fraction.

  • A is not correct: converting a first would be written as (double) a / b, without parentheses placed around the division.
  • B is not correct: the cast is written to the left of the whole group, so it applies to the completed division rather than to the second operand alone.
  • D is not correct: a cast does not change the original variable; it converts the value that follows it for use in the surrounding expression.