AP Computer Science A · Topic 2.9
Using the Math Class Practice
Part of Using Objects.
Practice questions
18
Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.
Sample questions
5 of 18 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
int x = Math.abs(-7); System.out.println(x);What is printed?
- Acheck_circle
7
- B
-7
- C
0
- D
1
Why
Math.abs returns the absolute value of its argument, so Math.abs(-7) returns 7.
- A
Sample 2difficulty 2/5
<code>Math.abs(-7)</code> returns
- A
0
- B
−7
- C
1
- Dcheck_circle
7
Why
Absolute value: |−7| = 7.
- A
Sample 3difficulty 2/5
double r = Math.sqrt(16); System.out.println(r);What is printed?
- A
4
- Bcheck_circle
4.0
- C
16.0
- D
2.0
Why
Math.sqrt returns a double; the square root of 16 is 4.0.
- A
Sample 4difficulty 2/5
int m = Math.max(-4, -9); System.out.println(m);What is printed?
- A
9
- Bcheck_circle
-4
- C
-9
- D
4
Why
Math.max returns the greater of its arguments; -4 > -9, so the result is -4.
- A
Sample 5difficulty 2/5
double d = Math.abs(-3.5); System.out.println(d);What is printed?
- Acheck_circle
3.5
- B
-3.5
- C
3
- D
4
Why
Math.abs(double) returns the absolute value as a double, so -3.5 becomes 3.5.
- A