Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 4 Loops
Section 4.2 The while Loop
1
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
   System.out.println("Welcome to Java");
   count++;
}
A.
8
B.
9
C.
10
D.
11
E.
0
2
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
   System.out.println("Welcome to Java");
}
A.
8
B.
9
C.
10
D.
11
E.
0
3
Analyze the following code.
int count = 0;
while (count < 100) {
   // Point A
   System.out.println("Welcome to Java!");
   count++;
   // Point B
}
   // Point C
A.
count < 100 is always true at Point A
B.
count < 100 is always true at Point B
C.
count < 100 is always false at Point B
D.
count < 100 is always true at Point C
E.
count < 100 is always false at Point C
Section 4.3 The do-while Loop
4
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
   System.out.println("Welcome to Java");
   count++;
} while (count < 10);
A.
8
B.
9
C.
10
D.
11
E.
0
5
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
   System.out.println("Welcome to Java");
} while (count++ < 10);
A.
8
B.
9
C.
10
D.
11
E.
0
6
How many times will the following code print "Welcome to Java"?
int count = 0;
do {
   System.out.println("Welcome to Java");
} while (++count < 10);
A.
8
B.
9
C.
10
D.
11
E.
0
7
What is the value in count after the following loop is executed?
int count = 0;
do {
   System.out.println("Welcome to Java");
} while (count++ < 9);
System.out.println(count);
A.
8
B.
9
C.
10
D.
11
E.
0
Section 4.4 The for Loop
8
Analyze the following statement:
double sum = 0;
for (double d = 0; d<10;) {
   d += 0.1;
   sum += sum + d;
}
A.
The program has a syntax error because the adjustment is missing in the for loop.
B.
The program has a syntax error because the control variable in the for loop cannot be of the double type.
C.
The program runs in an infinite loop because d<10 would always be true.
D.
The program compiles and runs fine.
9
Do the following two statements result in the same value in sum?
for (int i = 0; i<10; ++i) {
   sum += i;
}
for (int i = 0; i<10; i++) {
   sum += i;
}
A.
Yes
B.
No
10
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
   y += i;
}
System.out.println(y);
A.
10
B.
11
C.
12
D.
13
E.
45
11
What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
   y += i;
}
A.
9
B.
10
C.
11
D.
undefined
12
Is the following loop correct?
for (; ; );
A.
Yes
B.
No
Section 4.5 Which Loop to Use?
13
Analyze the following fragment:
double sum = 0;
double d = 0;
while (d != 10.0) {
   d += 0.1;
   sum += sum + d;
}
A.
The program does not compile because sum and d are declared double, but assigned with integer value 0.
B.
The program never stops because d is always 0.1 inside the loop.
C.
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
D.
After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + ... + 1.9
14
Analyze the following code:
public class Test {
   public static void main (String args[]) {
     int i = 0;
     for (i = 0; i < 10; i++);
       System.out.println(i + 4);
   }
}
A.
The program has a syntax error because of the semicolon (;) on the for loop line.
B.
The program compiles despite the semicolon (;) on the for loop line, and displays 4.
C.
The program compiles despite the semicolon (;) on the for loop line, and displays 14.
D.
The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);
Section 4.9 (Optional) Keywords break and continue
15
Will the following program terminate?
int balance = 10;
while (true) {
   if (balance < 9) break;
   balance = balance - 9;
}
A.
Yes
B.
No
16
What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do {
   item++;
   sum += item;
   if (sum > 4) break;
}
while (item < 5);
A.
5
B.
6
C.
7
D.
8
17
What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do {
   item++;
   sum += item;
   if (sum >= 4) continue;
}
while (item < 5);
A.
15
B.
16
C.
17
D.
18
18
Will the following program terminate?
int balance = 10;
while (true) {
   if (balance < 9) continue;
   balance = balance - 9;
}
A.
Yes
B.
No
19
After the break outer statement is executed in the following loop, which statement is executed?
outer:
   for (int i = 1; i < 10; i++) {
   inner:
     for (int j = 1; j < 10; j++) {
       if (i * j > 50)
         break outer;
       System.out.println(i * j);
     }
   }
next:
A.
The statement labeled inner.
B.
The statement labeled outer.
C.
The statement labeled next.
D.
The program terminates.
20
After the continue outer statement is executed in the following loop, which statement is executed?
outer:
   for (int i = 1; i < 10; i++) {
   inner:
     for (int j = 1; j < 10; j++) {
       if (i * j > 50)
         continue outer;
       System.out.println(i * j);
     }
   }
next:
A.
The control is in the outer loop, and the next iteration of the outer loop is executed.
B.
The control is in the inner loop, and the next iteration of the inner loop is executed.
C.
The statement labeled next.
D.
The program terminates.
21
What is the number of iterations in the following loop:
   for (int i = 1; i < n; i++) {
     // iteration
   }
A.
2*n
B.
n
C.
n - 1
D.
n + 1
22
What is the number of iterations in the following loop:
   for (int i = 1; i <= n; i++) {
     // iteration
   }
A.
2*n
B.
n
C.
n - 1
D.
n + 1