Resolution for Test - Program Design and Development | 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: Spring 2008;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-mst-1
koofers-user-mst-1 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
C Sc 227: 28 Practice Test Questions for 2-May-08 SL_________ Name _____________________
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 fro m 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 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 power function recursively
!
"
#
$%
=
1xif1)-n x,(x
0nif1
n)power(x, power
6. Implement Fibonacci recursively as f
!
"
#
$+
%
1xif 2)-fib(n 1)-fib(n
2n if1
fib(n)
7. Write recursive method addReciprocals that takes an integer as a pa rameter 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).
8. Write recursive method sum that returns the sum of all the int elements in a filled array (filled to
capacity) referenced by x. Do not use a loop. You must have a recursive call so mewhere in your answer.
9. Write recursive method printArray that prints all array elements in a filled array of ints referenced
by x. Do not use a loop.
public void printArray(int[] x)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

C Sc 227: 28 Practice Test Questions for 2 - May- 08 SL_________ Name _____________________

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 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 power function recursively

x (x,n-1)ifx 1

1 ifn 0

power(x, n)

power

6. Implement Fibonacci recursively as f

fib(n-1) fib(n-2)ifx 1

1 ifn 2

fib(n)

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

8. Write recursive method sum that returns the sum of all the int elements in a filled array (filled to

capacity) referenced by x. Do not use a loop. You must have a recursive call somewhere in your answer.

9. Write recursive method printArray that prints all array elements in a filled array of ints referenced

by x. Do not use a loop.

public void printArray(int[] x)

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

public void reverse(int[] x)

11. Write recursive method isPalindrime that returns true if the string argument is a palindrome.

public boolean isPalindrome(String str)

Use the following code to help answer questions 12.. 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)); }

12. Write recursive method sumLinked that return the sum of all int elements in a linked structure

beginning with the node referenced by first (assume the data field is int).

13. Write a test method for occurrencesOf that return the number of times an int element occurs in a

linked structure beginning with the node referenced by first (assume the data field is int).

14. Write recursive method occurrencesOf that return the number of times an int element occurs in a

linked structure beginning with the node referenced by first (assume the data field is int).

Use class BinarySearchTree (from lecture) and the inner TreeNode for questions 15.. public class BinarySearchTree<E extends Comparable> { private class TreeNode { private E data; private TreeNode left; private TreeNode right; TreeNode(E theData) { data = theData; left = null ; right = null ; } } private TreeNode root; public BinarySearchTree() { root = null ; } public boolean insert(E newElement) { // … } }

23. Is this a Binary Search Tree (yes or no)? _______

M

G R

G H X

24. Is this a Binary Search Tree (yes or no)? _______

M

R H

X Q G

25. What potential benefit is there to using a binary search tree rather than an array to store the same

collection of elements?

26. Draw the a picture of the binary search tree that results from the following code that uses our

BinarySearchTree class:

BinarySearchTree aBST = new BinarySearchTree(); aBST.insert("Matrix"); aBST.insert("Antz"); aBST.insert("Bean"); aBST.insert("Shaft"); aBST.insert("Scream"); aBST.insert("Titanic");

27. Complete the output that would be generated by this code fragment

ArrayList list = new ArrayList(); list.add(12); list.add(23); list.add(-5); list.add(-5); list.add(12); Collections. sort (list); Iterator itr = list.iterator(); while (itr.hasNext()) System. out .print(itr.next() + " "); System. out .println(); System. out .println("===="); System. out .println(list.contains(-5)); System. out .println(list.contains(9999)); System. out .println(Collections. min (list)); System. out .println(Collections. binarySearch (list, - 5)); System. out .println(Collections. binarySearch (list, 23)); System. out .println(Collections. binarySearch (list, 999) < 0); System. out .println("===="); Collections. reverse (list); itr = list.iterator(); while (itr.hasNext()) System. out .print(itr.next() + " ");

Add output here

28. Complete the code that reads all integers from the file named "numbers.txt" (assuming it is found)

and prints how many integers were found (assume numbers only has integers). The output should be

Count: 18 Scanner scanner = null; the file "numbers.txt" try { scanner = new Scanner(new File("numbers.txt")); } catch (FileNotFoundException fnfe) { System. out .println("the file 'numbers' was not found"); } int count; // Complete the code System.out.println("Count: " + count);

printArray(x,index+1); } }

10.. public^ void^ reverse(int[]^ x)^ {

reverse(x, 0, x.length-1); } private void reverse(int[] x, int left, int right) { if (left < right) { int temp = x[left]; x[left] = x[right]; x[right] = temp; reverse(x, left + 1, right - 1); } }

11. public^ boolean^ isPalindrome(String^ s)^ {

if(s.length() <= 1) //empty string is a palindrome return true; else { if( s.charAt[0] != s.charAt(s.length()-1)) return false; else return isPalindrome(s.substring(1,s.length()-1)); } }

12. public^ int^ sumLinked()^ {

return sum(first); } private int sum(Node n) { if (n == null) return 0; else return n.data + sum(n.next); }

13. public^ void^ testOccurencesOf()^ {

IntNode front = new IntNode(1); front = new IntNode(3, front); front = new IntNode(1, front); front = new IntNode(7, front); front = new IntNode(1, front); assertEquals (3, occurencesOf(front, 1)); }

14. private^ int^ occurencesOf(IntNode^ ref,^ int^ search)^ {

if (ref == null ) return 0; else if (ref.data == search) return 1 + occurencesOf(ref.next, search); else return 0 + occurencesOf(ref.next, search); } ------or------- private int occurencesOf(IntNode ref, int search) { if (ref == null ) return 0; else { int found = 0; if (ref.data == search) found++; return found + occurencesOf(ref.next, search); } }

15. D

/ \

B E

/ \ \

A C F

16. F^ D^ E^ C^ B^ A

17. public^ int^ count(String^ element)^ {

return count(root, element); } private int count(TreeNode node, String element) { if (node == null ) return 0; else if (element.equals(node.data)) return 1 + count(node.left, element) + count(node.right, element); else return 0 + count(node.left, element) + count(node.right, element); } -------or--------- public int count(String element) { return count(root, element); } private int count(TreeNode node, String element) { if (node == null ) return 0; else { int found = 0; if (element.equals(node.data)) found++; return found + count(node.left, element)

  • count(node.right, element); } }

18. public^ void^ append(String^ s)^ {

appendHelper(root, s); } private void appendHelper(TreeNode t, String s) { if (t != null ) { t.data += s; appendHelper(t.left, s); appendHelper(t.right, s); } }

19. public^ void^ printInOrder^ ()^ {

printSideways(root); } private void printInOrder(TreeNode curr) { if(curr!=null) { printInOrder(curr.left); System.out.println(curr.data); printInOrder(curr.right); } }

27. -^5 -^5 12 12

true

false

  • 5

true

28. count^ =^ 0;

while (scanner.hasNextInt()) {

scanner.nextInt();

count++;