

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 3
This page cannot be seen from the preview
Don't miss anything!


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