A method that belongs to the class itself rather than to an instance of the class. It can be called directly using the class name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Flip
Revise later
SpaceTo flip
If confident
All Flashcards
What is a static method?
A method that belongs to the class itself rather than to an instance of the class. It can be called directly using the class name.
What is the absolute value of a number?
The distance of a number from zero, always non-negative.
What does inclusive mean in the context of ranges?
Including the specified endpoint in the range.
What does exclusive mean in the context of ranges?
Not including the specified endpoint in the range.
What is the purpose of the Math class in Java?
To provide static methods for performing common mathematical operations.
What is the return type of Math.random()?
double
What is the return type of Math.abs()?
The same type as the input argument.
What is the return type of Math.pow()?
double
What is the return type of Math.sqrt()?
double
What is the purpose of casting to int when using Math.random()?
To convert the double value returned by Math.random() to an integer, effectively truncating the decimal part.
Why are Math class methods static?
So they can be called directly using the class name without creating an object.
How do you generate a random integer between a and b (inclusive)?
Use (int)(Math.random() * (b - a + 1) + a).
Why is Math.abs() useful for distance calculations?
It ensures the distance is always positive, regardless of the order of subtraction.
Why does Math.pow() always return a double?
To handle fractional exponents and potentially large results accurately.
What is the range of values returned by Math.random()?
Greater than or equal to 0.0 and less than 1.0.
Explain the concept of method overloading.
Method overloading is having multiple methods in the same class with the same name but different parameters.
How does the Math class relate to primitive data types?
The Math class methods often take primitive data types (int, double) as input and return primitive data types as output.
Explain the significance of return types in Math class methods.
Return types determine the type of value that the method provides back to the caller, which must be handled correctly in the code.
What are the implications of using Math.random() for simulations?
It allows for the introduction of randomness and variability, making simulations more realistic.
Why is understanding the Math class important for the AP CSA exam?
Because it is a fundamental tool used throughout the course, often combined with other concepts in exam questions.
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);
```
0. 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));
```