Skip to main content\(
\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 7.2 Loops-WE1-P1
Subgoals for Evaluating a Loop.
-
-
Determine start condition
-
Determine update condition
-
Determine termination condition
-
Determine body that is repeated
-
-
For every iteration of loop, write down values
Subsection 7.2.1
Exercises Exercises
1.
Q1: What is the output of the following loop?
int i = 0;
while (i < 3) {
System.out.println("hi");
i++;
}
Figure 7.2.1.
See diagram for answer A
Incorrect
See diagram for answer B
Incorrect
See diagram for answer C
Correct
See diagram for answer D
Incorrect
See diagram for answer E
Incorrect
2.
Q2: What is the output of the following loop?
int i = 10;
while (i > 1) {
System.out.print(i + " ");
i--;
}
10 9 8 7 6 5 4 3 2 1
Incorrect
9 8 7 6 5 4 3 2 1
Incorrect
9 8 7 6 5 4 3 2
Incorrect
10 9 8 7 6 5 4 3 2
Correct
compiler error
Incorrect
3.
Q3: What is the output of the following loop?
int i = 10;
while (i > 1) {
System.out.print(i + " ");
i++;
}
10 11 12 13 14 15 16
Incorrect
10 9 8 7 6 5 4 3 2
Incorrect
10 9 8 7 6 5 4 3 2 1
Incorrect
1 2 3 4 5 6 7 8 9 10
Incorrect
infinite loop
Correct
4.
Q4: What is the output of the following loop?
int i = 0;
int total = 0;
while (i <= 50) {
total += i;
i += 5;
}
System.out.println(i);
0 5 10 15 20 25 30 35 40 45 50
Incorrect
0 5 10 15 20 25 30 35 40 45
Incorrect
5 10 15 20 25 30 35 40 45 50
Incorrect
5 10 15 20 25 30 35 40 45
Incorrect
55
Correct
5.
Q5: What is the value of
counter after the execution of the folowing code?
int counter = 0;
while (counter > 100) {
if (counter % 2 == 1)
System.out.println(counter + " is odd.");
else
System.out.println(counter + " is even.");
}
counter++;
System.out.println(counter);
0
Incorrect
1
Correct
99
Incorrect
100
Incorrect
101
Incorrect
You have attempted
of
activities on this page.