Unit 4 CSA

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

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)

A

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

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

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? A The output of the code segments is the same because the loops in each code segment terminate when i 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 II 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 II iterates from i = 1 to i = 11. E Neither code segment produces output because both loop conditions are initially false.

B

Consider the following method. public String mystery(String word, int num) { String result = ""; for (int k = num; k >= 0; k--) { result += word.substring(0, k); } return result; } What is returned as a result of the call mystery("computer", 3) ? A ccocom B comcoc C ccocomcomp D compcomcoc E comcomcomcom

B

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

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

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

C

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 c < 3 C c <= 3 D c > 2 E c >= 3

C

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? A 1 B 7 C 8 D 35 E More than 35 times, because the code segment will cause an infinite loop.

C

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

Consider the following method. public static int mystery(String string1, String string2) { String temp = string1; int position = 0; int result = 0; while(temp.indexOf(string2) >= 0) { position = temp.indexOf(string2); result++; temp = temp.substring(position + 1); } return result; } The following code segment appears in another method in the same class. System.out.println(mystery("Mississippi", "si")); 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

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? A The numbers will be printed in the reverse order as they were in the original code segment because the outer loop will occur in reverse order. 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

Unit 4 Progress Check: MCQ Start Date3/31/2021Due5/15/2021Performance16/18 Q5 calculate total equivalent for loops Skill 4.C 1/1 MC point 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


Ensembles d'études connexes

Principles of Economics-Chapter 1

View Set

MAN 3025- Final Exam: Chapters 10-13

View Set

Varcarolis: Chapter 12 - Schizophrenia and Schizophrenia Spectrum Disorders

View Set

Wireless Networking Part 2 Chp 8

View Set

Biology 1210 Chapter 5 Mastering Biology

View Set