Ch. 10

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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: public void mystery(int n) { int i; if (n <= 0) return; for (i = 0; i < n; i++) { System.out.print("-"); } for (i = 0; i < n; i++) { System.out.print("+"); } System.out.println(); mystery(n - 1); // Recursive call } What is the output when mystery(4) is called?

----++++ ---+++ --++ -+

Consider the following recursive method: public static int mystery(int n) { if (n >= 5) return mystery(n - 2) - n / 2; else if (n <= 2) return (int) Math.pow(n, 2) - 1; else return n + mystery(n - 1); } What is the output when mystery(10)) is called?

-2

Consider the following method: public int mystery(int t) { if (t > 0) return 1; else return mystery(t + 2) - 2; } What is the output when mystery(-6) is called?

-7

Consider the following method: public void mystery(int a, int b) { System.out.print(a + " "); if (a <= b) mystery(a + 5, b - 1); } What is the output when mystery(0, 16)is called?

0 5 10 15

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

16

Consider the following recursive method. public int recur(int n) { if (n <= 10) return n * 2; else return recur(recur(n / 3)); } What value is returned as a result of the call recur(27)? A 8 B 9 C 12 D 16 E 18

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

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

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

2101

Consider the following recursive method. public static int mystery(int n) { if (n == 0) return 1; else return 3 * mystery(n - 1); } What value is returned as a result of the call mystery(5) ?

243

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

25

Consider the following method. // precondition: arr contains no duplicates; // the elements in arr are in sorted order; // 0 ≤ low ≤ arr.length; low - 1 ≤ high < arr.length public static int mystery(int[] arr, int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(arr, mid + 1, high, num); } else if (arr[mid] > num) { return mystery(arr, low, mid - 1, num); } else // arr{mid] == num { return mid; } } How many calls to mystery (including the initial call) are made as a result of the call mystery(arr, 0, arr.length - 1, 14) if arr is the following array? A 1 B 2 C 4 D 7 E 8

4

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

41

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 recursive method: public int mystery(int k) { if (k == 1) return 0; else return(1 + mystery(k / 2)); } Which of the following best characterizes the values of k for which the call mystery(k) leads to an infinate recursion?

All nonpositive values

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

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information Consider the following instance variable and methods. You may assume that data has been initialized with length > 0. The methods are intended to return the index of an array element equal to target, or -1 if no such element exists. For which of the following test cases will the call seqSearchRec(5) always result in an error? data contains only one element. data does not contain the value 5. data contains the value 5 multiple times. A I only B II only C III only D I and II only E I, II, and III

II only

The following question refer to the following information. 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 corrects the method maxHelper so that it works as intended? A Insert the following statement before Line 1. if (numVals == 0) return numVals; B Insert the following statement before Line 1. if (numVals == 1 return nums[0]; C Insert the following statement between Line 1 and Line 2. if (numVals == 0) return numVals; D Insert the following statement between Line 1 and Line 2. if (numVals == 1) return nums[0]; E Insert the fol

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

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

No numbers will be printed because of infinite recursion.

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. 012300123145672891011312131415 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.Consider the following method. /* Precondition: 0 < numVals <= nums.length / public static int mystery(int[] nums, int v, int numVals) { int k = 0; if (v == nums[numVals - 1]) { k = 1; } if (numVals == 1) { return k; } else { return k + mystery(nums, v, numVals - 1); } } 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. /* Precondition: num ≥ 0 / public static int what(int num) { if (num < 10) { return 1; } else { return 1 + what(num / 10); } } 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. 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 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 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. public static void whatsItDo(String str) { int len = str.length(); if (len > 1) { String temp = str.substring(0, len - 1); whatsItDo(temp); System.out.println(temp); } } 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. 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")

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

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

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

ipce

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 incomplete method powerOfgiven below. The call powerOf(n, x)should return the quantity nx. int powerOf(int base, int power) // precondition: power >= 1 // postcondition: returns basepower { int result; if (<expression1> == 1) { result = <expression2>; } else { result = <expression3> * powerOf(base, power - 1); } return result; } Which of the following could be used to replace <expression1>, <expression2>, and <expression3> so that powerOf will work as intended?

power base base


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

81 Small talk - Chatting about countries

View Set

Statistics//Chapter 4.4, 4.5, 4.6

View Set

THTRE 110: Second set of TopHat and Chapter Quiz questions (Not reading assessments) Final Exam

View Set

Section 5.2: "Inner Product Spaces"

View Set

Summation of Arithmetic Series (2)

View Set

Social Psychology Exam 2: Reactance Theory

View Set

ATI: Fluid, Electrolyte, and Acid/Base Regulation

View Set

Physics I: Final Exam Study Guide

View Set

T6: Compression Techniques (Ch 11,12,13,14)

View Set