CSA Unit 4 Progress Check: MCQ

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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.

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

5 times

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.

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. 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.

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; }


संबंधित स्टडी सेट्स

The Functions of Proteins in the Human Body

View Set

FIN 3100 Chapter 6 Review Questions Bonds and Bond Valuation. Your Welcome :)

View Set