Recursion Practice Quiz: Solving Recursive Problems, Quizzes of Computer Science

A practice quiz for students in a csc 127b course, focusing on recursion. It includes various recursive problems, such as writing the output of mystery functions, implementing recursive methods for tribonacci sequence, power function, factorial function, and greatest common divisor algorithm. Additionally, it covers abstract things like changing all occurrences of 'x' to 'y' in a string and checking if a string is a palindrome. The document also includes recursive solutions for array processing and linked structure processing.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-xf4-1
koofers-user-xf4-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
C Sc 127B Practice Quiz 4 Section Leader __________ Name _______________________________
From each class of Recursion problems, complete the a question before your section this week. These and the other questions
constitute the practice quiz. All answers will be linked Friday afternoon. Writing many recursive solutions will help you understand
recursion. Note: Some of these exercises may be a repeat of what you have seen or done.
Before section, complete these 5 problems with pencil and paper and turn in at the beginning of your section for a 3pt
homework: 1a, 2a, 3a, 4a, and 5a. Before the quiz, complete all.
1. Write the output from mystery functions
1a) 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);
}
}
1b) 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);
}
1c) 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);
}
1d. 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();
}
pf3

Partial preview of the text

Download Recursion Practice Quiz: Solving Recursive Problems and more Quizzes Computer Science in PDF only on Docsity!

C Sc 127B Practice Quiz 4 Section Leader __________ Name _______________________________

From each class of Recursion problems, complete the “a” question before your section this week. These and the other questions constitute the practice quiz. All answers will be linked Friday afternoon. Writing many recursive solutions will help you understand recursion. Note: Some of these exercises may be a repeat of what you have seen or done.

Before section, complete these 5 problems with pencil and paper and turn in at the beginning of your section for a 3pt homework: 1a, 2a, 3a, 4a, and 5a. Before the quiz, complete all.

1. Write the output from mystery functions

1a) 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); } }

1b) 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); }

1c) 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); }

1d. 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(); }

2) Use a recursive definition to write a recursive method

2a) Implement the tribonacci recursively.

trib(n-1) trib(n-2) trib(n-3)if x 1

1 if n 3 trib(n)

2b) Implement the power function recursively.

x (x,n-1)if x 1

1 if n 0 power(x,n) power

2c) Implement the factorial function recursively as fact.

x ( 1 ) ifx 1

1 if n 1 fact(n) factn

2d) Implement the Greatest Common Divisor algorithm as recursive method GCD. Use recursion. Do NOT use a loop.

GCDn m n ifn

m ifn GCDmn

3) Write a method to do an abstract thing

3a) Given a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been

changed to 'y' chars.

changeXY("codex") → "codey" changeXY("xxhixx") → "yyhiyy" changeXY("xhixhix") → "yhiyhiy"

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

3c) Write recursive method printInt to print an integer with commas in the correct places. printInt(12004) should print 12,004 and printInt(123456780) should print 123,456,

3d) Write recursive method printIntInNewBase to print an integer in any base from 2 through decimal (base 10) without using an array. printIntInNewBase(17, 10) should print 17, printIntInNewBase(17, 2) should print 10001, and printIntInNewBase(17, 8) should print 21.

4) Recursive solutions to array processing

4a) Write recursive method getRange that returns the range in a filled array of int referenced by x. The range is defined as the largest minus the smallest. Do not use a loop. You may use a helper method.

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

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

4d) Implement method compare that returns 0 if all elements in two arrays of ints are in the same exact order and have the same length. Return the difference between the first two non-equal integers—negative if the element in the first array