Using Objects in AP Computer Science A
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 would give you a random even integer between 102 and 120 inclusive by using Java's Math.random()
method?
(int)(Math.random() * (119 -103)) /2)*2+104
((int)Math.random() * (60-51))*4+104
(int)(Math.random() * (120 - 102) + 1) * 2 + 102
(int)(Math.random() * ((120 - 102) / 2 + 1)) * 2 + 102
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()
.
Which method from the Math class would you use in Java to obtain the larger of two integers – x and y?
Math.max(x,y)
Math.ceil(x/y)
Math.abs(x-y)
Math.min(x,y)
If x
and y
are both positive double values greater than 0 and less than 10, which code snippet will return a double value closest to an integer without ever exceeding it?
Math.min(Math.round(x), y - Math.round(y))
Math.max(Math.floor(x * y), x + y)
Math.max(Math.ceil(x), y - Math.ceil(y))
Math.min(Math.floor(x), y - Math.floor(y))
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)
What would be the result of calling Math.ceil on a negative non-integer double value in Java?
The smallest whole number greater than or equal to the value.
The original non-integer double value without changes.
A positive integer closest to zero from the given value.
The largest whole number less than or equal to the value.

How are we doing?
Give us your feedback and let us know how we can improve
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.
How would you generate a random integer between 50 and 100 inclusive using Math.random()
?
(int)(Math.random() * 51 + 50)
(int)(Math.random() * 51)
(int)(Math.random() * 100 + 50)
(int)(Math.random() * (100 - 50))