APCSA AP Classroom Chapter 4 Questions

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

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") ?

"53"

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. for (int i = 0; i < m * n; i++){ System.out.print("A");} for (int j = 1; j <= m; j++){ for (int k = 1; k < n; k++) { System.out.print("B"); }} Assume that the initial values of m and n are the same in code segment I as they are in code segment II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed?

"A" is printed m more times than "B".

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

Consider the following method. public static String rearrange(String str) { String temp = ""; for (int i = str.length() - 1; i > 0; i--) { temp += str.substring(i - 1, i); } return temp; } What, if anything, is returned by the method call rearrange("apple") ?

"lppa"

Consider the following method. public String mystery (String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; } What is returned as a result of the call mystery("computer") ?

"optr"

Consider the following method. public static String abMethod(String a, String b) { int x = a.indexOf(b); while (x >= 0) { a = a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return a; } What, if anything, is returned by the method call abMethod("sing the song", "ng") ?

"si the so"

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?

10

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?

18

Consider the following code segment. for (int outer = 1; outer <= 6; outer++) { for (int inner = outer; inner <= 6; inner++) { if (inner % 2 == 0) { System.out.print(inner + " "); } } System.out.println(); } What will be printed as a result of executing the code segment?

2 4 6 2 4 6 4 6 4 6 6 6

Consider the following code segment. int num = 2574; int result = 0; while (num > 0) { result = result * 10 + num % 10; num /= 10; } System.out.println(result); What is printed as a result of executing the code segment?

4752

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?

5

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?

60

/** Precondition: a > 0 and b > 0 */ public static int methodOne(int a, int b) { int loopCount = 0; for (int i = 0; i < a / b; i++) { loopCount++; } return loopCount; } /** Precondition: a > 0 and b > 0 */ public static int methodTwo(int a, int b) { int loopCount = 0; int i = 0; while (i < a) { loopCount++; i += b; } return loopCount; } Which of the following best describes the conditions under which methodOne and methodTwo return the same value?

When a % b is equal to zero

Consider the following incomplete method, which is intended to return the number of integers that evenly divide the integer inputVal. Assume that inputVal is greater than 0. public static int numDivisors (int inputVal) { int count = 0; for (int k = 1; k <= inputBal; k++) { if ( /* condition */ ) { count++; } } return count; } Which of the following can be used to replace / * condition * / so that numDivisors will work as intended?

inputVal % k == 0

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 = outer; inner < 3; inner++

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) { System.out.print(j); j += 2; }

Which of the following code segments produces the output "987654321" ?

int num = 10; while (num > 1) { num--; System.out.print(num); }

Consider the following method, which is intended to return the number of local maximum values in an array. Local maximum values are array elements that are greater than both adjacent array elements. The first and last elements of an array have only a single adjacent element, so neither the first nor the last array element is counted by this method. For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values: 9 and 12. public static int countPeaks(int[] data) { int numPeaks = 0; for ( /* missing loop header */ ) { if (data[p - 1] < data[p] && data[p] > data[p + 1]) { numPeaks++; } } return numPeaks; } Which of the following can replace /* missing loop header */ so the method countPeaks works as intended?

int p = 1; p < data.length - 1; p++

Consider the following method. public static boolean mystery (String str) { String temp = ""; for (int k = str.length(); k > 0; k--) { temp = temp + str.substring(k -i, k); } return temp.equals(str); } Which of the following calls to mystery will return true?

mystery ("noon")

Directions: Select the choice that best fits each statement. The following question(s) refer to the following method public static int mystery (int n) { int x = 1; int y = 1; // Point A while (n > 2) { x = x + y; // Point B y = x - y; n--; } //Point C return x; } Which of the following is true of method mystery ?

n will always be greater than 2 at // Point B.

Consider the following method. public int compute (int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) answer *= n; return answer; } Which of the following represents the value returned as a result of the call compute (n, k) ?

n^k

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?

num != 0

Consider the following method. public static void printSome(int num1, int num2) { for (int i = 0; i < num1; i++) { if (i % num2 == 0 && i % 2 == 0) { System.out.print(i + " "); } } } Which of the following method calls will cause "0 10 " to be printed?

printSome(20, 5)

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 < 4)

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 <= 101)

Directions: Select the choice that best fits each statement. The following question(s) refer to the following method public static int mystery (int n) { int x = 1; int y = 1; // Point A while (n > 2) { x = x + y; // Point B y = x - y; n--; } //Point C return x; } What value is returned as a result of the call mystery (6)?

8

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?

AA

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 < x; y++)

Consider the following code segment. for(int k = 1; k <= 100; k++) if ((k % 4) == 0) System.out.println(k); Which of the following code segments will produce the same output as the code segment above?

for(int k = 4; k <= 100; k = k + 4) System.out.println(k);

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 = 9; k >= 0; k--) { count++; } System.out.println(count);

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?

161

Consider the following recursive method. public static void stars(int num) { if (num == 1) { return; } stars(num - 1); for (int i = 0; i < num; i++) { System.out.print("*"); } System.out.println(); } What is printed as a result of the method call stars(5) ?

** *** **** *****

Consider the following code segment. for (int r = 3; r < 0; r--) { int c; for( c = 1; c < r; c++) { System.out.print("-"); } for (c = r; c <= 3; c++) { System.out.print("*"); } System.out.println(); } What is printed as a result of executing the code segment?

--* -** ***

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

Consider the following code segment. int count = 0; for (int x = 0; x < 4; x++) { for(int y = x; y < 4; y++) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?

10

Consider the following method. public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length - 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--: } } Which of the following describes what the method arrayMethod() does to the array nums?

The contents of the array nums are reversed.

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 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 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? I) for (int j = 0; j < 3; j++) II) for (int j = 1; j < 3; j++) III) for (int j = 1; j <= 3; j++)

I and III

Consider the following code segment. int x = 1; while ( /* missing code */ ) { System.out.print(x + " "); x = x + 2; } Consider the following possible replacements for /* missing code */. I) x < 6 II) x != 6 III) x < 7 Which of the proposed replacements for /* missing code */ will cause the code segment to print only the values 1 3 5?

I and III only

Consider the following code segment. int x = 1; while (/* condition */ ) { if (x % 2 == 0) { System.out.println(x + " "); } x = x + 2; } The following conditions have been proposed to replace /* condition */ in the code segment. I) x < 0 II) x <= 1 III) x < 10 For which of the conditions will nothing be printed?

I, II, and III

Consider the following code segment. int sum = 0; int k = 1; while (sum < 12 || k < 4) sum += k; System.out.println(sum); What is printed as a result of executing the code segment?

Nothing is printed due to an infinite loop.

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?

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?

eeSepC

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 = 1; k <= 8; k += 2) { System.out.print(k); }

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, 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 string "Fun" will be printed more times because the outer loop will execute more times.

Consider the following code segment. Assume that num3 > num2 > 0. int num1 = 0; int num2 = /* initial value not shown */; int num3 = /* initial value not shown */; while (num2 < num3) { num1 += num2; num2++; } Which of the following best describes the contents of num1 as a result of executing the code segment?

The sum of all integers from num2 to num3 - 1, inclusive

Consider the following method. public static int doWhat(int num) { int var = 0; for (int loop = 1; loop <= num; loop = loop + 2) { var += loop; return var; } Which of the following best describes the value returned from a call to doWhat ?

The sum of all odd integers between 1 and num, inclusive

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 four more values printed because the outer loop will iterate one additional time.


Ensembles d'études connexes

Certified Information System Auditor

View Set

Chapter 20: The Cardiovascular System-The Heart

View Set

Prep U Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set

Religion -Test 2 Multiple Choice

View Set

Abnormal Psychology Chapter 6 Questions

View Set