Sei sulla pagina 1di 4
Multiple-Choice Questions on Recursion eee a MULTIPLE-CHOICE QUESTIONS ON RECURSION erence renee ARREARS RRR 1. Which of the following statements about recursion are true? I Every recursive algorithm can be written iteratively. Il Tail recursion is always used in “divide-and-conquer” algorithms. II In a recursive definition, an object is defined in terms of a simpler case of itself. (A) Tonly (B) Monly (©) Land Tonly (D) Land M only (E) Hand Ill only 2. Which of the following, when used as the /+ body +/ of method sus, will enable that method to compute 1+2+----+n correctly for any n > 0? public int sum(int 2) /[Precondition: n> 0 //Postcondition: 1 + 2+... +m has been returned { I+ body */ ? I return n + sum(n - 1); Dit @= return 1; else return n + sum(n - 1); I st (a == 1) return 1; else return sua(n) + sum(n - 1); (A) Lonly (B) Honly (©) Monly (©) Land Il only (E) 1,0 and I 350 Chapter 7 Recursion | 3. Refer to the method stringRecur: public void stringRecur(String s) { if (s.Length() < 15) System.out.printin(s); stringRecur(s + "#"); y ‘When will method stringRecur terminate without error? (A) Only when the length of the input string is less than 15 (B) Only when the length of the input string is greater than or equal to 15, (©) Only when an empty string is input () Forall scring inputs (E) For no string inputs 4, Refer to method strRecur: public void strRecur(String s) < Af (s-Lengtn() < 15) i t system. out.printin(s); strRecur(s + *¢*); > > When will method strRecur terminate without error? (A) Only when the length of the input string is less than 15 (B) Only when the length of the input string is greater than or equal to 15 (©) Only when an empty string is input (©) Forall sering inputs (E) For no string inputs Questions 5 and 6 refer to method resutt: public int result(int a) { if (m= 1) return 2; else return 2 * result(a - 1); . 5. What value does result (5) return? (A) 64 (B) 32 (C) 16 ©) 8 (E) 2 ‘Multiple-Choice Questions on Recursion 351 6. If 2 > 0, how many times will result be called to evaluate result(n) (including the initial call)? (A) 2 (B) 2" ©n @) 2n (B) 7. Refer to method aystery: public int mystery(int m, int a, int 4) t if @=1) else return d + mystery(n - 1, a, ds } What value is returned by the call aystery(3, 2. 6)? (A) 20 (B) 4 © 10 @) 8 (E) 2 8. Refer to method : public int f(int k, int a) £ if ee return Xi; else if @> return £(k, a ~ 0; else return fe - a, m); + ‘What value is returned by the call £(6, 8)? (A) 8 (B) 4 (3 @)2 (B) 1 352 Chapter 7 Recursion 9. What does method recur do? //Precondition: x is an array of n integers public int recur(int(] x, int a) ‘ int t; if (== 1) return x[0); else { t = recur(x, n - 1); if (xle-1] > 2) return x{n-1); else return t; } r (A) I finds the largest value in x and leaves x unchanged. (B) Icfinds che smallest value in x and leaves x unchanged. (©) Itsorts x in ascending order and returns the largest value in x. (D) Iesorts x in descending order and returns the largest value in x. (E) Ie returns x(0] or x{n~11, whichever is larger. 10. Which best describes what the printString method below does? public void printstring(String s) ¢ Sf (s.lengta) > 0) { printString(e.subatring(1)); System.out print(s.eubstring(0, 1)); y > (A) It prints sering (B) Ie prints string s in reverse order. (C) It prints only the first character of string (D) Ie prints only the first rwo characters of string s. (E) Ie prints only the last character of string ».

Potrebbero piacerti anche