CSA Unit 4 Progress Check: MCQ

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

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? (A) 21 (B) 161116 (C) 161161 (D) 16111621 (E) Nothing is printing because of an infinite loop.

(B) 161116

13. 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? (A) C く=2 (B) с < 3 (C)C<=3 (D) c> 2 (E) с >= 3

(C)C<=3

15. Consider the following code segment. for (int i = 0; 1 < 5; it+) // 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? (A) The rumbers will be printee or everse order as they were in the original code segment because the (B) Five additional values will be printed because the outer for loop will iterate one additional time. (C) An infinite loop will occur because the termination condition of the loop will never be reached. (D) There will be no change to the program output. (E) Nothing will be printed because the body of the outer for loop will not execute at all.

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

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

(c) 2

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? A6 B 5 C 4 D 1 E

( b ) 5 times

7. 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; 1 < 10; 1++) { System. out. print ( "*"); { Which of the following best explains how the difference in the two loop headers affects the output? (A) The output of the code segments is the same because the loops in each code segment terminate when is 10. (B) The output of the code segments is the same because the loops in both code segments iterate 10 times. ( c )The output of the code segments is different because code segment I iterates from i = 0 to i = 9 and code segment I iterates from i = 1 to i = 10. (D)The output of the code segments is different because code segment I iterates from i = 0 to i = 10 and code segment Il iterates from i = 1 to i = 11. (E) Neither code segment produces output because both loop conditions are in

(B)

6• 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? 1. for (int h = 0; h < num; h ++ ) { total †= num % 10; num / = 10; } 2. for (int j = num ; J > 0:J ++ ) total += J % 10 } 3. For ( int k = num; k > 0; k /= 10) { total += % 10; }

(C) 3 only

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

(C) 6

17. Consider the following code segment. int k = 35; while (k >= 0) { System.out.printIn ("X") ; k -= 5; } How many times will the string "X" be printed as a result of executing the code segment? (A) 1 (B) 7 (C) 8 (D) 35 (E) More than 35 times, because the code segment will cause an infinite loop.

(C) 8

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? A 15 B 16 C 20 D 24 E 25

A 15

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? A while (num > 0) B while (num >= 0) C while (num > 1) D while (num > 2) E while (num > sum)

Answer A Correct. The expression num % 10 is used to get the value of the rightmost digit of num. The while loop executes five times. At the beginning of the first iteration, num is 12345 and sum is 0. At the beginning of the second iteration, num is 1234 and sum is 5. At the beginning of the third iteration, num is 123 and sum is 9. At the beginning of the fourth iteration, num is 12 and sum is 12. At the beginning of the fifth iteration, num is 1 and sum is 14. At the end of the fifth iteration, num is 0 and sum is 15.

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? A An additional value will be printed because the for loop will iterate one additional time. B One fewer value will be printed because the for loop will iterate one fewer time. C There will be no change to the program output because the loop will iterate the same number of times. D An infinite loop will occur because the loop condition will never be false. E The body of the loop will not execute at all because the loop condition will initially be false.

C There will be no change to the program output because the loop will iterate the same number of times. ** If the loop incremented i by 1 instead of 2, changing i < n to i <= n would cause the loop to iterate one additional time.

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? A 0 B 5 C 0 5 D 0 6 E 1 6

C 0 5

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? A for (int h = 0; h < k; h++) B for (int h = 1; h < k + 1; h++) C for (int h = 0; h < 3; h++) D for (int h = k; h >= 0; h--) E for (int h = k; h <= 0; h--)

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

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? A x < 100 B x <= 100 C x > 10 D x >= 10 E x != 10

D x >= 10 ;; Correct. The variable x has an initial value of 100 and is decremented by 10 in the loop body. The last iteration of the loop occurs when x has the value 10, and 10 is the last value added to total.

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? A ccocom B comcoc C ccocomcomp D compcomcoc E comcomcomcom

B comcoc

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? A The output of the code segment will be unchanged. B The string "hello" will be printed three fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. C The string "hello" will be printed four fewer times because the inner loop will iterate one fewer time for each iteration of the outer loop. D The string "hello" will be printed three additional times because the inner loop will iterate one additional time for each iteration of the outer loop. E 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.

E 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 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? A for (int k = 0; k < 100; k += 2) { total += k + 1; } B for (int k = 1; k < 101; k += 2) { total += k - 1; } C for (int k = 0; k <= 101; k += 2) { total += k + 1; } D for (int k = 1; k <= 101; k += 2) { total += k + 1; } E for (int k = 1; k <= 101; k += 2) { total += k - 1; }

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


Set pelajaran terkait

Chapter 5 Therapeutic Relationship

View Set

Schizo Spectrum Disorders & Psychosis

View Set

Penny chapter 18 The Ovaries and Fallopian Tubes

View Set

briefly explain the importance of Brown v. board of education of Topeka. cite the case and the legal doctrine that the Supreme Court overruled in the brown case.

View Set