AP CSA Practice Test: Math and Random
AP CSA Practice Test: Math and Random
Unit 1: Using Objects and Methods | 10 Questions | AP Computer Science A
Math.abs(Math.min(-8, -3))?Explanation: Math.min(-8, -3) returns -8 (the smaller value). Math.abs(-8) returns 8.
Common Mistake: Confusing Math.min with Math.abs. Math.min(-8, -3) returns -8, not -3.
(int)(Math.random() * 15) + 5
(int)(Math.random() * 10) + 5
(int)(Math.random() * 11) + 5
(int)(Math.random() * 10) + 6
Explanation: Range formula: (int)(Math.random() * (max - min + 1)) + min. Here: (int)(Math.random() * (15 - 5 + 1)) + 5 = (int)(Math.random() * 11) + 5. This produces values from 5 to 15 inclusive.
Common Mistake: Using * 10 instead of * 11. That produces 5 to 14, missing 15.
System.out.println((int) Math.pow(3, 3) + 1);
Explanation: Math.pow(3, 3) returns 27.0 (a double). (int) 27.0 = 27. 27 + 1 = 28. Since 27 and 1 are both ints, the result is the int 28.
Common Mistake: Forgetting that Math.pow returns a double and needs casting for int arithmetic.
Math.random()?I. It returns a value greater than or equal to 0.0
II. It returns a value less than 1.0
III. It can return exactly 1.0
Explanation: Math.random() returns a value in the range [0.0, 1.0). It CAN return 0.0, it is ALWAYS less than 1.0, and it can NEVER return exactly 1.0. So I and II are true, III is false.
Common Mistake: Thinking Math.random() can return 1.0. The upper bound is exclusive.
double x = -4.0; System.out.println(Math.sqrt(Math.abs(x)));
Explanation: Math.abs(-4.0) = 4.0. Math.sqrt(4.0) = 2.0. Without the Math.abs(), you would get NaN since the square root of a negative number is undefined.
Common Mistake: Thinking Math.sqrt of a negative number causes an error. It returns NaN, not an exception.
val take?int val = (int)(Math.random() * 6) + 1;
Explanation: Math.random() * 6 produces [0.0, 6.0). Casting to int gives 0, 1, 2, 3, 4, or 5. Adding 1 gives 1, 2, 3, 4, 5, or 6. That is 6 possible values (like rolling a die).
Common Mistake: Off-by-one error: thinking * 6 gives 7 possible values.
(int)(Math.pow(2, 0.5) * 10) / 10.0
Explanation: Math.pow(2, 0.5) = 1.41421... Multiply by 10 = 14.1421... Cast to int = 14. Divide by 10.0 = 1.4. The cast truncates the decimal before dividing.
Common Mistake: Not tracking the order of operations: the int cast happens to (Math.pow(2,0.5)*10), truncating to 14.
Math.pow(x, 0.5)?Math.sqrt(x)
x / 2
Math.abs(x) / 2
x * 0.5
Explanation: Raising a number to the 0.5 power is mathematically equivalent to taking its square root. Math.pow(x, 0.5) and Math.sqrt(x) return the same value.
Common Mistake: Confusing x^0.5 (square root) with x * 0.5 (half of x).
mystery(-5, 3)?public static int mystery(int a, int b) {
return Math.abs(a) + Math.abs(b - a);
}
Explanation: Math.abs(-5) = 5. b - a = 3 - (-5) = 8. Math.abs(8) = 8. Return 5 + 8 = 13.
Common Mistake: Calculating b - a as 3 - 5 = -2 instead of 3 - (-5) = 8.
System.out.println(Math.pow(Math.pow(2, 3), 2));
Explanation: Inner call: Math.pow(2, 3) = 8.0. Outer call: Math.pow(8.0, 2) = 64.0. This is (2^3)^2 = 8^2 = 64.
Common Mistake: Confusing with 2^(3*2) = 2^6 = 64 (same answer here, but for wrong reasoning) or miscalculating.
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]