AP Computer Science A • Unit 1.11

Math Class

Detailed answer explanations for the Unit 1.11 multiple-choice practice questions.

Q1B
Q2D
Q3B
Q4B
Q5C
Q6B
Q7B
Q8B
Q9A
Q10C
Q11B
Q12C
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following statement. What is printed?

System.out.println(Math.sqrt(49));
A7
B7.0
C49
D49.0
Correct answer: B. Math.sqrt returns a double, so the square root of 49 is printed as 7.0 rather than 7.
Explanation

The method returns the nonnegative square root of a nonnegative argument as a double. Even when the root is mathematically a whole number, the returned value keeps its decimal form, and printing it shows that form.

  • A is not correct: 7 is the whole-number form of the answer, but Math.sqrt returns a double, so the printed value carries a decimal point.
  • C is not correct: 49 is the argument passed into the call, not the value the call returns; the method returns the square root of that argument.
  • D is not correct: 49.0 is the argument written in decimal form, but the call returns the square root of 49, which is 7.0.
Q2

Consider the following code segment. What is printed?

double value = Math.pow(2, 3);
System.out.println(value);
A5
B6
C8
D8.0
Correct answer: D. Math.pow raises 2 to the power 3 and returns a double, so the printed value is 8.0.
Explanation

The method raises a number to a power and returns a double, even when the mathematical answer is a whole number. The returned value is stored in a double variable and then printed in that form.

  • A is not correct: 5 is the sum of the two arguments, but Math.pow raises the first argument to the power given by the second.
  • B is not correct: 6 is the product of the two arguments, and raising a number to a power is not the same operation as multiplying the arguments together.
  • C is not correct: 8 is the mathematical answer but not the printed form, because Math.pow returns a double even for a whole-number result.
Q3

Consider the following statement. What is printed?

System.out.println(Math.abs(-9));
A-9
B9
C9.0
DThe program throws an exception.
Correct answer: B. Math.abs returns the absolute value of its argument, and the version that accepts an int returns the int value 9.
Explanation

The name Math.abs refers to more than one method. One version accepts an int and returns an int; another accepts a double and returns a double. Java selects the version whose parameter type matches the argument.

  • A is not correct: -9 is the argument supplied to the call, and the absolute value that Math.abs returns is never negative.
  • C is not correct: 9.0 would be printed by the version that returns a double, but the argument -9 is an int, so the int version runs.
  • D is not correct: nothing here causes an exception; Math.abs accepts an int argument and returns an int result in the ordinary way.
Q4

Consider the following code segment. Which list shows all possible values of x?

int x = (int)(Math.random() * 4) + 3;
A0, 1, 2, 3
B3, 4, 5, 6
C3, 4, 5, 6, 7
D4, 5, 6, 7
Correct answer: B. Multiplying by 4 gives offsets 0 through 3, and adding 3 shifts them to 3 through 6.
Explanation

Casting the product of Math.random() and a count produces an integer from 0 through one less than that count. Adding a number shifts the whole range upward without changing how many values it can contain.

  • A is not correct: 0, 1, 2, 3 describes (int)(Math.random() * 4) on its own, before the added 3 shifts every possible value upward.
  • C is not correct: 3, 4, 5, 6, 7 lists one value too many, because multiplying by 4 produces only four equally likely offsets.
  • D is not correct: 4, 5, 6, 7 leaves out the lowest result, because the offset can be 0 and 0 added to 3 gives 3.
Q5

Consider the following code segment. Which statement best describes the code?

int result = Math.pow(2, 3);
System.out.println(result);
AIt prints 8.
BIt prints 8.0.
CIt does not compile because Math.pow returns a double.
DIt does not compile because Math.pow requires one argument.
Correct answer: C. Math.pow returns a double, and storing that returned value in an int variable requires an explicit cast.
Explanation

A double value does not fit an int variable on its own. Assigning the result of Math.pow to an int requires an explicit cast and may lose information, so this declaration is not valid as written.

  • A is not correct: the code never reaches the printing statement, because the double returned by Math.pow cannot be stored in an int without an explicit cast.
  • B is not correct: 8.0 is the value Math.pow returns, but the declaration above it is not valid, so nothing is printed at all.
  • D is not correct: Math.pow raises a number to a power and takes two arguments, so the number of arguments supplied here is not the problem.
Q6

Consider the following code segment. What is printed?

double a = Math.abs(-6.5);
int b = Math.abs(-2);
System.out.println(a + b);
A4.5
B8.5
C-4.5
DThe code does not compile.
Correct answer: B. Math.abs makes both results nonnegative, and adding the double and the int together gives 8.5.
Explanation

One version of Math.abs accepts a double and returns a double; another accepts an int and returns an int. When an int and a double are combined in the same operation, the result is a double.

  • A is not correct: 4.5 would come from subtracting the two stored values, but the statement adds them after Math.abs has made both nonnegative.
  • C is not correct: -4.5 keeps a negative sign, and Math.abs returns the absolute value, so neither a nor b holds a negative number.
  • D is not correct: the code compiles: Math.abs(-6.5) returns a double and Math.abs(-2) returns an int, and each variable has the matching type.
Q7

Which expression produces a random integer from 1 through 10, inclusive?

A(int)(Math.random() * 10)
B(int)(Math.random() * 10) + 1
C(int)(Math.random() * 11)
D(int)(Math.random() * 9) + 1
Correct answer: B. There are 10 values from 1 through 10, so multiply by 10, cast the product, and add 1.
Explanation

To generate a random integer from a through b, inclusive, first count the possible values; there are b - a + 1 of them. Generate an offset starting at 0, then add the lowest value of the range.

  • A is not correct: (int)(Math.random() * 10) produces integers 0 through 9, so it can produce 0 and never reaches 10.
  • C is not correct: multiplying by 11 gives eleven equally likely offsets, which is one more than the number of values from 1 through 10.
  • D is not correct: multiplying by 9 gives only nine offsets, so after adding 1 the largest possible result is 9 rather than 10.
Q8

Which statement about Math.random() is correct?

AIt returns an int from 0 through 1.
BIt returns a uniformly distributed double greater than or equal to 0.0 and less than 1.0.
CIt returns a double greater than 0.0 and less than or equal to 1.0.
DIt returns an int from 1 through 10.
Correct answer: B. Math.random() returns a uniformly distributed double that can be 0.0 and is always less than 1.0.
Explanation

The returned values are spread evenly across the interval, so equal-width portions of it have equal probability. The lower endpoint belongs to the interval and the upper endpoint does not, and the returned type is double.

  • A is not correct: Math.random() returns a double rather than an int, and the value it returns is always less than 1.0.
  • C is not correct: this choice reverses the endpoints: the returned value can be 0.0, and it is always less than 1.0 rather than reaching it.
  • D is not correct: a result from 1 through 10 comes from multiplying and shifting the interval, not from Math.random() on its own.
Q9

Let r be the result of one call to Math.random(). Which two intervals are equally likely to contain r?

A([0.0, 0.2)) and ([0.8, 1.0))
B([0.0, 0.2)) and ([0.7, 1.0))
C([0.0, 0.3)) and ([0.8, 1.0))
D([0.0, 0.4)) and ([0.9, 1.0))
Correct answer: A. The two intervals have the same width, and equal-width portions of the interval have equal probability.
Explanation

Values from Math.random() are distributed uniformly. Because the spread is even, comparing two portions of the interval means comparing how wide they are, and only portions of the same width are equally likely to contain the result.

  • B is not correct: the portion from 0.7 to 1.0 is wider than the portion from 0.0 to 0.2, so the wider one is more likely.
  • C is not correct: the portion from 0.0 to 0.3 is wider than the portion from 0.8 to 1.0, and equal probability requires equal widths.
  • D is not correct: the portion from 0.0 to 0.4 is much wider than the portion from 0.9 to 1.0, so it is far more likely.
Q10

Consider the following code segment. What value is assigned to x?

int x = (int)Math.random() * 6 + 1;
AAn integer from 0 through 5
BAn integer from 1 through 6
CAlways 1
DAlways 6
Correct answer: C. The cast applies to Math.random() alone, which gives 0, so the whole expression always evaluates to 1.
Explanation

Since Math.random() is always less than 1.0, casting it first gives 0. Java then computes 0 * 6 + 1. For a random integer formula, the cast must surround the product instead.

  • A is not correct: an integer from 0 through 5 would require the cast to surround the product, as in (int)(Math.random() * 6).
  • B is not correct: an integer from 1 through 6 comes from (int)(Math.random() * 6) + 1, where the cast surrounds the whole product.
  • D is not correct: the expression evaluates 0 * 6 + 1 every time, so the value assigned is 1 and 6 is never produced.
Q11

Which declaration compiles and preserves the value returned by Math.abs(-4.5)?

Aint value = Math.abs(-4.5);
Bdouble value = Math.abs(-4.5);
CString value = Math.abs(-4.5);
Dboolean value = Math.abs(-4.5);
Correct answer: B. Math.abs returns a double for a double argument, so a double variable preserves the returned value.
Explanation

The name Math.abs refers to more than one method, and Java selects the version whose parameter type matches the argument. Here the argument is a double, so the value returned by the call is a double as well.

  • A is not correct: storing the double returned by Math.abs(-4.5) in an int variable requires an explicit cast and may lose information.
  • C is not correct: String is not a numeric type, so it cannot hold the double value that this call to Math.abs returns.
  • D is not correct: boolean is not a numeric type either, so the double returned for a double argument cannot be stored in it.
Q12

Consider the following code segment. What is the probability that ticket stores a value from 0 through 4, inclusive?

int ticket = (int)(Math.random() * 20);
A5%
B20%
C25%
D50%
Correct answer: C. The expression gives 20 equally likely values, and 5 of them fall from 0 through 4.
Explanation

Multiplying by 20 and casting gives an integer from 0 through one less than 20, with each value equally likely. Counting how many of those equally likely results belong to an event gives the probability of that event.

  • A is not correct: 5% counts the wanted results but ignores that there are 20 equally likely values in total to compare them against.
  • B is not correct: 20% uses the total number of possible values rather than the share of them that falls from 0 through 4.
  • D is not correct: 50% would require half of the 20 possible values, but only 5 of them are from 0 through 4.