professor-curious-logo
professor-curious-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What are the differences between Math.abs(int) and Math.abs(double)?

Math.abs(int) takes an integer and returns an integer. Math.abs(double) takes a double and returns a double.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What are the differences between Math.abs(int) and Math.abs(double)?

Math.abs(int) takes an integer and returns an integer. Math.abs(double) takes a double and returns a double.

What are the differences between Math.pow(x, 2) and x * x?

Math.pow(x, 2) works for doubles and returns a double. x * x works for both integers and doubles, but might be faster for integers.

What are the differences between inclusive and exclusive ranges when generating random numbers?

Inclusive range includes both endpoints, exclusive range excludes the upper endpoint.

What are the differences between using Math.sqrt(x) and Math.pow(x, 0.5)?

Both calculate the square root, but Math.sqrt(x) is more readable and potentially optimized for square root calculation.

What are the steps to generate a random integer between 10 and 20 (inclusive) using Math.random()?

  1. Calculate the range: 20 - 10 + 1 = 11. 2. Multiply Math.random() by the range: Math.random() * 11. 3. Add the minimum value: Math.random() * 11 + 10. 4. Cast the result to an integer: (int)(Math.random() * 11 + 10).

Steps to calculate distance between two points (x1, y1) and (x2, y2)?

  1. Find the difference in x-coordinates: (x2 - x1). 2. Find the difference in y-coordinates: (y2 - y1). 3. Square both differences. 4. Sum the squared differences. 5. Take the square root of the sum.

What are the steps to find the absolute value of a number?

  1. Check if the number is negative. 2. If negative, multiply by -1. 3. If non-negative, return the number as is.

Steps to calculate x to the power of y?

  1. Use Math.pow(x, y). 2. The method returns a double representing x raised to the power of y.

Steps to simulate a dice roll using Math.random()?

  1. Multiply Math.random() by 6: Math.random() * 6. 2. Add 1: Math.random() * 6 + 1. 3. Cast to an integer: (int)(Math.random() * 6 + 1).

What are the steps to use a method from the Math class?

  1. Identify the required method. 2. Use the syntax Math.methodName(arguments). 3. Ensure you handle the return type appropriately.

Steps to generate a random number between a (inclusive) and b (exclusive)?

  1. Calculate the range: b - a. 2. Multiply Math.random() by the range: Math.random() * (b - a). 3. Add the starting value: Math.random() * (b - a) + a. 4. Cast to int if needed.

Steps to find the square root of a number?

  1. Use Math.sqrt(number). 2. The method returns a double representing the square root of the number.

What does the following code output?

java
System.out.println(Math.abs(-10 + 3));

7

What does the following code output?

java
System.out.println(Math.pow(4, 0.5));

2.0

What does the following code output?

java
System.out.println((int)(Math.random() * 6) + 1);

A random integer between 1 and 6 (inclusive).

What does the following code output?

java
System.out.println(Math.sqrt(9) + Math.pow(2,3));

11.0

What does the following code output?

java
System.out.println(Math.abs(Math.min(-5, 5)));

5

Identify the error in the following code:

java
int x = Math.pow(2, 3);

Math.pow() returns a double, so you need to cast it to an int or use a double variable: double x = Math.pow(2, 3); or int x = (int) Math.pow(2, 3);

Identify the error in the following code:

java
System.out.println(Math.sqrt(-4));

The code will compile and run but will return NaN (Not a Number) because you cannot take the square root of a negative number.

What does the following code output?

java
System.out.println((int)Math.random() * 10);
  1. The Math.random() is cast to int first, resulting in 0, then multiplied by 10.

What does the following code output?

java
int a = 5;
double b = 2.0;
System.out.println(Math.pow(a, b));

25.0

What does the following code output?

java
System.out.println(Math.abs(3 - 7.5));

4.5