AP Computer Science A Practice Quiz: Casting and Range of Variables
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 16 questions to check your progress.
Question 1 of 16
All Questions (16)
A) 7
B) 8
C) 7.0
D) 7.8
Correct Answer: A
The `(int)` casting operator truncates a double value, meaning it removes the decimal part entirely. Therefore, 7.8 becomes 7.
A) 2
B) 2.0
C) 2.5
D) 3
Correct Answer: C
Casting the integer `x` to a `double` before the division forces the operation to be floating-point division instead of integer division. The expression becomes 5.0 / 2, which evaluates to 2.5.
A) When a double value is converted to an int, losing its decimal part.
B) When an arithmetic expression evaluates to an integer value outside of its allowed range.
C) When a double value is rounded because it cannot be stored with perfect precision.
D) When an int is divided by another int, resulting in a non-integer value.
Correct Answer: B
The provided content states, 'If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs.'
A) When an integer calculation results in a value larger than the maximum storable integer.
B) When a double expression evaluates to a value that is more precise than can be stored.
C) When a double is cast to an int.
D) When an integer is divided by zero.
Correct Answer: B
The content specifies that a round-off error occurs 'If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory.'
A) 6
B) 7
C) 7.3
D) 8
Correct Answer: B
First, the expression inside the parentheses is evaluated: `4.5 + 2.8` equals `7.3`. Then, the `(int)` cast is applied to `7.3`, which truncates the decimal part, resulting in `7`.
A) The program terminates with an error.
B) The value becomes the maximum possible integer value.
C) The value is an int within the allowed range, but it is not the mathematically correct value.
D) The value is automatically converted to a double to accommodate the size.
Correct Answer: C
The content states that when an integer overflow occurs, 'The result is an int value in the allowed range but not necessarily the value expected.'
A) It is truncated, losing its decimal part.
B) It causes the program to crash.
C) It is rounded to the nearest representable value.
D) It is set to zero.
Correct Answer: C
The content explains that when a round-off error occurs, 'The result will be rounded to the representable value.'
A) 5 / 2
B) (int) 5.0 / 2
C) (double) 5 / 2
D) (int) (5 / 2.0)
Correct Answer: C
Expression C, `(double) 5 / 2`, casts the integer 5 to a double (5.0) before the division, resulting in floating-point division and the value 2.5. Option A is integer division (result 2). Option B results in 2 / 2 = 1. Option D results in (int) 2.5, which is 2.
A) A round-off error
B) An integer overflow
C) A casting error from double to int
D) A syntax error
Correct Answer: B
An integer overflow occurs when an integer expression exceeds its maximum storable value. The result is an unexpected value that is still within the int range, which matches the description. Round-off errors apply to double values.
A) 2.5
B) 2.0
C) 2
D) 3.0
Correct Answer: B
Due to the parentheses, the integer division `10 / 4` is performed first, which results in `2`. Then, the `(double)` cast is applied to the integer `2`, converting it to the double value `2.0`.
A) (int) 10.5
B) 200 * 500
C) 10.0 / 2.0
D) 1.0 / 3.0
Correct Answer: D
The expression `1.0 / 3.0` results in `0.3333...`, a repeating decimal that cannot be stored with perfect precision in a finite amount of memory. This will cause a round-off error. The other options result in exact representable values.
A) Syntax errors and logic errors
B) Integer overflow and round-off error
C) Casting to int and casting to double
D) Using addition and using multiplication
Correct Answer: B
The content explicitly describes integer overflow and round-off error as conditions that limit the accuracy of expressions, producing results that are not the expected mathematical values.
A) 4
B) 5
C) 5.0
D) 4.0
Correct Answer: A
First, `a / b` (5.0 / 2) evaluates to `2.5`. Next, `(int) (2.5)` truncates this to `2`. Finally, `2 * b` (2 * 2) evaluates to `4`. The final value stored in `result` is 4.
A) 12
B) 13
C) 12.0
D) 12.99
Correct Answer: A
The `(int)` cast is applied to the `double` variable `val`, which holds `12.99`. The cast truncates the decimal portion, resulting in the integer value `12` being assigned to `price`.
A) Rounding to the nearest integer
B) Truncation of the fractional part
C) An integer overflow if the value is too large
D) A round-off error
Correct Answer: B
The `(int)` cast operator truncates a `double` value, meaning it discards the decimal part entirely, rather than rounding. This is a form of information loss but is distinct from round-off error or overflow.
A) A round-off error, storing an approximate value.
B) The program will automatically use a double to store the result.
C) An integer overflow, resulting in an unexpected int value.
D) The program will halt and report a compile-time error.
Correct Answer: C
The content states that if an integer expression evaluates to a value outside the allowed range, an integer overflow occurs. The result is an int value, but not the one expected. This scenario perfectly describes an integer overflow.