Multiple Choice ExercisesΒΆ
- 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-11-1: What does the following code print?
System.out.println(2 % 3);
- 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-11-2: What does the following code print?
System.out.println(19 % 5);
- 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-11-3: What does the following code print?
System.out.println(1 / 3);
- 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-11-4: What does the following code print?
System.out.println(2 + 3 * 5 - 1);
- 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-11-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;
- 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-11-6: What does the following code do when it is executed?
System.out.println(5 / 0);
- 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-11-7: What will the following code print?
System.out.println(1.0 / 3);
- x = 3, y = 3, z = 9
- This would be true if the x++ wasn't there.
- x = 4, y = 3, z = 9
- First 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.
1-11-8: 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++;
You have attempted of activities on this page