CSE 1302C Test2 SP12.docx

advertisement

CSE 1302C – Test 2

Directions: You have 90 minutes to complete this exam. It is a pencil and paper only exam.

3/29/12

Question 1 (25 pts): What is the output of the following code? Please be careful on this one… Again, I would recommend drawing the stack. static int doSomething ( int j , int k )

{

Console.WriteLine

( j + "," + k ); if (( j > 0 ) && ( k > 0 ))

{ j -= k ; k-; return j + doSomething ( j , k );

} else

{ return k + j ;

}

} static void Main ( string [] args )

{ int result = doSomething ( 11 , 4 );

Console.WriteLine

( result );

}

Question 2 (40 pts): Using “Big-Oh” notation, show how long would it take to do the following operations in the most efficient way. Assume you are working with integers. Write any assumptions that you may have (i.e. any extra variables you may have). Since there are two parts to each question, you can write your answer as the time it takes to do each part (i.e. “step one takes…” and “step two takes”).

If in doubt, you may also consider explaining your reasoning.

Find the maximum element in an unsorted linked list and put it into an unsorted array.

Find the smallest element in a sorted array and put it into an unsorted linked lists.

Determine if a number exists (e.g. 17) in a sorted array and if so, put it into an unsorted array.

Determine if a number exists (e.g. 24) in an unsorted array and if so, put it into a Binary Search

Tree.

Question 3 (35 pts): Assume you have a file called “salaries.txt” that contains the salaries of all your employees, where each salary is on its own line. Write a program that reads in each salary from file, converts it to an integer and puts it into a List. The program should output (to Console) the largest salary. You must handle any exceptions that occur.

Download