Java Chapter 5 Loops

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

yes

Is the following loop correct? for (; ; );

yes

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9;

yes

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);

15

The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }

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"); count++; } while (count < 10);

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);

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

10

How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); }

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);

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);

11

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)

45

What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y);

45

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);

6

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"); }

AB

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);

CD

Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C

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

Will the following program terminate? int balance = 10; while (true) { if (balance < 9) continue; balance = balance - 9; }

No

Analyze the following statement: double sum = 0; for (double d = 0; d < 10;) { d += 0.1; sum += sum + d; }

The program compiles and runs fine.

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(); } } }

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.

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

The program compiles despite the semicolon (;) on the for loop line, and displays 14. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4); Explanation: 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<10; i++);

Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; }

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

To add 0.01 + 0.02 + ... + 1.00, what order should you use to add the numbers to get better accuracy?

add 0.01, 0.02, ..., 1.00 in this order to a sum variable whose initial value is 0

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"); } }

i is 4 followed by 9 is not prime

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

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

i is 6 isPrime is false

What is the number of iterations in the following loop? for (int i = 1; i <= n; i++) { // iteration }

n

What is the number of iterations in the following loop? for (int i = 1; i < n; i++) { // iteration }

n - 1

What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; }

undefined


Set pelajaran terkait

Ch 21 Nursing Care of a Family Experiencing a Sudden Pregnancy Complication

View Set

Strength Based Interview Questions

View Set

A&P II Cardiolody/Blood Vessels Exam 1

View Set

Environmental Biology Chapter 15

View Set

Accounting Final Exam Study Guide

View Set

AP Exam Multiple Choice questions 1-45

View Set