AP CS- Unit 4

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

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? Answer A: for (int k = 0; k <= 7; k += 2) { System.out.print(k); } Answer B: for (int k = 0; k <= 8; k += 2) { System.out.print(k + 1); } Answer C: for (int k = 0; k <= 8; k += 2) { System.out.print(k + 1); } Answer D: for (int k = 1; k < 7; k += 2) { System.out.print(k + 1); } Answer E: for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

Answer E: for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

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? Answer A: 100 Answer B: 101 Answer C: 160 Answer D: 161 Answer E : 321

Answer A: 1

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

Answer A: 1

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? Answer A. int j = 1; while (j < 10) { j += 2; System.out.print(j); } Answer B. int j = 1; while (j < 10) { System.out.print(j); j += 2; } Answer C. int j = 1; while (j <= 10) { j += 2; System.out.print(j); } Answer D. int j = 1; while (j >= 10) { j += 2; System.out.print(j); } Answer E. int j = 1; while (j >= 10) { System.out.print(j); j += 2; }

Answer B. int j = 1; while (j < 10) { System.out.print(j); j += 2; }

Consider the following code segment. int num = 1; while (num < 5) { System.out.print("A"); num += 2; } What is printed as a result of executing the code segment? Answer A: A Answer B: AA Answer C: AAA Answer D: AAAA Answer E: AAAAA

Answer B: AA

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 "? Answer A: while (k < 3) Answer B: while (k < 4) Answer C: while (k < 5) Answer D: while (k <= 4) Answer E: while (k <= 5)

Answer B: while (k < 4)

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 ? Answer A: Code segment 1 and code segment 2 will produce the same output. Answer B: 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 . Answer C: 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 . Answer D: 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. Answer E: 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.

Answer C: 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

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? Answer A: The method call will print fewer characters than it did before the change because the loop will iterate fewer times. Answer B: The method call will print more characters than it did before the change because the loop will iterate more times. Answer 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 7 in a string whose last element is at index 6. Answer D: 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 . Answer E: The behavior of the code segment will remain unchanged.

Answer 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 7 in a string whose last element is at index 6.

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? Answer A. int count = 0; for (int k = 1; k < 10; k++) { count++; } System.out.println(count); Answer B. int count = 1; for (int k = 1; k <= 10; k++) { count++; } System.out.println(count); Answer C. int count = 1; for (int k = 0; k <= 9; k++) { count++; } System.out.println(count); Answer D. int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count); Answer E. int count = 0; for (int k = 10; k >= 0; k--) { count++; } System.out.println(count);

Answer D. int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);

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? Answer A: count != 0 Answer B: count > 0 Answer C: num >= 0 Answer D: num != 0 Answer E: num == 0

Answer D: num != 0

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) Answer A: while (r <= 100) Answer B: while (sum <= 100) Answer C: while (r < 101) Answer D: while (r <= 101) Answer E: while (sum <= 101)

Answer D: while (r <= 101)

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? Answer A: Both code segments produce the same output, because they both iterate four times. Answer B: Both code segments produce the same output, because they both iterate five times. Answer C: Code segment I prints more values than code segment II does, because it iterates for one additional value of k . Answer D: Code segment II prints more values than code segment I, because it iterates for one additional value of k . Answer E: 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.

Answer E: 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 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? Answer A: C Answer B: ci tm Answer C: eeStm Answer D: ncepC Answer E: eeSepC

Answer E: eeSepC


Ensembles d'études connexes

Med Surg: Patients With Musculoskeletal Disorders

View Set

AWS Cloud Practitioner Exam Questions

View Set

Plate Tectonic, Volcano, and Earthquake Test Review!

View Set

Chapter 47: Conservation of Biodiversity

View Set

Chapter 7: Criminal Law and Cyber Crime

View Set