Iteration

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

What will this while loop do? while(true) { System.out.println("Hello"); }

Print Hello in an infinite loop

Why does the following code produce an infinite loop? Choose the best answer. public static void main(String[] args) { int x = 7; while (x > 0) { System.out.println(x); } }

The loop doesn't ever change the value of x in the loop, so it never ends.

Why do we use while loops in Java?

To repeat some code while a condition is true

Which statement could be added to the while loop below so that it doesn't run indefinitely? public static void main(String[] args) { int x = 7; while (x > 0) { System.out.println(x); } }

You can add x-- after the print statement. You can add x = -1 after the print statement.

How many stars are output when the following code is executed? for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { System.out.println("*"); } }

50 stars

What is printed as a result of executing the following code snippet? for (int k = 0; k < 25; k = k + 2) { if (k % 3 == 0) { System.out.print(k + " "); } }

0 6 12 18 24

How many times will the following code print out the value of x? public static void main(String[] args) { int x = 1; while (x > 10) { System.out.println(x); x--; } }

0 times

What does the following code print? for (int i = 2; i < 9; i++) { System.out.print(i + ", "); }

2, 3, 4, 5, 6, 7, 8,

How often is the inner loop of a nested loop run?

Each time the outer loop runs

What does the following code print? int x = -4; while (x < 0) { x++; System.out.print(x + " "); }

-3 -2 -1 0

Which statement can we use inside of a loop to end it?

break;

Which of the following code snippets will produce the output below? 1 1 1 1 2 2 2 3 3 4

for (int j = 1; j <= 4; j++) { for (int k = 4; k >= j; k--) { System.out.print(j + " "); } System.out.println(); }

What does the following code print? for (int i = 1; i < 6; i++) { for (int y = 1; y <= 4; y++) { System.out.print("*"); } System.out.println(); }

A rectangle of 5 rows with 4 stars per row.

Consider the following loop, where n is some positive integer. for (int i = 0; i < n; i += 2) { if (/* condition to test */) { /* perform some action */ } } In terms of n, which Java expression represents the maximum number of times that /* perform some action */ could be executed?

(n + 1) / 2

Which for loop will properly print "hello" 10 times? A for(int i = 0; i < 10; i++) { System.out.println("hello"); } B for(int i = 10) { System.out.println("hello"); } C for(int i = 0; i++; i < 10) { System.out.println("hello"); } D for(int i = 10; i < 0; i++) { System.out.println("hello"); }

A

Why do we use for loops in Java?

To repeat something for a fixed number of times


Set pelajaran terkait

MAN3025.521S19QuizzesQuiz 6 (Ch. 18 & 11)

View Set

Pharmacology Chapter One Questions

View Set

Econ 102 plq 10 elasticity of demand

View Set

Unit 5 -Agriculture and Rural Land-Use Patterns and Processes

View Set