Introduction to Java Programming: Ch. 5 quiz

Ace your homework & exams now with Quizwiz!

T or F: 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.

t

T or F: You can always convert a while loop to a for loop.

t

T or F: You can always write a program without using break or continue in a loop.

t

T or F: A continue statement can be used only in a loop.

t

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. 11 B. 10 C. 9 D. 8 E. 0

a

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. 18 D. 17

a

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 6 isPrime is false B. i is 5 isPrime is true C. i is 5 isPrime is false D. i is 6 isPrime is true

a

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"); } D: for (int count = 0; count <= 10; count++) { System.out.println("Welcome to Java"); } A. AB B. BC C. BD D. ABC E. AC

a

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; } A. Yes B. No

a

Analyze 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 and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop. C. The program compiles, but does not run because there is not prompting message for entering the input. D. The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop.

b

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 less than 0 B. x equals 0 C. None D. x is greater than 0

b

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. No B. Yes

b

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. 11 B. 10 C. 9 D. 0 E. 8

b

T or F: In a for statement, if the continuation condition is blank, the condition is assumed to be ______.

t

To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy? A. add 1.00, 0.99, 0.98, ..., 0.02, 0.01 in this order to a sum variable whose initial value is 0. B. add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0.

b

Which of the loop statements always have their body executed at least once. A. The for loop B. The do-while loop C. The while loop

b

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. 9 B. 11 C. 10 D. 8 E. 0

c

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 C B. count < 100 is always false at Point B C. count < 100 is always true at Point A D. count < 100 is always true at Point B E. count < 100 is always false at Point C

c e

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++) { if (number % i == 0) { isPrime = false; } } 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 4 followed by 9 is prime B. i is 3 followed by 9 is not prime C. i is 4 followed by 9 is not prime D. i is 3 followed by 9 is prime

c

What is the output of the following code: for ( ; ; ) System.out.println("Welcome to Java"); A. prints out Welcome to Java two times. B. prints out Welcome to Java one time. C. prints out Welcome to Java forever. D. does not print anything

c

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. 0 B. 2 C. 1 D. -1

c

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

d

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 B. 1 3 5 7 9 11 13 15 C. 1 5 9 13 16 D. 1 5 9 13 E. 1 4 8 12

d

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); } System.out.println("Sum is " + sum); A. ABCD B. BCD C. B D. CD E. CDE

d

T or F: The elements inside the for loop control are separated using semicolons instead of commas.

t

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. 0 B. 8 C. 11 D. 9 E. 10

e

T or F: A variable declared in the for loop control can be used after the loop exits.

f


Related study sets

Why was there a revolution in Iran 1979?

View Set

Chapter 14: Assessing Skin, Hair, and Nails

View Set

Sociology terms: Statuses and Roles

View Set

Accounting Quiz Notes, 1. ACC 310F Final Exam Review, Accounting 310F Final, ACC 310F Final Exam: (Exam 3), ACC 310F Final, Exam 3 - Quizzes

View Set

Cognitive Psychology: Exam 1 Yandell

View Set