Unit 4: Iteration

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

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 n / 2 (n - 1) / 2 n n-1

(n + 1) / 2

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

-3 -2 -1 0

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 0 3 6 9 12 15 18 21 24 0 2 4 6 8 10 12 14 16 18 20 22 24 This produces an infinite loop.

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--; } } 10 times 9 times 1 time 0 times

0 times

Given String str = "RETRIEVER"; int index = str.substring(1, 4).indexOf("R"); what is the value of index? 0 1 2 -1 5

2

What does the following code print? for (int i = 2; i < 9; i++) { System.out.print(i + ", "); } 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, This code will produce an infinite loop.

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

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("*"); } } 5 stars 10 stars 15 stars 50 stars 500 stars

50 stars

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

Which of the following will execute without throwing an exception error? String str1 = "Golden"; String str2 = "Retriever"; if (str1.equals(str2)) { System.out.println("Golden Retriever!"); } String str1 = "Golden"; String str2 = str1.substring(4); System.out.println(str1 + str2); None of the above A and B above.

A and B above.

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 6 rows with 4 stars per row. A rectangle of 6 rows with 3 stars per row. A rectangle of 5 rows with 4 stars per row. A rectangle of 4 rows with 5 stars per row.

A rectangle of 5 rows with 4 stars per row.

Why is having efficient algorithms important? It reduces the cost of running a program. It can improve the speed that programs operate. It increases the speed of innovations. Both 1 and 2. Both 2 and 3. Answered

Both 1 and 2.

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. You can add x++ after the print statement. Both A and B above.

Both A and B above.

Consider the mystery method below. public static String mystery(String str1, String str2) { int index = str1.indexOf(str2); return str1.substring(index, index + str2.length()); } What is true about mystery? A. It may return a string that is equal to str2. B. It may return a string that has no characters in common with str2. C. It may return a string that is equal to str1. Both A and C are true None of these choices will be true. Answered

Both A and C are true

What will this while loop do? while(true) { System.out.println("Hello"); } Print Hello one time Print Hello in an infinite loop Print Hello until program receives user input Nothing

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. The conditional with the while will never be true, so actually the code will never execute. There's no break statement to end the code. The code cannot contain just a print statement.

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 break out of some block of code To do something if a condition is true To repeat some code while a condition is true To repeat something for a fixed number of times

To repeat some code while a condition is true

Why do we use for loops in Java? To break out of some block of code To do something if a condition is true To do something while a condition is true To repeat something for a fixed number of times

To repeat something for a fixed number of times

ow many times does the following loop iterate? // x has been initialized with a positive int value for (int count = 0; count <= x; count++) { System.out.print("*"); } x times x + 1 times x - 1 times Infinite number of times The condition is never true so this loop never executes.

x + 1 times

How many times does the following loop execute? // x has been initialized with a positive int value greater than 5 int count = 5; while (count < x) { count++; } x times x - 1 times x - 5 times Infinite number of times The condition is never true so this loop never executes.

x - 5 times

How many times does the following loop iterate? // x has been initialized with a positive int value int count = 0; while (count < x) { count++; } x times x + 1 times x - 1 times Infinite number of times The condition is never true so this loop never executes.

x times

How often is the inner loop of a nested loop run? Infinitely Twice as often as the outer loop Each time the outer loop runs One less time than the outer loop runs

Each time the outer loop runs

Consider the following method. public void processString (String str) { str = str.substring(2, 3) + str.substring(1, 2) + str.substring(0, 1); } What is printed as result of executing the following statements (in a method in the same class)? String str = "Frog"; processString(str); System.out.println(str); Frog orF ogroF ogroFr StringIndexOutOfBoundsException

Frog

Which statement can we use inside of a loop to end it? System.out.println; break; SENTINEL; else;

break;

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 3? int count = 0; while (count < 5) { System.out.println("CodeHS Rocks!"); count++; } // point 3 count < 5 is always true at point 3 count < 5 always false at point 3 count < 5 is sometimes true/sometimes false at point 2

count < 5 always false at point 3

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 1? int count = 0; while (count < 5) { // point 1 System.out.println("CodeHS Rocks!"); count++; } count < 5 is always true at point 1 count < 5 always false at point 1 count < 5 is sometimes true/sometimes false at point 1

count < 5 is always true at point 1

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 2? int count = 0; while (count < 5) { System.out.println("CodeHS Rocks!"); count++; // point 2 } count < 5 is always true at point 2 count < 5 always false at point 2 count < 5 is sometimes true/sometimes false at point 2 Answered

count < 5 is sometimes true/sometimes false at point 2

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(); } for (int j = 1; j <= 4; j++) { for (int k = 4; k >= j; k++) { System.out.print(j + " "); } System.out.println(); } for (int j = 1; j <= 4; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); } for (int j = 1; j <= 4; j++) { for (int k = 4; k >= j; k--) { System.out.print(k + " "); } System.out.println(); } Answered

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

What is the general formula for traversing a String character by character? for(int i = 0; i < string.length(); i++) { String character = string.substring(i, i +1); } for(int i = 0; i <= string.length(); i++) { String character = string.substring(i, i +1); } for(int i = 0; i < string.substring(i, i+1); i++) { String character = string.length(); } for(int i = string.length(); i > 0; i++) { String character = string.substring(i, i +1); }

for(int i = 0; i < string.length(); i++) { String character = string.substring(i, i +1); }


Set pelajaran terkait

Revature Quiz 1, and HTML/CSS questions

View Set

LS CH3 - Cost-Volume-Profit Relationships

View Set

Chapter 55: Drugs Acting on the Lower Respiratory Tract

View Set

07 Law of Agency 2 Chapter 4: Dual Agency

View Set

1.REF/disintermedian/hypothecation/primary market/ expansion/silent generation/taxplay relief act/homeowner affordability/dodd-frank/ HARP/US MINT/BOG/FOMC/Prime rate/FDIC/Regulation Z

View Set