Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (77 votes)
315 views

Introduction To Java Programming Comprehensive Version 10th Edition Liang Test Bank 1

The document contains a chapter on loops from a Java programming textbook. It includes 20 multiple choice questions about various loop constructs in Java like while, for, and do-while loops. It covers topics like loop termination conditions, nested loops, and the break and continue keywords. The questions test understanding of how loops work and when different loop types should be used.

Uploaded by

nancy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (77 votes)
315 views

Introduction To Java Programming Comprehensive Version 10th Edition Liang Test Bank 1

The document contains a chapter on loops from a Java programming textbook. It includes 20 multiple choice questions about various loop constructs in Java like while, for, and do-while loops. It covers topics like loop termination conditions, nested loops, and the break and continue keywords. The questions test understanding of how loops work and when different loop types should be used.

Uploaded by

nancy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

chapter5.

txt

Introduction to Java Programming


Comprehensive Version 10th Edition
Liang Test Bank
Full download at link:

Test Bank: https://testbankpack.com/p/test-bank-for-introduction-to-


java-programming-comprehensive-version-10th-edition-liang-
0133761312-9780133761313/

Solution Manual: https://testbankpack.com/p/solution-manual-


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) {
System.out.println("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
chapter5.txt

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
Key:ae

#
3. 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

Page 2
chapter5.txt

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 {
System.out.println("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 {
System.out.println("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 {
System.out.println("Welcome to Java");
} while (++count < 10);
a. 8
b. 9
c. 10
d. 11
e. 0
Key:c

Page 3
chapter5.txt

#
7. What is the value in count after the following loop is executed?

Page 4
chapter5.txt

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
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&lt;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++) {
System.out.println("Welcome to Java");
}

B:
for (int count = 0; count < 10; count++) {
System.out.println("Welcome to Java");
}

C:
for (int count = 1; count < 10; count++) {
System.out.println("Welcome to Java");

Page 5
chapter5.txt

Page 6
chapter5.txt

D:
for (int count = 0; count <= 10; count++) {
System.out.println("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);
}
System.out.println("Sum is " + sum);

B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
System.out.println("Sum is " + sum);

C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
System.out.println("Sum is " + sum);

D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
System.out.println("Sum is " + sum);

E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}

Page 7
chapter5.txt

System.out.println("Sum is " + sum);

Page 8
chapter5.txt

a. BCD
b. ABCD
c. B
d. CDE
e. CD
Key:e

#
3. The following loop displays _.

for (int i = 1; i <= 10; i++) {


System.out.print(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&lt;10; ++i) {
sum += i;
}

(II):
for (int i = 0; i&lt;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&lt;10; ++i) {
y += i;

Page 9
chapter5.txt

}
System.out.println(y);

Page 10
chapter5.txt

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&lt;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
chapter5.txt

Page 12
chapter5.txt

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 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++) { };
System.out.println(i + 4);
Key:cd This is a logic error. System.out.println(i + 4) is not a part the for loop
because the for loop ends with the last semicolon at for (i=0; i&lt;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++)
System.out.println(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
chapter5.txt

Section 5.9 Keywords break and continue


17. Will the following program terminate?

Page 14
chapter5.txt

int balance = 10;

while (true) {
if (balance &lt; 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 &gt; 4) break;
}
while (item &lt; 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;
}
}

System.out.println("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
chapter5.txt

d. i is 6 isPrime is false
Key:d

Page 16
chapter5.txt

#
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;
}
}

System.out.println("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 &gt;= 4) continue;
}
while (item &lt; 5);
a. 15
b. 16
c. 17
d. 18
Key:a

#
20. Will the following program terminate?

int balance = 10;

while (true) {
if (balance &lt; 9) continue;
balance = balance - 9;

Page 17
chapter5.txt

Page 18
chapter5.txt

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 java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();

int i;

boolean isPrime = true;


for (i = 2; i < number && isPrime; i++) {

Page 19
chapter5.txt

if (number % i == 0) {
isPrime = false;

Page 20
chapter5.txt

}
}

System.out.println("i is " + i);

if (isPrime)
System.out.println(number + " is prime");
else
System.out.println(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 java.util.Scanner;

public class Test {


public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner(System.in);
sum += input.nextInt();
}
}
}

a. The program does not compile because the Scanner input = new Scanner(System.in);
statement is inside the loop.
b. The program compiles, but does not run because the Scanner input = new
Scanner(System.in); 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(System.in); 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
chapter5.txt

1. You can always convert a for loop to a while loop.


a. true
b. false

Page 22
chapter5.txt

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>&nbsp; // body<br>}<br><br>to a for loop as follows:<br><br>for (;
;)<br>{<br> &nbsp;&nbsp; if (continuation-condition)<br> &nbsp; {<br>
&nbsp;&nbsp;&nbsp; // body <br> &nbsp; }<br> &nbsp; else <br> &nbsp;&nbsp;&nbsp;
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
chapter5.txt

24. Which of the loop statements always have their body executed at least once.
a. The while loop

Page 24
chapter5.txt

b. The do-while loop


c. The for loop
Key:b

#
25. Analyze the following code.

int x = 1;
while (0 < x) && (x < 100)
System.out.println(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
chapter5.txt

Page 26
chapter5.txt

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
chapter5.txt

36. What is the output of the following fragment?

Page 28
chapter5.txt

int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
System.out.println(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)
System.out.print(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++)
System.out.print(j + " ");
System.out.println();
}
(II)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}

(III)

Page 15
chapter5.txt

int i = 0;
while (i < 5) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i++;
}
(IV)
int i = 5;
while (i > 0) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
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 ( ; ; )
System.out.println("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 ; )
System.out.println("Welcome to Java");
a. does not print anything.

Page 16
chapter5.txt
b. prints out Welcome to Java one time.

Page 17
chapter5.txt

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)(Math.random() * 100)
b. (int)(Math.random() * 101)
c. (int)(Math.random() * 100) + 1
d. (int)(Math.random() * 100 + 1)
Key:b

Page 18

You might also like