Sei sulla pagina 1di 4

PAP / CS 1 Math/Type Casting – Review

This test will cover the following topics: Math methods, type casting, and anything covered in our previous
tests / labs. Below are some problems that are similar to what you'll find on the test. Solutions can be
found at the end of this document.

You are NOT expected to memorize all the Math class methods. All the methods used will either be self-
explanatory (e.g. Math.pow()) , or explanations will be provided.

1. How many parameters does Math.sqrt() have? What is its return type?

2. What is the output of the following code?


Math.pow(12, 2);

3. What is the output of the following code?


double result = Math.min(-4, 3);
System.out.println(result);

4. What is the output of the following code?


double result = Math.abs(-12 * 2);
System.out.println(result);

5. What is the output of the following code?


double result = Math.min(Math.abs(-6), 3);
System.out.println(result);

6. What is the output of the following code? Why?

System.out.println(Math.max(Math.min(11, 7)));

7. What is the output of the following code?

System.out.println((int) 1.23);

8. What is the output of the following code?

double result = Math.pow(2, 2) / 3; //Math.pow() returns a double


System.out.println(result);

9. What is the output of the following code?

double result = (int) (Math.pow(2, 2) / 3);


System.out.println(result);

Page 1 of 4
10. What is the output of the following code?

double result = (int) Math.sqrt(25) / 4;


System.out.println(result);
System.out.println(result);

11. What is the output of the following code? Why?

double result = Math.min(1.23, 4.56);


System.out.println("Result is shown below ");
System.out.println(result);

12. What is the output of the following code?

int a = 4, result = 0;
for (int i = 2; i < 10; i++)
{
if (i % 2 == 1 && i < 6)
{
result = Math.max(a, i);
}
}
System.out.println(result);

13. What is the output of the following code?

double result = (int) Math.min(1.23, 4.56);


System.out.println((int)(result / 2));

14. What is the output of the following code?

int a = 4, result = 0;
for (int i = 0; i < 10; i +=2)
{
result = Math.max(a, i);
}
System.out.println(result);

15. What is the output of the following code?

double result = Math.pow(Math.min(Math.max(1, 2), 3), 2);


System.out.println(result);

16. What is the output of the following code?

int a = 123;
while (a > 10)
{
a = Math.round(a / 10);
}
System.out.println(a);

17. What is the output of the following code?

int a = 1234;
while (a > 10)
{
System.out.print(Math.round(a / 10) + " ");
a /= 10;
}

Page 2 of 4
18. What is the algorithmic purpose (the function) of the following code?

int a = -1, b = -1, doWhatNow = 0;

while (a != 0 && b != 0)
{
System.out.print("Enter two integers, separated by a space >>> ");
a = console.nextInt();
b = console.nextInt();
doWhatNow += Math.max(a, b);
}
System.out.println(doWhatNow);

Solutions on the next page. Don't look before you try them on your own, you lazy!

Page 3 of 4
1. One parameter, returns a double
2. Trick question! There would be no explicit output (this method returns a value, but
doesn't print). You should wrap that statement in a System.out.println()
3. -4.0
4. 24.0
5. 3.0
6. This code wouldn't compile – the Max.max() method expect two parameters, and is only supplied
one (what the Math.min() method returns)
7. 1
8. 1.33... – when an expression contains a double and an int, the result will be a double
9. 1.0 – the expression is casted to int, but saved into a double
10. 1.0
1.0
11. Result is shown below
1.23
12. 5
13. 0
14. 8
15. 4.0
16. 1
17. 123 12 1
18. To sum the larger of two numbers repeatedly entered by the user until the user enters 0
0 and then to print that sum

Page 4 of 4

Potrebbero piacerti anche