CS 4A: Chapter 5 - Loops

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the output of the following fragment? for (int i = 0; i < 15; i++) { if (i % 4 == 1) System.out.print(i + " "); }

1 5 9 13

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

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

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 are the following loops 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

What is the output of the following code: for ( ; false ; ) System.out.println("Welcome to Java");

Does not print anything

A break statement can be used only in a loop. True/False

False

A variable declared in the for loop control can be used after the loop exits. True/False

False

Which of the following loops produces the following output? (choose all that apply) 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) 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--; }

I & IV

Suppose cond1 is a Boolean expression. When will this while condition be true? while ( !cond1) ...

If cond1 is false

What balance after the following code is executed? int balance = 10; while (balance >= 1) { if (balance < 9) continue; balance = balance - 9; }

The loop does not end

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

The program compiles and runs fine.

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

What is 1.0 + 1.0 + 1.0 == 3.0?

There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.

A continue statement can be used only in a loop. True/False

True

The elements inside the for loop control are separated using semicolons instead of commas. True/False

True

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. True/False

True

You can always convert a for loop to a while loop. True/False

True

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

True

You can always write a program without using break or continue in a loop. True/False

True

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

Undefined

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

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

Assume x is 0. What is the output of the following statement? if (x > 0) System.out.println("x is greater than 0"); else if (x < 0) System.out.println("x is less than 0"); else System.out.println("x equals 0");

x = 0

Which of the following expression yields an integer between 0 and 100, inclusive?

(int)(Math.random() * 101)

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

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

1


संबंधित स्टडी सेट्स

MKTG 4120 Consumer Behavior Final (ch 18)

View Set

Hultman, The Call of the Wild, Chapter 4 Study Guide

View Set

CSS Layout - The display Property

View Set

NUR 3420 Pharmacology PrepU Chapter 34, Pharm: Chapter 34 therapy for fluid volume, 32: Drug Therapy for Fluid Volume Excess, Chapter 34: Drug Therapy for Fluid Volume Excess, Chapter 34: Drug Therapy for Fluid Volume Excess, 28: Drug Therapy for Hyp…

View Set

Worksheet 2: Two Houses of Congress

View Set

Hesi Final Review from Spring 2016

View Set

Chapter 8 - Managing Change and Innovation

View Set

The Heart - Lecture Practice Test

View Set

Saunders Musculoskeletal - 64, 65, 43

View Set