Computers Final MC Review (unit 4)

अब 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? Answer A: 15 Answer B: 16 Answer C: 20 Answer D: 24 Answer 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? Answer A: while (num > 0) Answer B: while (num >= 0) Answer C: while (num > 1) Answer D: while (num > 2) Answer E: while (num > sum)

A while (num > 0)

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

C 0 5

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? Answer A: c <= 2 Answer B: c < 3 Answer C: c <= 3 Answer D: c > 2 Answer E: c >= 3

C c <= 3

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

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

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

B 161116

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

B 5

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

B comcoc

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

C 6

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

D x >= 10

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? Answer A: The output of the code segments is the same because the loops in each code segment terminate when i is 10 . Answer B: The output of the code segments is the same because the loops in both code segments iterate 10 times. Answer 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 . Answer 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 . Answer E: Neither code segment produces output because both loop conditions are initially false .

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

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? Answer A: 0 Answer B: 1 Answer C: 2 Answer D: 3 Answer E: Nothing is printed.

C 2

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

C 8

Consider the method digitSum below, which takes a positive integer parameter as input. public int digitSum(int num) { int total = 0; while (num > 0) { total += num % 10; num /= 10; } return total; } Which of the following code segments could replace the while loop in the method digitSum without changing the value returned by the method? 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; } Answer A: I only Answer B: II only Answer C: III only Answer D: I and II Answer E: II and III

C III only

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

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

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

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? Answer 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. Answer B: Five additional values will be printed because the outer for loop will iterate one additional time. Answer C: An infinite loop will occur because the termination condition of the loop will never be reached. Answer D: There will be no change to the program output. Answer 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.

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? Answer A: The output of the code segment will be unchanged. Answer 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. Answer 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. Answer 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. Answer 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.


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

NCLEX-RN from FB (random questions)

View Set

values dfo the differencet of the ansers and squestions

View Set