Using Objects in AP Computer Science A
When would it be most appropriate to use the Math.abs(a - b) method instead of just computing a - b directly in your code?
When you're trying to determine if a or b is a larger number without concern for their magnitude
If you want to receive a compile-time error if a is less than b
When you need to ensure that the result is always positive regardless of whether a or b is greater
If you want to perform a modulo operation between a and b instead of subtraction
Which method from the Java's built-in 'Math' class can generate random numbers between [0 and 1)?
Math.random()
No I'm not sure what you mean by that.
No I'm not sure what you mean by that.
No I'm not sure what you mean by that.
If an algorithm requires the calculation of the square root of a number, which method from the Math class in Java should be used?
Math.pow(double a, double b)
Math.round(double a)
Math.sqrt(double a)
Math.abs(int a)
What is the return type of the Math.pow() method?
String
Integer
The same type as the input number
Double
Given an ArrayList nums, which snippet efficiently checks if it is empty before trying to access its first element?
if (nums.size() > 0) System.out.println(nums.get(0));
if (!nums.isEmpty()) System.out.println(nums.get(0));
try { System.out.println(nums.get(0)); } catch(Exception e) {}
if (nums != null) System.out.println(nums.get(0));
What would be returned by calling Math.sqrt(-4)?
-2
A NumberFormatException
NaN (Not-a-Number)
An IllegalArgumentException
Which following expression could represent the sum of a geometric series where the first term, a, is a power of two, the common ratio r is half, and n equals the number of terms (given n > 0 and always an integer)?
a * (1 - Math.pow(r, (n - 1))) / (l - r)
a * ((Math.pow(n, r) - 1)) / (r - 1)
a * (1 - Math.pow(r, n)) / (1 - r)
a * (1 - Math.pow(r, n)) / (r - 1)

How are we doing?
Give us your feedback and let us know how we can improve
Which Java method returns the absolute value of an int variable named 'x'?
Math.pow(x,2)
Math.abs(x)
Math.min(x)
Math.sqrt(x)
Which expression correctly computes the value of a double x raised to the power of another double y, ensuring that the result is also a positive integer?
(double)Math.abs(Math.pow(x, y))
(int)Math.sqrt(Math.pow(x, y))
(int)Math.pow(Math.abs(x), y)
(int)Math.round(Math.exp(x) * y)
Given a recursive method public static int mystery(int x) that uses Math.random() to sometimes increase and other times decrease the value of x before the next recursive call, which change would most likely make it terminate more often?
Adding a base case that returns when x is within a specific range.
Removing any decrements to x in the method body.
Calling mystery(x) multiple times within each recursion.
Increasing the range of values for Math.random().