Unit 4 Test
Consider the following method. public static String changeStr(String str) { String result = ""; for (int i = str.length() - 1; i >= str.length() / 2; i -= 2) { result += str.substring(i, i + 1); } return result; } What value is returned as a result of the method call changeStr("12345") ? "4" A "53" B "531" C "543" D "54321"
"53"
Consider the following code segment. What is printed as a result of executing the code segment? 4 16 A 4 10 16 B 0 6 12 18 C 1 4 7 10 13 16 19 D 0 2 4 6 8 10 12 14 16 18
4 10 16
Consider the following code segment. int outerMax = 10; int innerMax = 5; for (int outer = 0; outer < outerMax; outer++) { for (int inner = 0; inner <= innerMax; inner++) { System.out.println(outer + inner); } } How many values will be printed when the code segment is executed? 45 A 50 B 55 C 60 D 66
60
Consider the following code segment. int num = 1; int count = 0; while (num <= 10) { if (num % 2 == 0 && num % 3 == 0) { count++; } num++; } What value is stored in the variable count as a result of executing the code segment? 1 A 3 B 5 C 7 D 8
1
Consider the following code segment. int counter = 0; for (int x = 10; x > 0; x--) { for (int y = x; y <= x; y++) { counter++; // line 6 } } How many times will the statement in line 6 be executed as a result of running the code segment? 0 A 1 B 10 C 11 D 20
10
Consider the following code segment. for (int k = 1; k <= 7; k += 2) { System.out.print(k); } Which of the following code segments will produce the same output as the code segment above? for (int k = 0; k < 7; k += 2) { System.out.print(k); } A for (int k = 0; k <= 7; k += 2) { System.out.print(k); } B for (int k = 0; k <= 8; k += 2) { System.out.print(k + 1); } C for (int k = 1; k < 7; k += 2) { System.out.print(k + 1); } D for (int k = 1; k <= 8; k += 2) { System.out.print(k); }
for (int k = 1; k <= 8; k += 2) { System.out.print(k); }
Consider the following code segment, which is intended to print the sum of all the odd integers from 0 up to and including 101. int r = 0; int sum = 0; /* missing loop header */ { if (r % 2 == 1) { sum += r; } r++; } System.out.println(sum); Which of the following could replace /* missing loop header */ to ensure that the code segment will work as intended? while (r <= 100) A while (sum <= 100) B while (r < 101) C while (r <= 101) D while (sum <= 101)
while (r <= 101)
Consider the following code segment. for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); } Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y < 4 to y <= 4 in line 3 ? "a" will be printed fewer times because while each output line will have the same length as before, the number of lines printed will decrease by 1. A "a" will be printed more times because while the number of output lines will be the same as before, the length of each output line will increase by 1. B "a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1. C "a" will be printed more times because both the number of output lines and the length of each line will increase by 1. D The output of the code segment will not change in any way.
"a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1. C
the
D
Consider the following code segment. int k = 0; /* missing loop header */ { k++; System.out.print(k + " "); } Which of the following can be used as a replacement for /* missing loop header */ so that the code segment prints out the string "1 2 3 4 "? while (k < 3) A while (k < 4) B while (k < 5) C while (k <= 4) D while (k <= 5)
while (k < 4)
Consider the following code segment. int j = 1; while (j < 5) { int k = 1; while (k < 5) { System.out.println(k); k++; } j++; } Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ? There will be one more value printed because the outer loop will iterate one additional time. A There will be four more values printed because the outer loop will iterate one additional time. B There will be one less value printed because the outer loop will iterate one fewer time. C There will be four fewer values printed because the outer loop will iterate one fewer time. D There will be no change to the output of the code segment.
B There will be four more values printed because the outer loop will iterate one additional time.
Consider the following method definition. The method printAllCharacters is intended to print out every character in str, starting with the character at index 0. public static void printAllCharacters(String str) { for (int x = 0; x < str.length(); x++) // Line 3 { System.out.print(str.substring(x, x + 1)); } } The following statement is found in the same class as the printAllCharacters method. printAllCharacters("ABCDEFG"); Which choice best describes the difference, if any, in the behavior of this statement that will result from changing x < str.length() to x <= str.length() in line 3 of the method? The method call will print fewer characters than it did before the change because the loop will iterate fewer times. A The method call will print more characters than it did before the change because the loop will iterate more times. B The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6. C The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 8 in a string whose last element is at index 7. D The behavior of the code segment will remain unchanged.
The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6.
Consider the following code segment. for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { System.out.println("Fun"); } } Which of the following best explains how changing the outer for loop header to for (int j = 0; j <= 3; j++) affects the output of the code segment? The output of the code segment will be unchanged. A The string "Fun" will be printed more times because the outer loop will execute more times. B The string "Fun" will be printed more times because the inner loop will execute more times in each iteration of the outer loop. C The string "Fun" will be printed fewer times because the outer loop will execute fewer times. D The string "Fun" will be printed fewer times because the inner loop will execute fewer times in each iteration of the outer loop.
The string "Fun" will be printed more times because the outer loop will execute more times.
Consider the following method. public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; } What value is returned as a result of the call mystery(1034) ? 4 A 10 B 34 C 103 D 1034
103 D
Consider the following code segment. for (int outer = 0; outer < 3; outer++) { for (/* missing loop header */) { System.out.print(outer + "" + inner + "_"); } } Which of the following can be used as a replacement for /* missing loop header */ so that the code segment produces the output 00_01_02_11_12_22_ ? int inner = 0; inner < 3; inner++ A int inner = 1; inner < 3; inner++ B int inner = outer - 1; inner < 3; inner++ C int inner = outer; inner < 3; inner++ D int inner = outer + 1; inner < 3; inner++
int inner = outer; inner < 3; inner++
Consider the following method. public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ? 48 A 105 B 384 C 5040 D 40320
48
Consider the following code segment. int num = 1; while (num < 5) { System.out.println("A"); num += 2; } What is printed as a result of executing the code segment? A A AA B AAA C AAAA D AAAAA
AA
Consider the following code segment. /* missing loop header */ { for (int k = 0; k < 4; k++) { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0123 0123 0123 Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? for (int j = 0; j < 3; j++) for (int j = 1; j < 3; j++) for (int j = 1; j <= 3; j++) I only A II only B III only C I and II D I and III
I and III
Consider the following code segment. int count = 0; for (int k = 0; k < 10; k++) { count++; } System.out.println(count); Which of the following code segments will produce the same output as the code segment above? int count = 0; for (int k = 1; k < 10; k++) { count++; } System.out.println(count); A int count = 1; for (int k = 1; k <= 10; k++) { count++; } System.out.println(count); B int count = 1; for (int k = 0; k <= 9; k++) { count++; } System.out.println(count); C int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count); D int count = 0; for (int k = 10; k >= 0; k--) { count++; } System.out.println(count);
int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);
Consider the following code segment. for (int j = 1; j < 10; j += 2) { System.out.print(j); } Which of the following code segments will produce the same output as the code segment above? int j = 1; while (j < 10) { j += 2; System.out.print(j); } A int j = 1; while (j < 10) { System.out.print(j); j += 2; } B int j = 1; while (j <= 10) { j += 2; System.out.print(j); } C int j = 1; while (j >= 10) { j += 2; System.out.print(j); } D int j = 1; while (j >= 10) { System.out.print(j); j += 2; }
int j = 1; while (j < 10) { System.out.print(j); j += 2; }
The following method is intended to print the number of digits in the parameter num. public int numDigits(int num) { int count = 0; while (/* missing condition */) { count++; num = num / 10; } return count; } Which of the following can be used to replace /* missing condition */ so that the method will work as intended? count != 0 A count > 0 B num >= 0 C num != 0 D num == 0
num != 0
Consider the following method. public String wordPlay(String word) { String str = ""; for (int k = 0; k < word.length(); k++) { if (k % 3 == 0) { str = word.substring(k, k + 1) + str; } } return str; } The following code segment appears in another method in the same class as wordPlay. System.out.println(wordPlay("Computer Science")); What is printed as a result of executing the code segment? C A ci tm B eeStm C ncepC D eeSepC
E eeSepC
Consider the following code segment. int count = 5; while (count < 100) { count = count * 2; } count = count + 1; What will be the value of count as a result of executing the code segment? 100 A 101 B 160 C 161 D 321
161
Consider the following code segment. String str = "a black cat sat on a table"; int counter = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("a") && !str.substring(i + 1, i + 2).equals("b")) { counter++; } } System.out.println(counter); What is printed as a result of executing this code segment? 1 A 2 B 3 C 5 D 6
5
Consider the following code segment. int val = 1; while (val <= 6) { for (int k = 0; k <= 2; k++) { System.out.println("Surprise!"); } val++; } How many times is the string "Surprise!" printed as a result of executing the code segment? 3 A 6 B 12 C 15 D 18
E 18
Consider the following two code segments. Code segment II is a revision of code segment I in which the loop header has been changed. I. for (int k = 1; k <= 5; k++) { System.out.print(k); } II. for (int k = 5; k >= 1; k--) { System.out.print(k); } Which of the following best explains how the output changes from code segment I to code segment II? Both code segments produce the same output, because they both iterate four times. A Both code segments produce the same output, because they both iterate five times. B Code segment I prints more values than code segment II does, because it iterates for one additional value of k. C Code segment II prints more values than code segment I, because it iterates for one additional value of k. D The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.
Consider the following code segment. int count = 0; for (int x = 1; x <= 3; x++) { /* missing loop header */ { count++; } } System.out.println(count); Which of the following should be used to replace /* missing loop header */ so that the code segment will print 6 as the value of count ? for (int y = 0; y <= 2; y++) A for (int y = 0; y < 3; y++) B for (int y = 2; y >= 0; y--) C for (int y = 3; y > 0; y--) D for (int y = 0; y < x; y++)
for (int y = 0; y < x; y++)
Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k = 1; k <= 30; k = k + 2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2 ? Code segment 1 and code segment 2 will produce the same output. A Code segment 2 will print the sum of only the even integers from 1 through 30, inclusive because it starts sum at zero, increments k by twos, and terminates when k exceeds 30. B Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30. C Code segment 2 will print the sum of only the even integers from 1 through 60, inclusive because it starts sum at zero, increments k by twos, and iterates 30 times. D Code segment 2 will print the sum of only the odd integers from 1 through 60, inclusive because it starts k at one, increments k by twos, and iterates 30 times.
Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.