This preview shows pages 1–4. Sign up to view the full content.
Name: ________________________
Midterm 2
CS 20
Fall 2008
Short Answer:
_____/25
Output:
_____/25
removeHead:
_____/15
iterative countOccurrences
_____/20
recursive countOccurrences
_____/15
Total:
_____/100
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 1)
Write the base case and recursive case of each of the following problems
(5 pts
each):
a) Calculating the value 4
x
Base Case:
Recursive Case:
b) Counting the number of occurrences of an element in a linked list
Base Case:
Recursive Case:
c) Your CD with initial balance b earns 4.5% interest per year.
Calculate the value in
year n.
Base Case:
Recursive Case:
2)
10 pts  Write the recursive method, in Java, to calculate the value 4
x
public static int fourToTheX(int x)
{
}
3)
(15 pts) (2355)
public static int mystery(int x)
{
if (x <= 1)
return 3;
else
return (3 + mystery(x1));
}
What is the output for mystery(1)?
What is the output for mystery(5)?
In one sentence, describe what the previous method calculates:
What is the computational complexity of the above method?
Give an explanation.
4) 10 pts  What is the output of the following code?
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 12/27/2011 for the course CMPSC 40 taught by Professor Egiceoclu during the Fall '09 term at UCSB.
 Fall '09
 Egiceoclu

Click to edit the document details