Explicit casting and compound assignment operators are important parts of Java expression evaluation. AP Computer Science A questions can test how casting changes a value, how compound assignment updates a variable, and how post-increment and post-decrement affect numeric variables.
In this interactive practice set, we cover AP CSA Topics 1.5 and 1.6: Casting and Range of Variables, and Compound Assignment Operators. These 15 multiple-choice questions cover casting, integer range, rounding, compound assignment, and post-increment and post-decrement operators.
Key Concepts to Remember Before You Start:
- Casting: The casting operators
(int)and(double)can convert primitive values in arithmetic expressions. Casting adoubleto aninttruncates the digits to the right of the decimal point. - Rounding: For a non-negative
double,(int)(x + 0.5)rounds to the nearest integer. For a negativedouble,(int)(x - 0.5)rounds to the nearest integer. - Compound Assignment: The operators
+=,-=,*=,/=, and%=perform the indicated arithmetic operation and assign the result back to the variable on the left. - Integer Range: Java
intvalues have a finite range fromInteger.MIN_VALUEthroughInteger.MAX_VALUE. Arithmetic that produces a value outside this range can result in integer overflow. - Post-Increment and Post-Decrement:
x++andx--add 1 or subtract 1 from the stored value of a numeric variable. Prefix forms such as++xare outside the current AP CSA scope.
AP CSA 1.5–1.6: Casting & Compound Assignment
int displayTemp = (int) temperature;
System.out.println(displayTemp);
double to an int does not round the number mathematically; it explicitly truncates (chops off) the decimal portion entirely. Therefore, 88.9 simply becomes 88.System.out.println((int) balance);
-15.75 drops the .75 and becomes -15. It does not round down to the next lowest mathematical integer (-16).int count = 4;
double average = (double) (total / count);
System.out.println(average);
(total / count) happens first. 11 / 4 evaluates to 2. After the integer division is entirely complete, that result (2) is cast to a double, turning it into 2.0. To get 2.75, the code would need to be (double) total / count;.System.out.println(result);
1.
(double) 9 casts the 9 to 9.0.2.
9.0 / 2 performs floating-point division, yielding 4.5.3.
(int) 4.5 casts the result back to an integer, truncating the decimal, leaving exactly 4.score += 15;
System.out.println(score);
+= is a shorthand syntax. The statement score += 15; is functionally identical to score = score + 15;. Therefore, 50 + 15 evaluates to 65 and is assigned back to score.x *= 3 + 2;
System.out.println(x);
x *= 3 + 2; actually evaluates as x = x * (3 + 2);. Therefore, it calculates 10 * 5, which equals 50.m %= 6;
System.out.println(m);
m %= 6; translates to m = m % 6;. The modulo operator finds the remainder. 6 goes into 23 three times (18), leaving a remainder of 5.double variable val to the nearest integer and stores it in roundedVal?int truncates rather than rounds, adding 0.5 pushes values with a decimal of .5 or higher up to the next integer before truncation occurs. For example, if val is 3.6, 3.6 + 0.5 = 4.1. Truncating 4.1 yields 4.double variable negVal to the nearest integer?negVal is -3.6, -3.6 - 0.5 = -4.1. Casting to an int truncates the decimal toward zero, yielding -4.int count = 10;. Which of the following is functionally equivalent to the statement count = count - 1;?--) subtracts 1 from the integer variable as a standalone statement. Option A assigns negative one (count = -1). Option D is a boolean comparison, not an assignment.val -= 3;
val *= 2;
System.out.println(val);
1.
val -= 3; subtracts 3, updating val to 7.2.
val *= 2; multiplies the current value by 2, updating val to 14.2 += d attempts to reassign the value of the integer literal 2, which is impossible and immediately throws a compilation error. Note: Option C is legal because c =+ 5; evaluates to assigning a positive five: c = (+5);.int x1 = 5; and int x2 = 8;. Which of the following correctly stores the exact mathematical average (6.5) into the variable avg?II. double avg = (double) (x1 + x2) / 2;
III. double avg = (x1 + x2) / 2.0;
(x1 + x2) / 2 divides an integer by an integer, resulting in 6, which is then implicitly widened to 6.0.II is correct because the sum
13 is cast to 13.0 before division, triggering floating-point division.III is correct because dividing by the floating-point literal
2.0 automatically promotes the sum 13 to 13.0, resulting in 6.5.num++;
System.out.println(num);
Integer.MAX_VALUE is 2147483647, the largest value an int can store. The post-increment operator attempts to add 1, producing a value outside the int range. In Java, the resulting int value wraps around to -2147483648, which is Integer.MIN_VALUE.int q = 3;
p += q * 2 + 1;
System.out.println(p);
p += [expression] equates to p = p + ([expression]). First, evaluate the entire right side using standard order of operations: q * 2 + 1 -> 3 * 2 + 1 -> 6 + 1 -> 7. Finally, add this result to the original value of p: 5 + 7 = 12.Topics 1.5–1.6 Practice Complete!
Why Casting and Compound Assignment Matter for AP CSA
Casting and compound assignment are useful when tracing Java code because they can change both the type and value of an expression or update a variable in a compact statement. The current College Board framework includes casting, integer range, compound assignment operators, and post-increment and post-decrement operators in Unit 1.
Continue Your Unit 1 Practice
After practicing Topics 1.5–1.6, continue with the available Unit 1 practice assessment. The existing assessment covers selected Unit 1 foundation topics and is not a complete assessment of every current Unit 1 topic.
Continue your AP CSA review with the Unit 1 Foundation Mock Exam, featuring 20 mixed multiple-choice questions from the existing Unit 1 practice set.