Unit 4: Iteration

Ace your homework & exams now with Quizwiz!

Consider the following code segment. int k = 35; while (k >= 0) { System.out.println("X"); k -= 5; } How many times will the string "X" be printed as a result of executing the code segment?

8

Consider the following recursive method. public static void stars(int num) { if (num == 1) { return; } stars(num - 1); for (int i = 0; i < num; i++) { System.out.print("*"); } System.out.println(); } What is printed as a result of the method call stars(5) ?

** *** **** *****

Consider the following code segment. String str = "AP-CSA"; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 1).equals("A")) { System.out.print(i + " "); } } What is printed as a result of executing the code segment?

0 5

Consider the following code segment. int j = 1; while (j <= 5) { for (int k = 4; k > 1; k--) { System.out.println("ha"); // line 6 } j++; } How many times will the print statement on line 6 execute?

15

Consider the following code segment. String temp = "Mississippi"; String part = "si"; int position = 0; int count = 0; while(temp.indexOf(part) >= 0) { position = temp.indexOf(part); result++; temp = temp.substring(position + 1); } System.out.println(count); What, if anything, is printed as a result of executing the code segment?

2

Consider the following code segments, which differ only in their loop header. Code Segment I for (int i = 0; i < 10; i++) { System.out.print( "*" ); } Code Segment II for (int i = 1; i <= 10; i++) { System.out.print( "*" ); } Which of the following best explains how the difference in the two loop headers affects the output?

The output of the code segments is the same because the loops in both code segments iterate 10 times.

Consider the following code segment. for (int k = 0; k < 4; k++) { /* missing loop header */ { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0 11 222 3333 Which of the following can be used to replace /* missing loop header */ so that the code segment will work as intended?

for (int h = k; h >= 0; h--)

Consider the following code segment. int total = 0; for (int k = 0; k <= 100; k += 2) { total += k; } Which of the following for loops could be used to replace the for loop in the original code segment so that the original and the revised code segments store the same value in total?

for (int k = 1; k <= 101; k += 2) { total += k - 1; }

Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19

1,2,3

Consider the following code segment. int a = 1; String result = ""; while (a < 20) { result += a; a += 5; } System.out.println(result); What, if anything, is printed as a result of executing the code segment?

161116

Consider the following code segment where num is a properly declared integer initialized to a positive value. int total = 0; while (num > 0) { total += num % 10; num /= 10; } Which of the following code segments could replace the while loop without changing the resulting value of total? I. for (int h = 0; h < num; h++) { total += num % 10; num /= 10; } II. for (int j = num; j > 0; j--) { total += j % 10; } III. for (int k = num; k > 0; k /= 10) { total += k % 10; }

3 only

Consider the following code segment. int count = 0; int number = 20; while (number > 0) { number = number / 2; count++; } What will be the value of count after executing the code segment?

5

Consider the following code segment. int a = 100; while (a > 1) { System.out.println("$"); a /= 2; } How many $'s are printed as a result of executing the code segment?

6

Consider the following code segment. for (int i = 0; i < 5; i++) // Line 1 { for (int j = 0; j < 5; j++) { int k = i + j; System.out.print(k + " "); } } Which of the following best describes the result of changing i < 5 to i > 5 in line 1?

Nothing will be printed because the body of the outer for loop will not execute at all.

Consider the following code segment. for (int j = 0; j < 4; j++) { for (int k = 0; k < j; k++) { System.out.println("hello"); } } Which of the following best explains how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment?

The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.

Consider the following code segment. int n = 6; for (int i = 1; i < n; i = i + 2) // Line 2 { System.out.print(i + " "); } Which of the following best explains how changing i < n to i <= n in line 2 will change the result?

There will be no change to the program output because the loop will iterate the same number of times.

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment?

abbccddeef

Consider the following code segment. int a = 1; while (a <= 2) { int c = 1; while (/* missing condition */) { System.out.print("*"); c++; } a++; } The code segment is intended to print "******". Which of the following can be used to replace /* missing condition */ so that the code segment works as intended?

c <= 3

Consider the following code segment. String word = "Computer"; int num = 3; String result = ""; for (int k = num; k >= 0; k--) { result += word.substring(0, k); } System.out.print(result); What is printed as a result of executing the code segment?

comcoc

Consider the following method, which is intended to return the number of local maximum values in an array. Local maximum values are array elements that are greater than both adjacent array elements. The first and last elements of an array have only a single adjacent element, so neither the first nor the last array element is counted by this method. For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values: 9 and 12. public static int countPeaks(int[] data) { int numPeaks = 0; for ( /* missing loop header */ ) { if (data[p - 1] < data[p] && data[p] > data[p + 1]) { numPeaks++; } } return numPeaks; } Which of the following can replace /* missing loop header */ so the method countPeaks works as intended?

int p = 1; p < data.length - 1; p++

Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the code segment will work as intended?

while (num > 0)

Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total. int x = 100; int total = 0; while( /* missing code */ ) { total = total + x; x = x - 10; } Which of the following can be used as a replacement for /* missing code */ so that the code segment works as intended?

x >= 10


Related study sets

UNIT 3: USE ADVANCED FEATURES TO OPTIMIZE WORKFLOW

View Set

CCNA 2 Routing and Switching(v6.0) Chapter 5 Exam Answers

View Set

Powers note taking worksheet chapter 18

View Set

algebra 2a - unit 5: rational equations lesson 19-24

View Set