Skip to main content
Contents Index
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 1.19 Multiple Choice Exercises for Unit 1a (1.1-1.6)
Activity 1.19.1 .
What does the following code print?
System.out.println(2 % 3);
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.
This is the number of times that 3 goes into 2 but the % operator gives you the remainder.
Try it. Remember that % gives you the remainder after you divide the first number by the second one.
This would be correct if it was 3 % 2 since 2 would go into 3 one time with a remainder of 1.
Activity 1.19.2 .
What does the following code print?
System.out.println(19 % 5);
This is the number of times that 5 goes into 19, but % is the remainder.
This would only be true if the first number was evenly divisible by the second number.
5 goes into 19 3 times (15) with a remainder of 4 (19-15=4)
This would be correct if it was 19 % 2, but here we are dividing by 5.
Activity 1.19.3 .
What does the following code print?
System.out.println(1 / 3);
This would be correct if it was 1.0 / 3 or 1 / 3.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.
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.
Activity 1.19.4 .
What does the following code print?
System.out.println(2 + 3 * 5 - 1);
This would be true if it was System.out.println(((2 + 3) * 5) - 1), but without the parentheses the multiplication is done first.
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.
The multiplication is done first (3 * 5 = 15) and then the addition (2 + 15 = 17) and finally the subtraction (17 - 1 = 16).
Activity 1.19.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;
This would be true if it was b = a. What does the (int) do?
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.
Java does not round when converting from a double to an integer.
When a double is converted into an integer in Java, it truncates (throws away) the digits after the decimal.
Activity 1.19.6 .
What does the following code do when it is executed?
System.out.println(5 / 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.
This would be true if it was System.out.println(5 / 1)
Activity 1.19.7 .
What will the following code print?
System.out.println(1.0 / 3);
This would be true if it was (1 / 3).
It will give you more than just one digit after the decimal sign.
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.
Activity 1.19.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++;
This would be true if the x++ wasnβt there.
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.
You might think that y = x means that y takes xβs value, but y is set to a copy of xβs value.
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.
You have attempted
of
activities on this page.