Recursion Quiz 5: Solutions for C Sc 227, Quizzes of Computer Science

The solutions for quiz 5 of c sc 227, which covers various recursion questions. It includes the return values for the 'mystery' and 'mysterytwo' methods, the output generated by 'mystery', 'stars', and 'recursive' methods, and the implementation of 'odddownevenup' and 'count8' and 'countx' methods as required by nick parlante javabat problems.

Typology: Quizzes

Pre 2010

Uploaded on 08/31/2009

koofers-user-gja
koofers-user-gja 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227: 20 Recursion Questions for Quiz 5 on 12-July-2007 @ 10:30am
1. Write the return values from each call to the method named mystery. (4pts)
____mystery(0) ___mystery(1) ____mystery(2) ____mystery(3)
public int mystery(int n) {
if (n <= 0)
return 1;
else
return 3 + mystery(n - 1);
}
2. Write the return values from each call to the method named mysteryTwo. (6pts)
______ mysteryTwo("T") ________ mysteryTwo("ab") ________ mysteryTwo("123")
public String mysteryTwo(String s) {
if (s.length() == 0)
return "";
else
return s.charAt(0) + "/" + mysteryTwo(s.substring(1)) ;
}
3. Write the output generated by the following method calls (6pts)
mystery("X", 1);
mystery("Y", 2); mystery("Z", 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);
}
}
4. Write the output of stars with arguments 1, 2, and 3
stars(1)
stars(2)
stars(3)
public void stars(int n) {
if (n > 1)
stars(n-1);
for (int i = 0; i < n; i++)
System.out.print("*");
System.out.println();
}
1
pf3

Partial preview of the text

Download Recursion Quiz 5: Solutions for C Sc 227 and more Quizzes Computer Science in PDF only on Docsity!

C Sc 227: 20 Recursion Questions for Quiz 5 on 12-July-2007 @ 10:30am

1. Write the return values from each call to the method named mystery. (4pts)

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

2. Write the return values from each call to the method named mysteryTwo. (6pts)

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

3. Write the output generated by the following method calls (6pts)

mystery("X", 1); mystery("Y", 2); mystery("Z", 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); } }

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

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

  1. Write the output generated with the following method calls recursive( 0, "A", "B", "C" ); recursive( 1, "A", "B", "C"); recursive( 2, "A", "B", "C"); public void recursive(int num, String one, String two, String three) { if( num <= 0 ) System.out.println(num + " " + one + " " + two + " " + three); else { System.out.println(num + " " + three + " " + two + " " + one); recursive( num - 1, three, two, one ); } } 6.Using recursion, complete method oddDownEvenUp that first prints all odd integers from the largest odd number <= argument down to 1. The same method call must then print all the even integers in the range of 2 through the largest even integers <= argument. The following method calls shown to the left must generate the output shown to the right. (10pts) Method call Required Output oddDownEvenUp(1); oddDownEvenUp(2); oddDownEvenUp(3); oddDownEvenUp(4); oddDownEvenUp(5); oddDownEvenUp(9);

public void oddDownEvenUp(int n) {