AP CS A Q10A

Ace your homework & exams now with Quizwiz!

Consider the following method. What will be printed as a result of the call showMe(0) ? A 10 B 11 C 0 1 2 3 4 5 6 7 8 9 D 9 8 7 6 5 4 3 2 1 0 E 0 1 2 3 4 5 6 7 8 9 10

10

// precondition: x >= 0 public void mystery(int x) { if ((x / 10) != 0) { mystery(x / 10); } System.out.print(x % 10); } Which of the following is printed as a result of the call mystery(123456) ? A 16 B 56 C 123456 D 654321 E Many digits are printed due to infinite recursion.

123456

Consider the following recursive method. What value is returned as a result of the call recur(27)? A 8 B 9 C 12 D 16 E 18

16

The printRightToLeft method is intended to print the elements in the ArrayList words in reverse order. For example, if words contains ["jelly bean", "jukebox", "jewelry"], the method should produce the following output. jewelry jukebox jelly bean The method is shown below. public static void printRightToLeft(ArrayList<String> words) { if (words.size() > 0) { System.out.println(words.get(words.size() - 1)); /* missing code */ } } Which of the following can be used to replace /* missing code */ so that the printRightToLeft method works as intended? A words.remove(0); printRightToLeft(words); B words.remove(words.size()); printRightToLeft(words); C words.remove(words.size() - 1); printRightToLeft(words); D printRightToLeft(words); words.remove(0); E printRightToLeft(words); words.remove(words.size() - 1); words.remove(words.size() - 1);printRightToLeft(words);

words.remove(words.size() - 1); printRightToLeft(words);

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) ? A ***** B ** *** **** ***** C * ** *** **** ***** D ***** **** *** ** E ***** **** *** ** *

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

Consider the following method. Which of the following is printed as a result of the call mystery (1234)? A 1234 B 4321 C 12344321 D 43211234 E Many digits are printed due to infinite recursion.

43211234

Consider the following recursive method. private int recur(int n) { if (n <= 1) { return 1; } else { return (recur(n - 2) + recur(n - 1)); } } What value will be returned by the call recur(4)? A 1 B 2 C 3 D 5 E 8

5

Consider the following method. public static void mystery(int[] a, int index) { if (index < a.length) { mystery(a, index + 1); } if (index > 0) { System.out.print(a[index - 1]); } } What is printed as a result of executing the following code segment? int[] array = {6, 8, 7, 9, 5}; mystery(array, 0); A 5978 B 8795 C 59786 D 68795 E Many digits are printed because of infinite recursion.

59786

Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { L1: int max = maxHelper(numVals - 1); L2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following corrects the method maxHelper so that it works as intended? A Insert the following statement before L1. if (numVals == 0) return numVals; B Insert the following statement before L1. if (numVals == 1) return nums[0]; C Insert the following statement between L1 and L2. if (numVals == 0) return numVals; D Insert the following statement between L1 and L2. if (numVals == 1) return nums[0]; E Insert the following statement between L1 and L2. if (numVals < 2) return numVals;

B Insert the following statement before L1. if (numVals == 1) return nums[0];

The bark method below is intended to print the string "woof" a total of num times. public static void bark(int num) { if (num > 0) { System.out.println("woof"); /* missing code */ } } Which of the following can be used to replace /* missing code */ so that the call bark(5) will cause "woof" to be printed five times? A bark(num - 5); B bark(num - 1); C bark(num); D bark(num + 1); E bark(num + 5);

B bark(num - 1);

Consider the following method, which is intended to return the largest value in the portion of the int array data that begins at the index start and goes to the end of the array. /** Precondition: 0 <= start < data.length */ public int maximum(int[] data, int start) { if (start == data.length - 1) { return data[start]; } /* missing statement */ if (val > data[start]) { return val; } else { return data[start]; } } Which of the following can be used as a replacement for /* missing statement */ so that the maximum method works as intended? A int val = maximum(data, start); B int val = maximum(data, start - 1); C int val = maximum(data, start + 1); D int val = maximum(data[start - 1], start); E int val = maximum(data[start + 1], start);

C int val = maximum(data, start + 1);

Consider the following method. public int addFun(int n) { if (n <= 0) return 0; if (n == 1) return 2; return addFun(n - 1) + addFun(n - 2); } What value is returned as a result of the call addFun(6) ? A 10 B 12 C 16 D 26 E 32

C 16

Consider the following method. // Precondition: b > 0 public int surprise(int b) { if ((b % 2) == 0) { if (b < 10) return b; else return ((b % 10) + surprise(b / 10)); } else { if (b < 10) return 0; else return surprise(b / 10); } } Which of the following expressions will evaluate to true ? surprise(146781) == 0 surprise(7754) == 4 surprise(58216) == 16 A I only B II only C III only D II and III only E I, II, and III

D II and III only

Consider the following method. public String goAgain(String str, int index) { if (index >= str.length()) return str; return str + goAgain(str.substring(index), index + 1); } What is printed as a result of executing the following statement? System.out.println(goAgain("today", 1)); A today B todayto C todayoday D todayodayay E todayodaydayayy

D todayodayay

Consider the following recursive method. public static String recur(int val) { String dig = "" + (val % 3); if (val / 3 > 0) return dig + recur(val / 3); return dig; } What is printed as a result of executing the following statement? System.out.println(recur(32)); A 20 B 102 C 210 D 1020 E 2101

E 2101

Consider the following static method. private static void recur(int n) { if (n != 0) { recur(n - 2); System.out.print(n + " "); } } What numbers will be printed as a result of the call recur(7) ? A -1 1 3 5 7 B 1 3 5 7 C 7 5 3 1 D Many numbers will be printed because of infinite recursion. E No numbers will be printed because of infinite recursion.

E No numbers will be printed because of infinite recursion.

Consider the following method. public String recScramble(String str, int[] positions, int k) { if (str == null || str.length() == 0) return ""; if (str.length() == 1) return str; int pos = positions[k]; String nStr = str.substring(pos, pos + 1); str = str.substring(0, pos) + str.substring(pos + 1); return nStr + recScramble(str, positions, k + 1); } Consider the following code segment. int[] indexes = {2, 1, 1}; System.out.println(recScramble("epic", indexes, 0)); What is printed as a result of executing the code segment? A cepi B epci C iecp D iepc E ipce

E ipce

Consider the following method. public static int calcMethod(int num) { if (num == 0) { return 10; } return num + calcMethod(num / 2); } What value is returned by the method call calcMethod(16) ? A 10 B 26 C 31 D 38 E 41

E 41

Consider the following method. public static int mystery(int n) { if (n <= 0) { return 0; } else { return n + mystery(n - 2); } } What value is returned as a result of the call mystery(9) ? A 0 B 9 C 16 D 24 E 25

E 25

Consider the following two static methods, where f2 is intended to be the iterative version of f1. public static int f1(int n) { if (n < 0) { return 0; } else { return (f1(n - 1) + n * 10); } } public static int f2(int n) { int answer = 0; while (n > 0) { answer = answer + n * 10; n--; } return answer; } The method f2 will always produce the same results as f1 under which of the following conditions? I. n < 0 II. n = 0 III. n > 0 A I only B II only C III only D II and III only E I, II, and III

I, II, and III

Consider the following method. public static void strChange(String str) { if (str.length() > 0) { strChange(str.substring(1)); System.out.print(str.substring(0, 1)); } } Which of the following best describes the behavior of the method? A It prints the first character of str. B It prints the last character of str. C It prints the characters of str in the order they appear. D It prints the characters of str in reverse order. E It prints nothing due to infinite recursion.

It prints the characters of str in reverse order.

Consider the following method. /* Precondition: j <= k */ public static void mystery(int j, int k) { System.out.println(j); if (j < k) { mystery(j + 1, k); } } Which of the following best describes the behavior of the mystery method? A It repeatedly prints the value j due to infinite recursion. B It prints the initial value of j a total of k times. C It prints the initial value of k a total of j times. D It prints the integers from j to k, inclusive, in order from least to greatest. E It prints the integers from j to k, inclusive, in order from greatest to least.

It prints the integers from j to k, inclusive, in order from least to greatest.

Consider the following method. public static int mystery(ArrayList<Integer> numList) { if (numList.size() == 0) { return 0; } else { int val = numList.remove(0); return val + mystery(numList); } } Which of the following best describes the value returned by the method? A It returns the value of the first element in numList. B It returns the value of the last element in numList. C It returns the sum of the elements in numList. D It returns 0 regardless of the contents of numList. E It returns nothing due to infinite recursion.

It returns the sum of the elements in numList.

Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following best describes the conditions under which maxHelper does not work as intended? A When numVals is 1 B When numVals is even C When the elements of nums are in nonincreasing order D When the elements of nums are in nondecreasing order E Method maxHelper never works as intended.

Method maxHelper never works as intended.

Consider the following data field and method. private int[][] mat; public int mystery(int r, int c) { if (r != 0 || c != 0) { return (mat[r][c] + mystery(r - 1, c - 1)); } else { return mat[r][c]; } } Assume that mat is the 2-D array shown below. 0 1 2 3 0 | 0 | 1 | 2 | 3 1 | 4 | 5 | 6 | 7 2 | 8 | 9 | 10 | 11 3 | 12 | 13 | 14 | 15 What value is returned as a result of the call mystery(2, 3)? A 1 B 11 C 17 D 18 E No value is returned because mystery throws an ArrayIndexOutOfBoundsException.

No value is returned because mystery throws an ArrayIndexOutOfBoundsException.

Consider the following method. Which of the following best describes what the call mystery(numbers, val, numbers.length)does? You may assume that variables numbers and val have been declared and initialized. A Returns 1 if the last element in numbers is equal to val; otherwise, returns 0 B Returns the index of the last element in numbers that is equal to val C Returns the number of elements in numbers that are equal to val D Returns the number of elements in numbers that are not equal to val E Returns the maximum number of adjacent elements that are not equal to val

Returns the number of elements in numbers that are equal to val.

Consider the following recursive method. Assume that int val has been declared and initialized with a value that satisfies the precondition of the method. Which of the following best describes the value returned by the call what(val) ? A The number of digits in the decimal representation of val is returned. B The sum of the digits in the decimal representation of val is returned. C Nothing is returned. A run-time error occurs because of infinite recursion. D The value 1 is returned. E The value val/10 is returned.

The number of digits in the decimal representation of val is returned.

Consider the following instance variable and method. What is returned by the call mystery (0, arr.length − 1, num) ? A The number of elements in arr that are less than num B The number of elements in arr that are less than or equal to num C The number of elements in arr that are equal to num D The number of elements in arr that are greater than num E The index of the middle element in arr

The number of elements in arr that are less than num

Consider the following recursive method. /** Precondition: 0 <= numVals <= nums.length */ public static int mystery(int[] nums, int v, int numVals) { if (numVals == 0) { return 0; } else if (v == nums[numVals - 1]) { return 1 + mystery(nums, v, numVals - 1); } else { return mystery(nums, v, numVals - 1); } } Which of the following best describes the value returned by the call mystery(nums, v, nums.length) ? A The value 0 is returned. B The value 1 is returned. C The number of times that v occurs in nums is returned. D The number of times that numVals occurs in nums is returned. E Nothing is returned. A runtime error occurs because of infinite recursion.

The number of times that v occurs in nums is returned.

Consider the following recursive method. What is printed as a result of the call whatsItDo("WATCH") ? A H B WATC C ATCH ATC AT A D WATC WAT WA W E WATCH WATC WAT WA

WATC WAT WA W

Consider the following two methods, which are intended to return the same values when they are called with the same positive integer parameter n. public static int mystery1(int n) { if (n > 1) { return 5 + mystery1(n - 1); } else { return 1; } } public static int mystery2(int n) { int total = 0; int x = 1; while (x < n) { total += 5; x++; } return total; } Which, if any, of the following changes to mystery2 is required so that the two methods work as intended? A The variable total should be initialized to 1. B The variable x should be initialized to 0. C The condition in the while loop header should be x < n - 1. D The condition in the while loop header should be x <= n. E No change is required; the methods, as currently written, return the same values when they are called with the same positive integer parameter n.

The variable total should be initialized to 1.

Consider the following recursive method. What is printed as a result of the call whatsItDo ("WATCH") ? A WATC WAT WA W B WATCH WATC WAT WA C W WA WAT WATC D W WA WAT WATC WATCH E WATCH WATC WAT WA W WA WAT WATC WATCH

W WA WAT WATC

Consider the following recursive method. Assuming that k is a nonnegative integer and m = 2k, what value is returned as a result of the call mystery (m) ? A 0 B k C m D E

k

Consider the following recursive method. public static boolean recurMethod(String str) { if (str.length() <= 1) { return true; } else if (str.substring(0, 1).compareTo(str.substring(1, 2)) > 0) { return recurMethod(str.substring(1)); } else { return false; } } Which of the following method calls will return true ? A recurMethod("abcba") B recurMethod("abcde") C recurMethod("bcdab") D recurMethod("edcba") E recurMethod("edcde")

recurMethod("edcba")


Related study sets

Health Assessment Chapter 26: Newborns and Infants

View Set

Recommended Maternal and Newborn Success Questions Chapter 6: Newborn

View Set

Four Stages in the Healing of a Bone Fracture

View Set

West Virginia Laws, Rules, and Regulations (ALL lines and PROPERTY insurance) (CH.5)

View Set

Ch. 5-5 The Small Business Administration

View Set

Chapter 16: Pennsylvania Life Laws

View Set

Moseley Real Estate Pre License State Exam Review

View Set

Chapter 16, Intro to Bus. Chapter 13, Marketing 4, Chapter 16, Chapter 16, Marketing Chapter 17, HRIM 442 Ch 17 Exam 3, Marketing Ch 17, Marketing Ch 17-19, Marketing Chapter 17 & 18, Marketing Chapter 17, mkt ch 16, Marketing 4, MKT 301 - Ch. 16, Ma...

View Set