Program Design and Development - Test 3 Practice Questions | C SC 227, Exams of Computer Science

Material Type: Exam; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Fall 2006;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-9ei-2
koofers-user-9ei-2 🇺🇸

5

(1)

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 227, Fall 2006 Test 3 Practice Questions on Recursion and Binary Trees
Note: Do as many as you can before your next section. Practicing the writing of recursive solutions helps to understand recursion.
Some of these exercises may be a repeat of what you have seen or done.
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();
}
4. 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
5. Implement the Greatest Common Divisor algorithm as recursive method GCD. Use recursion. Do NOT use a loop.
>
==
0)%,(
0
),( nifnmnGCD
nifm
nmGCD
6. Implement the power function recursively
=
1xif1)-n x,(x
0nif1
n)power(x, power
1
pf3
pf4

Partial preview of the text

Download Program Design and Development - Test 3 Practice Questions | C SC 227 and more Exams Computer Science in PDF only on Docsity!

C Sc 227, Fall 2006 Test 3 Practice Questions on Recursion and Binary Trees

Note: Do as many as you can before your next section. Practicing the writing of recursive solutions helps to understand recursion.

Some of these exercises may be a repeat of what you have seen or done.

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

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

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

GCDn m n ifn

m if n

GCDm n

6. Implement the power function recursively

x (x,n-1)ifx 1

1 ifn 0

power(x, n)

power

  1. Implement the factorial function recursively as fact    ⋅ − > ≤ x ( 1 )ifx 1 1 ifn 1 fact(n) factn
  2. Implement Fibonacci recursively as f    + ≥ ≤ fib(n-1) fib(n-2)ifx 1 1 ifn 2 fib(n)
  3. 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).
  4. Write recursive method printInt to print an integer with commas in the correct places.
  5. Write recursive method printIntInNewBase to print an integer in any base from 2 through hexadecimal (base 16) without using an array.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. Implement recursive method sequentialSearch. Use this array of int values. Use recursion. Do NOT use a loop. int[] a = { 22 45, 8, 9, 12, 2, 5, 6, 16, 18 };
  11. Implement recursive method binarySearch. Use this sorted array of int values. Use recursion. Do NOT use a loop. int[] a = { 2, 5, 6, 8, 9, 12, 16, 18, 22, 45 };
  12. 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.
  13. Write recursive method isPalindrime that returns true if the string argument is a palindrome. Do not use a loop. Use the following code to help answer questions 20.. import static org.junit.Assert.*; import org.junit.Test; public class RecursionTest { private class IntNode { private int data; private IntNode next; public IntNode( int newElement, IntNode referenceToNext) { data = newElement; next = referenceToNext; } } @Test public void testSumLinked() { IntNode first = new IntNode(1, null ); first = new IntNode(3, first); first = new IntNode(5, first); first = new IntNode(7, first); assertEquals (16, sumLinked(first)); }
  1. Using the following print method, write the output generated by the three calls to print in the code above public static void print (TreeNode t) { if(t != null) { System.out.print(t.data + " "); print(t.left); print(t.right); } }
  2. Write method count that takes the root of a binary tree of String elements and a String parameter to return the number of occurrences of that object in a binary tree (use equals).
  3. Write method append that takes the root of a binary tree and a String parameter to return a reference to a new binary tree that is exactly like the original but has the String appended to each node of the binary tree. The original tree structure must remain the same. Assume the original tree store a tree of String references.
  4. Write method printInOrder that takes the root of a binary tree of Strings as a parameter and prints the elements of a binary tree in an in-order traversal.
  5. Write method printPreOrder that takes the root of a binary tree of Strings as a parameter and prints the elements in a pre-order traversal.
  6. Write method printPostOrder that takes the root of a binary tree of Strings as a parameter and prints the elements in an in-post traversal.
  7. Write method printSideways that prints a tree in a hierarchical manner if you look at it rotated 90 degrees. Indent each level by 3 spaces.
  8. Write method height that takes the root of a binary tree as a parameter and returns the height. The height of an empty tree is –1, the height of a tree with one node is 0. A tree with two nodes has height 1, and in general, the height of a tree is the longest path from the root to any leaf.
  9. Write method findMin that takes the root of a binary tree as a parameter and returns the minimum value in a binary tree (assume the compareTo method has been implemented).
  10. Write method findMax that takes the root of a binary tree as a parameter and returns the minimum value in a binary tree (assume the compareTo method has been implemented).
  11. Write method size that takes the root of a binary tree of Strings as a parameter and returns the number of nodes in the tree.
  12. Write method sum that takes the root of a binary tree of Integers as a parameter and returns the sum of all of the data in the tree. Assume the data stores references to Integer objects. You need to cast and ask for intValue().
  13. Write method atDepth that takes the root of a binary tree as a parameter and returns the number of nodes at a particular depth. By definition, the root is at depth 0, its children are at depth 1, and the children of its children are at depth 2, etc.
  14. Write method isFull that takes the root of a binary tree as a parameter and returns true if the binary tree is full. A full tree is a binary tree in which each node has exactly zero or two children.
  15. Write method numberVowels that takes the root of a binary tree of Strings as a parameter and returns the number of vowels integers in a binary tree of String objects (do not include "Y"). Assume all elements are one uppercase letter.
  16. Write method allValuesAreLess that takes the root of a binary tree of Strings and a String as parameters and returns true if all of the elements in a binary tree of String objects contain values less than the String argument. Returns false otherwise.
  17. Write method isValidGameTree that takes the root of a binary tree of Strings and returns true if all internal nodes have a question mark at the end, all leaves have no question mark, and there is precisely one more number of leafs (answers) than internal nodes (questions).