Program Design and Development - Recursion Questions for Quiz | C SC 227, Quizzes of Computer Science

Material Type: Quiz; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Spring 2007;

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-yjz-1
koofers-user-yjz-1 🇺🇸

7 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 127B: 20 Recursion Questions
For Quiz on 30-March-2007
Do as many as you can before section. Writing many recursive solutions will help you understand recursion. Some
of these exercises may be a repeat of what you have seen or done, but now write them by hand to practice for the
quiz on recursion, 30-March-2007
1. Write the output generated by the method call mystery("X", 6);
public void mystery(String s, int digit) {
if(digit <= 1)
System.out.println(digit);
else {
s = s + "<";
mystery(s, digit - 2);
System.out.println(s + digit);
}
}
2. Write the return values from each call to the method named mystery.
____mystery(0) ____mystery(1) ____mystery(2) ____mystery(3) ____mystery(4)
public int mystery(int n) {
if (n <= 0)
return 1;
else
return 3 + mystery(n - 1);
}
3. Write the return values from each call to the method named mysteryTwo.
________ mysteryTwo("T") ________ mysteryTwo("ab") ________ mysteryTwo("123")
public String mysteryTwo(String s) {
if (s.length() == 0)
return "";
else
return mysteryTwo(s.substring(1, s.length())) + "/" + s.charAt(0);
}
4. Write the output of stars with arguments 1, 2, and 3
public static void stars(int n) {
if (n > 1)
stars(n-1);
for (int i = 0; i < n; i++)
System.out.print("*");
System.out.println();
}
5. Write recursive method goingUp so it displays all the numbers from the first argument to the last in
ascending order (separate with a space). Use recursion, do NOT use a loop.
goingUp(1, 5); // 1 2 3 4 5
goingUp(2, 7); // 2 3 4 5 6 7
goingUp(3, 3); // 3
6. Implement the Greatest Common Divisor algorithm as recursive method GCD. Use recursion. Do NOT
use a loop.

0)%,(
0
),( nifnmnGCD
nifm
nmGCD
1
pf2

Partial preview of the text

Download Program Design and Development - Recursion Questions for Quiz | C SC 227 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 127B: 20 Recursion Questions

For Quiz on 30-March-

Do as many as you can before section. Writing many recursive solutions will help you understand recursion. Some

of these exercises may be a repeat of what you have seen or done, but now write them by hand to practice for the

quiz on recursion, 30-March-

1. Write the output generated by the method call mystery("X", 6);

public void mystery(String s, int digit) { if(digit <= 1) System.out.println(digit); else { s = s + "<"; mystery(s, digit - 2); System.out.println(s + digit); } }

2. Write the return values from each call to the method named mystery.

____mystery(0) ____mystery(1) ____mystery(2) ____mystery(3) ____mystery(4) public int mystery(int n) { if (n <= 0) return 1; else return 3 + mystery(n - 1); }

3. Write the return values from each call to the method named mysteryTwo.

________ mysteryTwo("T") ________ mysteryTwo("ab") ________ mysteryTwo("123") public String mysteryTwo(String s) { if (s.length() == 0) return ""; else return mysteryTwo(s.substring(1, s.length())) + "/" + s.charAt(0); }

4. Write the output of stars with arguments 1, 2, and 3

public static void stars(int n) { if (n > 1) stars(n-1); for (int i = 0; i < n; i++) System.out.print("*"); System.out.println(); }

5. Write recursive method goingUp so it displays all the numbers from the first argument to the last in

ascending order (separate with a space). Use recursion, do NOT use a loop.

goingUp(1, 5); // 1 2 3 4 5 goingUp(2, 7); // 2 3 4 5 6 7 goingUp(3, 3); // 3

6. Implement the Greatest Common Divisor algorithm as recursive method GCD. Use recursion. Do NOT

use a loop.

^     ( , % ) 0 ( , )^0 GCDn m n ifn GCDm n m if^ n

  1. Implement the power function recursively ^      x (x,n-1)ifx 1 power(x, n)^1 ifn^0 power

  2. Implement the factorial function recursively as fact ^       x ( 1 )ifx 1 fact(n) 1 ifn^1 factn

  3. Implement Fibonacci recursively as f ^      fib(n-1) fib(n-2)ifx 1 fib(n)^1 ifn^2

  4. Write recursive method addReciprocals that takes an integer as a parameter and returns the sum of the first n reciprocals. addReciprocals(n) returns (1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ... + 1.0/n).

  5. Write recursive method sum that returns the sum of all the int elements in a filled array referenced by y. Do not use a loop. You must have a recursive call somewhere in your answer.

  6. Write recursive method getMax that returns the largest integer in a filled array of int referenced by x. Do not use a loop. You may use a helper method.

  7. Write recursive method printArray that prints all array elements in a filled array of ints referenced by x. Do not use a loop. You may use a helper method.

  8. Write recursive method printArrayInReverse that prints all array elements in a filled array of ints referenced by x in their reverse order. Do not change the array. Do not use a loop. You may use a helper method.

  9. Implement recursive method sequentialSearch. Use recursion. Do NOT use a loop. Use this array of int values. int[] a = { 22 45, 8, 9, 12, 2, 5, 6, 16, 18 };

  10. Implement recursive method binarySearch. Use recursion. Do NOT use a loop. Use this sorted array of int values. int[] a = { 2, 5, 6, 8, 9, 12, 16, 18, 22, 45 };

  11. Write recursive method reverse that reverses the array elements in a filled array of ints referenced by x. Do not use a loop. You may use a helper method.

  12. Write recursive method isPalindrime that returns true if the string argument is a palindrome. Do not use a loop.

  13. Write recursive method printInt to print an integer with commas in the correct places.

  14. Write recursive method printIntInNewBase to print an integer in any base from 2 through hexadecimal (base 16) without using an array.