3.10. Easy Multiple Choice QuestionsΒΆ
These problems are easier than most of those that you will usually see on the AP CS A exam.
 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.
391: 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 (1915=4)
 1
 This would be correct if it was 19 % 2, but here we are dividing by 5.
392: 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 runtime error
 You would get a runtime 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 compiletime error
 Integer division is allowed in Java. It gives an integer result.
393: 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).
394: 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.
395: 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;
 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.
396: Given the following code segment, what is the value of num
when it finishes executing?
double value = Math.random(); int num = (int) (value * 5) + 5;
 It will print 0
 This would be true if it was System.out.println(0 / 5)
 It will give a runtime error
 You can't divide by 0 so this cause a runtime error.
 It will give a compiletime 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)
397: What does the follow code do when it is executed?
System.out.println(5 / 0);
 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.
398: Given the following code segment, what is the value of num
when it finishes executing?
double value = Math.random(); int num = (int) (value * 11)  5;
 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.
399: 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
 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.
3910: 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++;

3911: Which of the following is the decimal value for the binary number 1001011?
 75
 To convert from binary to decimal use the powers of 2 starting with 2 raised to the 0 power which is 1. So 1001011 is 1 + 2 + 8 + 64 = 75.
 67
 This would be true if the binary number was 1000011. This would be 1 + 2 + 64 = 67.
 150
 This would be true if we started at the right hand side with 2, but we start with 1 (2 raised to the 0 power is 1).
 43
 This would be true if the binary number was 101011.
 74
 This would be true if the binary number was 1001010.

3912: How many bits would you need to represent 25 distinct values?
 5
 This would be enough to represent 32 distinct values, so that is more then enough to represent 25 distinct values.
 4
 This would only be enough to represent 16 distinct values (2 to the 4th).
 6
 This is more than you need. 2 to the 6th is 64.

3913: What is the hexadecimal equivalent of the following binary number: 110100?
 34
 To convert from binary to hexadecimal (base 16) convert groups of 4 bits from the right to the left to hexadecimal. The rightmost 4 bits is 0100 which is 4 in hex. The leftmost 4 bits would be 0011 which is 3 in hex.
 52
 This would be correct if the question had asked for the value in decimal, but it asked for it in hexadecimal (base 16).
 64
 This would be correct if the question had asked for the value in octal, but it asked for it in hexadecimal (base 16).
 6
 This is the length of this binary number. Can you convert it to hexadecimal (base 16)?
 B4
 This would be correct if the binary number was 10110100 instead of 110100.

3914: What is the binary equivalent of the following base 10 number: 187?
 11011101
 This has an extra one in the front.
 1011001
 The decimal value of 1011001 is (1*64)+ (0 * 32) + (1 * 16) + (1 * 8) + (0 * 4) + (0 * 2) + (1*1) = which is 89 base 10 (decimal)
 10111011
 Using base 2 the value of 10111011 is: 128+32+16+8+2+1 = 187