Fall 2007 CMSC132 Midterm 1, Exams of Computer Science

The fall 2007 cmsc132 midterm 1 exam for computer science students. It includes multiple choice and fill-in-the-blank questions on java language features, program correctness, algorithm complexity, hashing, linear data structures, and sets and maps.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-qtg
koofers-user-qtg 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC132
Fall 2007
Midterm #1
First Name: _______________________
Last Name: _______________________
Student ID: _______________________
Discussion TA: ______________________
Discussion Time: _____________________
I pledge on my honor that I have not given or received any unauthorized assistance on this
examination.
Your signature: _____________________________________________________________
General Rules (Read):
This exam is closed book and closed notes.
If you have a question, please raise your hand.
Answer True/False questions by circling the T or F at the end of the question.
Note: +1 point if correct, -1 point if incorrect, 0 point if no answer given
Answer fill-in-the-blank questions with 1 or 2 words
Note: Longer answers are not necessary and will be penalized.
Answer essay questions concisely using 1 or 2 sentences.
Note: Longer answers are not necessary and will be penalized.
WRITE NEATLY. Unreadable answers will not be graded (i.e., 0 points).
Honors section questions only count for credit for students in the honors section.
1
Grader Use Only:
#1 Language Features (12 pts)
#2 Program Correctness (12 pts)
#3 Algorithm Complexity (14 pts)
#4 Hashing (12 pts)
#5 Linear Data Structures (24 pts)
#6 Sets and Maps (26 pts)
Total (100 pts)
Honors (15 pts)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Fall 2007 CMSC132 Midterm 1 and more Exams Computer Science in PDF only on Docsity!

CMSC

Fall 2007

Midterm

First Name: _______________________

Last Name: _______________________

Student ID: _______________________

Discussion TA: ______________________

Discussion Time: _____________________

I pledge on my honor that I have not given or received any unauthorized assistance on this

examination.

Your signature: _____________________________________________________________

General Rules (Read):

 This exam is closed book and closed notes.

 If you have a question, please raise your hand.

 Answer True/False questions by circling the T or F at the end of the question.

Note: +1 point if correct, -1 point if incorrect, 0 point if no answer given

 Answer fill-in-the-blank questions with 1 or 2 words

Note: Longer answers are not necessary and will be penalized.

 Answer essay questions concisely using 1 or 2 sentences.

Note: Longer answers are not necessary and will be penalized.

 WRITE NEATLY. Unreadable answers will not be graded (i.e., 0 points).

 Honors section questions only count for credit for students in the honors section.

Grader Use Only:

#1 Language Features (12 pts)

#2 Program Correctness (12 pts)

#3 Algorithm Complexity (14 pts)

#4 Hashing (12 pts)

Linear Data Structures

(24 pts)

Sets and Maps

(26 pts)

Total (100 pts)

Honors (15 pts)

Problem 3 (14 pts) Algorithmic Complexity

a. (8 pts) Calculate the asymptotic complexity of the code snippets below (using big-O notation) with respect to the problem size n.

  1. for (int i=1; i<= n ; i++) { f( n ) = O( ) i = i + n /2; }
  2. for (i=1; i<= n /3; i++) { f( n ) = O( ) for (k=1; k<= n /2; k++) { for (m= n ; m< n +100; m++) { s = s + 10; } } }
  3. for (int i= n /2; i<= n ; i++) { f( n ) = O( ) for (int j=1; j<= n ; j=j*2) { // ... } }
  4. n^2 log( n ) + 100 n f( n ) = O( ) b. (2 pts) List the following big-O expressions in order of asymptotic complexity (lowest complexity first) O(nlog(n)) O(1) O(log(n)) O(2n) O(n^3 ) c. (4 pts) Assume you are playing the number guessing game, where you are trying to guess a number x between 1 and n. For each number guessed, the answer may be “correct”, “too high”, or “too low.” Consider a game where x = 30 and n = 64:
  5. List your first 3 guesses using a linear search algorithm __________________
  6. List your first 3 guesses using a binary search algorithm __________________
  7. List the worst case value for x using a linear search algorithm __________________
  8. List the best case value for x using a binary search algorithm __________________

Problem 4 (12 pts) Hashing

a. (4 pts) Hash tables Assuming the following hash values are computed for the keys A, B, C, D, and E: H(A) = 4, H(B) = 3, H(C) = 3, H(D) = 3, H(E) = 0 Rewrite the following two hash tables to illustrate the state of each table after A, B, C, D, and E have been inserted in the table (in the specified order). Open addressing Chaining (bucket hashing) b. (4 pts) Load factor

  1. The load factor for a hash table is calculated as ____________________
  2. A search in an open addressing hash table with load factor 0.10 is O( )
  3. A search in an open addressing hash table with load factor 1.00 is O( )
  4. A search in an chaining (bucket) hash table with load factor 1.00 is O( ) c. (4 pts) Java hashCode( )
  5. The default Object hashCode and equals methods satisfy the Java Hash Code contract T F
  6. Comparing the hashCode( ) values of two objects answers whether 2 objects are equal T F
  7. The Java hash code contract requires that the hashCode( ) method guarantees the following: Λ Λ Λ Λ Λ 0^ Λ 1 2 3 4 5 Λ Λ Λ Λ Λ 0^ Λ 1 2 3 4 5

Problem 6 (26 pts) Sets and Maps

You are asked to use the HashSet and HashMap classes to implement a TVPrograms class to track the set of channels running a show. The class must also be able to report the set of shows running on each channel. public class TVPrograms { private Map<String, Set> showChannelMap; public void addChannelToShow(String showName, int channel) { // YOU MUST IMPLEMENT THIS METHOD } public Set channelsInShow(String showName) { // YOU MUST IMPLEMENT THIS METHOD } public Set showsInChannel(int channel) { // YOU MUST IMPLEMENT THIS METHOD } } What You Must Implement

  1. (2 pts) Implement a constructor for TVPrograms that creates an empty showChannelMap.
  2. (10 pts) Implement a addChannelToShow method that stores information a specified show is running on a specified channel. Note: you must handle the case where a show is added for the first time.

3. (2 pts) Implement a channelsInShow method that returns set of channels running specified show.

4. (12 pts) Implement a showsInChannel method that returns set of shows running on specified channel.

You may find the following Map methods helpful:  V get (Object key) - Returns the value to which this map maps the specified key.  V put (K key,V value) - Associates the specified value with the specified key in this map.  Set keySet () - Returns a set view of the keys contained in this map.  boolean isEmpty () - Returns true if this map contains no key-value mappings. You may find the following Set methods helpful:  boolean contains (Object o) - Returns true if this set contains the specified element.  boolean add (E o) - Adds the specified element to this set if it is not already present  V remove (Object key) - Removes the mapping for this key from this map if it is present  boolean isEmpty () - Returns true if this set contains no elements. Use the next page to provide your answers

NOTE: Only Honors Section Students Will Receive Credit

(15 pts) Honors Section

a. (6 pts) True or False (1pt each)

  1. Initialization blocks are executed before constructors T F
  2. Invoking a static method requires an instance of the class. T F
  3. Class methods may be declared outside the scope of the class T F
  4. Using generics can cause new compilation errors, even if no new errors are added. T F
  5. The clone method in the Object class makes a deep copy of an object. T F
  6. A final class cannot be overridden. T F b. (5 pts) Assume you are playing the number guessing game, where you are trying to guess a number x between 1 and n. For each number guessed, the answer may be “correct”, “too high”, or “too low.” You decide to use an enhanced linear search algorithm that starts at a random number 1 ≤ y ≤ n and increases the guess by 1 if the answer is “too low”, and decreases the guess by 1 if the answer is “too high”.
  7. What is the # of guesses needed when n=64, x=30, and y=40? __________________
  8. What is the best case # of guesses of needed for random x and y? __________________
  9. What is the worst case # of guesses of needed for random x and y? __________________
  10. What is the average # of guesses of needed for random x and y? __________________
  11. What is the asymptotic complexity of the enhanced linear search algorithm? O( ) c. (4 pts) You now decide to combine the enhanced linear search algorithm with the binary search algorithm. You start with 3 steps of the binary search algorithm, then switch to the enhanced linear search algorithm (which starts with the last guess of the binary search algorithm).
  12. What is the # of guesses needed when n=64 and x=40? __________________
  13. What is the worst case # of guesses of needed for random x? __________________
  14. What is the average # of guesses of needed for random x? __________________
  15. What is the asymptotic complexity of the combined algorithm? O( )