CSE 1302C Test 2_FA10.docx

advertisement
CSE 1302C – Test 2
11/9/2010
Your grade:____________ + 5 = _______________
Directions: Pencil and paper only (no books or notes). All answers are to be your own without the
assistance of others (i.e. don’t cheat).
Question 1: What is the output of the following code chunk? Show the stack at its tallest.
static double recurse(double i, double j)
{
Console.WriteLine ("i is " + i + " and j is " + j);
if (i <= 1)
{
return 0;
}
else
{
return 1 + recurse(i / j, j);
}
}
static void Main(string[] args)
{
Console.WriteLine(recurse(16.0, 2.0));
}
Question 2: Why does polymorphism work? Show an example. Hint: what does it leverage?
Question 3 (25 pts): Assume we have an array of n sorted numbers (ints) and a linked list of n unsorted
numbers (ints). Using O-notation (e.g. O(log2n) ), fill in how long it takes to do the following operations
in the most efficient way possible.
Array
Linked List
Find the min
Find the max
Find the average of all numbers
Insert a new number
Find if a number is in the data
structure (e.g. if it exists)
Remove a number if it exists
Question 4: Assume you have a text file called “data.jef”. Write a code snippet that counts the total
number lines in that file and prints out the total to the user (e.g. “this file has 105 lines of text”).
Extra credit (5 points): what’s the common name of the function in question 1?
Download