AP CSA Practice Test: Primitives and Casting
AP CSA Practice Test: Primitives and Casting
Unit 1: Using Objects and Methods | 10 Questions | AP Computer Science A
result after the following code executes?int a = 7; int b = 2; double result = a / b;
Explanation: Since a and b are both int, the division 7 / 2 performs integer division, producing 3. This integer 3 is then assigned to a double, becoming 3.0. To get 3.5, you would need to cast first: (double) a / b.
Common Mistake: Choosing 3.5 because the result is stored in a double. The cast to double happens AFTER the integer division.
int x = 13; int y = 5; System.out.println(x % y + x / y);
Explanation: x % y = 13 % 5 = 3 (remainder). x / y = 13 / 5 = 2 (integer division). So 3 + 2 = 5.
Common Mistake: Forgetting that / with two ints truncates. 13/5 is 2, not 2.6.
7.5?I.
(double)(15 / 2)II.
(double) 15 / 2III.
15.0 / 2
Explanation: I: 15 / 2 does integer division first (= 7), then casts to 7.0, not 7.5. II: casts 15 to 15.0 first, then divides by 2 = 7.5. III: 15.0 is already a double, so 15.0 / 2 = 7.5. Only II and III produce 7.5.
Common Mistake: Thinking (double)(15/2) casts before dividing. Parentheses force the integer division first.
double d = 9.7; int n = (int) d; System.out.println(n);
Explanation: Casting a double to an int truncates (drops the decimal), it does NOT round. (int) 9.7 = 9.
Common Mistake: Choosing 10 by rounding. Java casting always truncates toward zero.
Line 1: int a = 5; Line 2: double b = a; Line 3: int c = 3.14; Line 4: double d = 10;
Explanation: Line 3 attempts to assign a double literal (3.14) to an int without an explicit cast. Java does not allow narrowing conversions implicitly. Line 2 and Line 4 are fine because widening from int to double is automatic.
Common Mistake: Thinking Line 4 is an error. Assigning an int to a double (widening) is always allowed.
z after this code runs?int x = 5; int y = 3; int z = x + y * 2 - x % y;
Explanation: Order of operations: y * 2 = 6, x % y = 5 % 3 = 2. Then: 5 + 6 - 2 = 9. Multiplication and modulus have higher precedence than addition and subtraction.
Common Mistake: Evaluating left-to-right without respecting operator precedence.
System.out.println(2 + 3 + "hello" + 4 + 5);
Explanation: Java evaluates left-to-right. 2 + 3 = 5 (both ints). Then 5 + "hello" = "5hello" (now a String). Then "5hello" + 4 = "5hello4". Then "5hello4" + 5 = "5hello45".
Common Mistake: Thinking all numbers are added first or that 4+5 evaluates before concatenation.
I. A
boolean variable can store the value 0 or 1.II. An
int variable can store a decimal value like 3.14.III. A
double variable can store an integer value like 5.Explanation: I is false: boolean stores only true or false, not 0/1 (unlike C/C++). II is false: int cannot hold decimals. III is true: a double can hold 5 (it becomes 5.0 via widening).
Common Mistake: Confusing Java with other languages where booleans can be 0/1.
int a = Integer.MAX_VALUE; System.out.println(a + 1);
Explanation: Integer overflow wraps around. Integer.MAX_VALUE is 2147483647. Adding 1 causes overflow, producing -2147483648 (Integer.MIN_VALUE). Java does not throw an error for integer overflow.
Common Mistake: Expecting an error. Java silently overflows integers without any warning.
result?double result = 1 / 2 + 3 / 4 + 0.25;
Explanation: 1 / 2 = 0 (integer division). 3 / 4 = 0 (integer division). 0 + 0 + 0.25 = 0.25. Both divisions involve only ints, so they truncate before the double addition.
Common Mistake: Assuming the double on the left side forces all divisions to use decimal math.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]