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

Question 1
What is the value of Math.abs(Math.min(-8, -3))?
A3
B8
C-3
D-8

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.

Question 2
Which expression generates a random integer from 5 to 15, inclusive?
A(int)(Math.random() * 15) + 5
B(int)(Math.random() * 10) + 5
C(int)(Math.random() * 11) + 5
D(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.

Question 3
What is the output?
System.out.println((int) Math.pow(3, 3) + 1);
A27
B28
C28.0
DCompile error

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.

Question 4
Which of the following is always true about 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
AI and II only
BI and III only
CII and III only
DI, II, and III

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.

Question 5
What is the output?
double x = -4.0;
System.out.println(Math.sqrt(Math.abs(x)));
A2.0
BNaN
CCompile error
D-2.0

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.

Question 6
Consider the following code. How many possible values can val take?
int val = (int)(Math.random() * 6) + 1;
A5
B6
C7
DInfinite

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.

Question 7
What is the value of the expression?
(int)(Math.pow(2, 0.5) * 10) / 10.0
A1.4
B1.41421
C1.0
D1.4142

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.

Question 8
Which expression is equivalent to Math.pow(x, 0.5)?
AMath.sqrt(x)
Bx / 2
CMath.abs(x) / 2
Dx * 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).

Question 9
What does the following method return when called with mystery(-5, 3)?
public static int mystery(int a, int b) {
    return Math.abs(a) + Math.abs(b - a);
}
A13
B3
C10
D7

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.

Question 10
What is the output?
System.out.println(Math.pow(Math.pow(2, 3), 2));
A64.0
B12.0
C256.0
D8.0

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.

0% 0/10

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]