# 1.9. Multiple Choice ExercisesΒΆ

1-9-1: What does the following code print?

```System.out.println(2 % 3);
```
• 2
• Whenever the first number is smaller than the second, the remainder is the first number. Remember that % is the remainder and 3 goes into 2 0 times with a remainder of 2.
• 0
• This is the number of times that 3 goes into 2 but the % operator gives you the remainder.
• 3
• Try it. Remember that % gives you the remainder after you divide the first number by the second one.
• 1
• This would be correct if it was 3 % 2 since 2 would go into 3 one time with a remainder of 1.

1-9-2: What does the following code print?

```System.out.println(19 % 5);
```
• 3
• This is the number of times that 5 goes into 19, but % is the remainder.
• 0
• This would only be true if the first number was evenly divisible by the second number.
• 4
• 5 goes into 19 3 times (15) with a remainder of 4 (19-15=4)
• 1
• This would be correct if it was 19 % 2, but here we are dividing by 5.

1-9-3: What does the following code print?

```System.out.println(1 / 3);
```
• 0.3333333333333333
• This would be correct if it was 1.0 / 3 or 1 / 3.0.
• 0
• When two integers are divided the results will also be integer and the fractional part is thrown away.
• It will give a run-time error
• You would get a run-time error if it was 1 / 0, because you can not divide by zero.
• 0.3
• Try it. Is this what you get?
• It will give a compile-time error
• Integer division is allowed in Java. It gives an integer result.

1-9-4: What does the following code print?

```System.out.println(2 + 3 * 5 - 1);
```
• 24
• This would be true if it was System.out.println(((2 + 3) * 5) - 1), but without the parentheses the multiplication is done first.
• 14
• This would be true if it was System.out.println(2 + (3 * (5 - 1))), but without the parentheses the multiplication is done first and the addition and subtraction are handled from left to right.
• This will give a compile time error.
• This will compile and run. Try it in DrJava. Look up operator precedence in Java.
• 16
• The multiplication is done first (3 * 5 = 15) and then the addition (2 + 15 = 17) and finally the subtraction (17 - 1 = 16).

1-9-5: Given the following code segment, what is the value of b when it finishes executing?

```double a = 9.6982;
int b = 12;
b = (int) a;
```
• 9.6982
• This would be true if it was b = a. What does the (int) do?
• 12
• This is the initial value of b, but then b is assigned to be the result of casting the value in a to an integer. Casting to an integer from a double will truncate (throw away) the digits after the decimal.
• 10
• Java does not round when converting from a double to an integer.
• 9
• When a double is converted into an integer in Java, it truncates (throws away) the digits after the decimal.

1-9-6: Given the following code segment, what is the value of `num` when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.

```double value = Math.random();
int num = (int) (value * 5) + 5;
```
• a random number from 0 to 4
• This would be true if it was (int) (Math.random * 5)
• a random number from 1 to 5
• This would be true if it was ((int) (Math.random * 5)) + 1
• a random number from 5 to 9
• Math.random returns a value from 0 to not quite 1. When you multiply it by 5 you get a value from 0 to not quite 5. When you cast to int you get a value from 0 to 4. Adding 5 gives a value from 5 to 9.
• a random number from 5 to 10
• This would be true if Math.random returned a value between 0 and 1, but it won't ever return 1. The cast to int results in a number from 0 to 4. Adding 5 gives a value from 5 to 9.

1-9-7: What does the follow code do when it is executed?

```System.out.println(5 / 0);
```
• It will print 0
• This would be true if it was System.out.println(0 / 5)
• It will give a run-time error
• You can't divide by 0 so this cause a run-time error.
• It will give a compile-time error (won't compile)
• You might think that this would be caught at compile time, but it isn't.
• It will print 5
• This would be true if it was System.out.println(5 / 1)

1-9-8: Given the following code segment, what is the value of `num` when it finishes executing? Math.random() returns a random decimal number between 0 and up to 1, for example 0.4.

```double value = Math.random();
int num = (int) (value * 11) - 5;
```
• a random number from 0 to 10
• This would be true if it was (int) (value * 11)
• a random number from 0 to 9
• This would be true if it was (int) (value * 10)
• a random number from -5 to 4
• This would be true if it was (int) (value * 10) - 5
• a random number from -5 to 5
• Math.random returns a random value from 0 to not quite 1. After it is multipied by 11 and cast to integer it will be a value from 0 to 10. Subtracting 5 means it will range from -5 to 5.

1-9-9: What will the following code print?

```System.out.println(1.0 / 3);
```
• 0
• This would be true if it was (1 / 3).
• .3
• It will give you more than just one digit after the decimal sign.
• 0.3333333333333333
• The computer can not represent an infinite number of 3's after the decimal point so it only keeps 14 to 15 significant digits.
• 0.3 with an infinite number of 3's following the decimal point
• The computer can not represent an infinite number of 3's after the decimal point.

1-9-10: What are the values of x, y, and z after the following code executes?

```int x = 3;
int y = x;
int z = x * y;
x++;
```
• x = 3, y = 3, z = 9
• This would be true if the x++ wasn't there.
• x = 4, y = 3, z = 9
• Fist x is set to 3, then y is also set to 3, and next z is set to 3 * 3 = 9. Finally x is incremented to 4.
• x = 0, y = 3, z = 0
• You might think that y = x means that y takes x's value, but y is set to a copy of x's value.
• x = 4, y = 4, z = 9
• You might think that y = x means that if x is incremented that y will also be incremented, but y = x only sets y to a copy of x's value and doesn't keep them in sync.