CSC Chapter 5

Ace your homework & exams now with Quizwiz!

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

1 3 5 7 9

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

10

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

10

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

10

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

10

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

10

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

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

45

What is the output for y? int y = 0; for (int i = 0; i&lt;10; ++i) { y += i; } System.out.println(y); a. 10 b. 11 c. 12 d. 13 e. 45

45

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

6

Will the following program terminate? int balance = 10; while (true) { if (balance &lt; 9) continue; balance = balance - 9; } a. Yes b. No

No

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.

The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

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

Yes

Is the following loop correct? for (; ; ); a. Yes b. No

Yes

Will the following program terminate? int balance = 10; while (true) { if (balance &lt; 9) break; balance = balance - 9; } a. Yes b. No

Yes

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

count < 100 is always true at Point A count < 100 is always false at Point C

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

i is 5 isPrime is false

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 d. i is 6 isPrime is false

i is 6 isPrime is false

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

n

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

n-1

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

undefined


Related study sets

Metrix Learning CompTIA A+ 220-1102 Review Questions

View Set

Grammar Unit 4- Identifying Types of Phrases

View Set

English 3 Study Guide Crucible Act 1

View Set

Chapter 5 International Business test 1

View Set