chapter5.
txt
Introduction to Java Programming
Comprehensive Version 10th Edition
Liang Test Bank
Full download at link:
Test Bank: [Link]
java-programming-comprehensive-version-10th-edition-liang-
0133761312-9780133761313/
Solution Manual: [Link]
for-introduction-to-java-programming-comprehensive-
version-10th-edition-liang-0133761312-9780133761313/
Chapter 5 Loops
Section 5.2 The while Loop
1. How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
[Link]("Welcome to Java");
count++;
}
a. 8
b. 9
c. 10
d. 11
e. 0
Key:c
#
2. Analyze the following code.
int count = 0;
Page 1
[Link]
while (count < 100) {
// Point A
[Link]("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
Key:ae
#
3. How many times will the following code print "Welcome to Java"?
int count = 0;
while (count++ < 10) {
[Link]("Welcome to Java");
}
a. 8
b. 9
c. 10
d. 11
e. 0
Page 2
[Link]
Key:c
#
Section 5.3 The do-while Loop
4. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
[Link]("Welcome to Java");
count++;
} while (count < 10);
a. 8
b. 9
c. 10
d. 11
e. 0
Key:c
#
5. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
[Link]("Welcome to Java");
} while (count++ < 10);
a. 8
b. 9
c. 10
d. 11
e. 0
Key:d
#
6. How many times will the following code print "Welcome to Java"?
int count = 0;
do {
[Link]("Welcome to Java");
} while (++count < 10);
a. 8
b. 9
c. 10
d. 11
e. 0
Key:c
Page 3
[Link]
#
7. What is the value in count after the following loop is executed?
Page 4
[Link]
int count = 0;
do {
[Link]("Welcome to Java");
} while (count++ < 9);
[Link](count);
a. 8
b. 9
c. 10
d. 11
e. 0
Key:c
#
Section 5.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 compile error because the adjustment is missing in the for
loop.
b. The program has a compile 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.
Key:d
#
3. Which of the following loops prints "Welcome to Java" 10 times?
A:
for (int count = 1; count <= 10; count++) {
[Link]("Welcome to Java");
}
B:
for (int count = 0; count < 10; count++) {
[Link]("Welcome to Java");
}
C:
for (int count = 1; count < 10; count++) {
[Link]("Welcome to Java");
Page 5
[Link]
Page 6
[Link]
D:
for (int count = 0; count <= 10; count++) {
[Link]("Welcome to Java");
}
a. BD
b. ABC
c. AC
d. BC
e. AB
Key:e
#
3. Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?
A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
[Link]("Sum is " + sum);
B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
[Link]("Sum is " + sum);
C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
[Link]("Sum is " + sum);
D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
[Link]("Sum is " + sum);
E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
Page 7
[Link]
[Link]("Sum is " + sum);
Page 8
[Link]
a. BCD
b. ABCD
c. B
d. CDE
e. CD
Key:e
#
3. The following loop displays _.
for (int i = 1; i <= 10; i++) {
[Link](i + " ");
i++;
}
a. 1 2 3 4 5 6 7 8 9
b. 1 2 3 4 5 6 7 8 9 10
c. 1 2 3 4 5
d. 1 3 5 7 9
e. 2 4 6 8 10
Key:d
#
9. Do the following two statements in (I) and (II) result in the same value in
sum?
(I):
for (int i = 0; i<10; ++i) {
sum += i;
}
(II):
for (int i = 0; i<10; i++) {
sum += i;
}
a. Yes
b. No
Key:a
#
10. What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
Page 9
[Link]
}
[Link](y);
Page 10
[Link]
a. 10
b. 11
c. 12
d. 13
e. 45
Key:e y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 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
Key:d The scope of i is inside the loop. After the loop, i is not defined.
#
12. Is the following loop correct?
for (; ; );
a. Yes
b. No
Key:a
#
Section 5.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
Key:c
Page 11
[Link]
Page 12
[Link]
14. Analyze the following code:
public class Test {
public static void main (String args[]) {
int i = 0;
for (i = 0; i < 10; i++);
[Link](i + 4);
}
}
a. The program has a compile 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++) { };
[Link](i + 4);
Key:cd This is a logic error. [Link](i + 4) is not a part the for loop
because the for loop ends with the last semicolon at for (i=0; i<10; i++);
#
Section 5.6 Nested Loops
15. How many times is the println statement executed?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
[Link](i * j)
a. 100
b. 20
c. 10
d. 45
Key:d
#
Section 5.7 Minimizing Numerical Errors
16. To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to
get better accuracy?
a. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is
0.
b. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose
initial value is 0.
Key:a
Page 13
[Link]
Section 5.9 Keywords break and continue
17. Will the following program terminate?
Page 14
[Link]
int balance = 10;
while (true) {
if (balance < 9) break;
balance = balance - 9;
}
a. Yes
b. No
Key:a
#
18. 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
Key:b
#
18. What is the printout after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
if (number % i == 0) {
isPrime = false;
}
}
[Link]("i is " + i + " isPrime is " + isPrime);
a. i is 5 isPrime is true
b. i is 5 isPrime is false
c. i is 6 isPrime is true
Page 15
[Link]
d. i is 6 isPrime is false
Key:d
Page 16
[Link]
#
18. What is the printout after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false; break;
}
}
[Link]("i is " + i + " isPrime is " + isPrime);
a. i is 5 isPrime is true
b. i is 5 isPrime is false
c. i is 6 isPrime is true
d. i is 6 isPrime is false
Key:b
#
19. 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
Key:a
#
20. Will the following program terminate?
int balance = 10;
while (true) {
if (balance < 9) continue;
balance = balance - 9;
Page 17
[Link]
Page 18
[Link]
a. Yes
b. No
Key:b
#
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
Key:c
#
23. 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
Key:b
#
24. Suppose the input for number is 9. What is the output from running the following
program?
import [Link];
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter an integer: ");
int number = [Link]();
int i;
boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
Page 19
[Link]
if (number % i == 0) {
isPrime = false;
Page 20
[Link]
}
}
[Link]("i is " + i);
if (isPrime)
[Link](number + " is prime");
else
[Link](number + " is not prime");
}
}
a. i is 3 followed by 9 is prime
b. i is 3 followed by 9 is not prime
c. i is 4 followed by 9 is prime
d. i is 4 followed by 9 is not prime
Key:d
#
24. Analze the following code:
import [Link];
public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner([Link]);
sum += [Link]();
}
}
}
a. The program does not compile because the Scanner input = new Scanner([Link]);
statement is inside the loop.
b. The program compiles, but does not run because the Scanner input = new
Scanner([Link]); statement is inside the loop.
c. The program compiles and runs, but it is not effient and unnecessary to execute
the Scanner input = new Scanner([Link]); statement inside the loop. You should
move the statement before the loop.
d. The program compiles, but does not run because there is not prompting message for
entering the input.
Key:c
Page 21
[Link]
1. You can always convert a for loop to a while loop.
a. true
b. false
Page 22
[Link]
Key:a
#
2. You can always convert a while loop to a for loop.
a. true
b. false
Key:a You can rewrite the following while loop <p>while (continuation-condition)
<br>{ <br> // body<br>}<br><br>to a for loop as follows:<br><br>for (;
;)<br>{<br> if (continuation-condition)<br> {<br>
// body <br> }<br> else <br>
exit;<br>}<br>
#
3. The while loop and the do loop are equivalent in their expressive power; in
other words, you can rewrite a while loop using a do loop, and vice versa.
a. true
b. false
Key:a
#
4. You can always write a program without using break or continue in a loop.
a. true
b. false
Key:a
#
5. The elements inside the for loop control are separated using semicolons
instead of commas.
a. true
b. false
Key:a
#
11. A break statement can be used only in a loop.
a. true
b. false
Key:b A break statement can be used inside a switch statement too. A break statement
can only be used with a loop and a switch statement.
#
12. A continue statement can be used only in a loop.
a. true
b. false
Key:a
Page 23
[Link]
24. Which of the loop statements always have their body executed at least once.
a. The while loop
Page 24
[Link]
b. The do-while loop
c. The for loop
Key:b
#
25. Analyze the following code.
int x = 1;
while (0 < x) && (x < 100)
[Link](x++);
a. The loop runs forever.
b. The code does not compile because the loop body is not in the braces.
c. The code does not compile because (0 < x) && (x < 100) is not enclosed in a
pair of parentheses.
d. The numbers 1 to 99 are displayed.
e. The numbers 2 to 100 are displayed.
Key:c
#
26. Analyze the following code.
double sum = 0;
for (double d = 0; d < 10; sum += sum + d) {
d += 0.1;
}
a. The program has a syntax error because the adjustment statement is incorrect
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 compiles but does not stop because d would always be less than
10.
d. The program compiles and runs fine.
Key:d
#
27. What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}
a. 9
b. 10
c. 11
d. 12
Key:b
Page 25
[Link]
Page 26
[Link]
28. What balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9) continue;
balance = balance - 9;
}
a. -1
b. 0
c. 1
d. 2
e. The loop does not end
Key:e
#
29. What is the value of balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9) break;
balance = balance - 9;
}
a. -1
b. 0
c. 1
d. 2
Key:c
#
35. Assume x is 0. What is the output of the following statement?
if (x > 0)
printf("x is greater than 0");
else if (x < 0)
printf("x is less than 0");
else
printf("x equals 0");
a. x is greater than 0
b. x is less than 0
c. x equals 0
d. None
Key:c
Page 27
[Link]
36. What is the output of the following fragment?
Page 28
[Link]
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
[Link](j);
a. 4
b. 8
c. 16
d. 32
e. 64
Key:c
#
37. What is the output of the following fragment?
for (int i = 0; i < 15; i++) {
if (i % 4 == 1)
[Link](i + " ");
}
a. 1 3 5 7 9 11 13 15
b. 1 5 9 13
c. 1 5 9 13 16
d. 1 3 5 7 9 11 13
e. 1 4 8 12
Key:b
#
38. Which of the following loops produces the output
1 2 3 4 1 2 3 1 2 1
(I)
for (int i = 5; i > 0; i--) {
for (int j = 1; j < i; j++)
[Link](j + " ");
[Link]();
}
(II)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < i; j++)
[Link](j + " ");
[Link]();
}
(III)
Page 15
[Link]
int i = 0;
while (i < 5) {
for (int j = 1; j < i; j++)
[Link](j + " ");
[Link]();
i++;
}
(IV)
int i = 5;
while (i > 0) {
for (int j = 1; j < i; j++)
[Link](j + " ");
[Link]();
i--;
}
a. (I)
b. (II)
c. (III)
d. (IV)
Key:ad
#
41. In a for statement, if the continuation condition is blank, the condition is
assumed to be _.
a. true
b. false
Key:a
#
43. What the output of the following code:
for ( ; ; )
[Link]("Welcome to Java");
a. does not print anything.
b. prints out Welcome to Java one time.
c. prints out Welcome to Java two times.
d. prints out Welcome to Java forever.
Key:d
#
44. What the output of the following code:
for ( ; false ; )
[Link]("Welcome to Java");
a. does not print anything.
Page 16
[Link]
b. prints out Welcome to Java one time.
Page 17
[Link]
c. prints out Welcome to Java two times.
d. prints out Welcome to Java forever.
Key:a
#
10. A variable declared in the for loop control can be used after the loop
exits.
a. true
b. false
Key:b
#
34. Suppose cond1 is a Boolean expressions. When will this while condition be true?
while (cond1) ...
a. in case cond1 is true
b. in case cond1 is false
c. always true
d. always false
Key:a
#
41. Which of the following expression yields an integer between 0 and 100,
inclusive?
a. (int)([Link]() * 100)
b. (int)([Link]() * 101)
c. (int)([Link]() * 100) + 1
d. (int)([Link]() * 100 + 1)
Key:b
Page 18