Chapter 5
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
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 e. count < 100 is always false at point C
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
Is the following loop correct? for(; ;);
a. yes
Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; }
a. yes
The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }
d. 1 3 5 7 9
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)
d. 45
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);
d. i is 6 isPrime is false
What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; }
d. undefined
What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y);
e. 45
Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; }
c. the program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
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);
b. 6
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);
b. i is 5 isPrime is false
What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration }
b. n
What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration }
b. n - 1
Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }
b. no
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); }
c. 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);
c. 10
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10);
c. 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++; }
c. 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);
c. 10