University of Maryland, College Park - CS Department Midterm Exam CMSC132, Summer 2008, Exams of Computer Science

A midterm exam for the university of maryland, college park's computer science department's cmsc132 course, held in summer 2008. The exam covers topics such as algorithmic complexity, program correctness, hashing, java language features, sets and maps, and linear data structures.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-2od
koofers-user-2od 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Maryland College Park
Dept of Computer Science
CMSC132, Midterm #1 Key
Summer 2008
First Name (PRINT): ___________________________________________________
Last Name (PRINT): ___________________________________________________
University ID: _________________________________________________________
I pledge on my honor that I have not given or received any unauthorized assistance on this examination.
Your signature: _____________________________________________________________
Instructions
This exam is a closed-book and closed-notes exam.
Total point value is 100 points, 50 minutes exam.
Please use a pencil to complete the exam.
PUNT RULE: For any question, you may write PUNT, and you will get ¼ of the points for the question
(rounded down). If you feel totally lost on a question, you are encouraged to punt rather than write
down an incorrect answer in hopes of getting some partial credit.
WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit).
Grader Use Only
#1 Algorithmic Complexity (18)
#2 Program Correctness (10)
#3 Hashing (8)
#4 Language Features (24)
#5 Sets and Maps (20)
#6 Linear Data Structures (20)
Total (100)
Problem 1 (18 pts) Algorithmic Complexity
a. (6 pts) Calculate the asymptotic complexity of the code snippets below (using big-O notation) with
respect to the problem size n.
1
pf3
pf4
pf5

Partial preview of the text

Download University of Maryland, College Park - CS Department Midterm Exam CMSC132, Summer 2008 and more Exams Computer Science in PDF only on Docsity!

University of Maryland College Park

Dept of Computer Science

CMSC132, Midterm #1 Key

Summer 2008

First Name (PRINT): ___________________________________________________ Last Name (PRINT): ___________________________________________________ University ID: _________________________________________________________ I pledge on my honor that I have not given or received any unauthorized assistance on this examination. Your signature: ______________________________________________________________ InstructionsThis exam is a closed-book and closed-notes exam.Total point value is 100 points, 50 minutes exam.Please use a pencil to complete the exam.PUNT RULE: For any question, you may write PUNT, and you will get ¼ of the points for the question (rounded down). If you feel totally lost on a question, you are encouraged to punt rather than write down an incorrect answer in hopes of getting some partial credit.WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit)._ Grader Use Only #1 Algorithmic Complexity (18) #2 Program Correctness (10) #3 Hashing (8) #4 Language Features (24) #5 Sets and Maps (20) #6 Linear Data Structures (20) Total (100)

Problem 1 (18 pts) Algorithmic Complexity

a. (6 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=0; i<= n- 3; i++) { f( n ) = O( n ) System.out.println("Hello"); }
  2. for (i=1; i<= n ; i++) { f( n ) = O( n^2 ) for (k= 0 ; k<= n /2; k++) { System.out.println("Hello"); } }
  3. for (int i= n -1; i<= n ; i++) { f( n ) = O( log(n) ) for (int j=1; j<= n ; j=j*2) { System.out.println("Hello"); } } b. (4 points) Give the asymptotic bound of the following functions:
  4. n^3 + n log( n ) f( n ) = O( n^3 )
  5. 2 n^4 - 500 n - n^2 f( n ) = O( n^4 ) c. (2 pts) List the following big-O expressions in order of asymptotic complexity (lowest complexity first) where k represents a constant. O(nlog(n)) O(k n ) O(nn) O(log(n)) Answer: O(log(n)) O(nlog(n)) O(k n ) O(nn) d. (2 pts) What is the complexity of computing a[i] for an array of length n? Answer: O(1) e. (2 pts) What is the asymptotic complexity of finding in an element in a sorted array if we use binary search? Answer: O(log(n)) f. (2 pts) Give the complexity of an algorithm for problem size n whose running time:

i. Increases by 2 when n doubles O( log(n) )

ii. Is unchanged when n triples O( 1 )

Problem 2 (10 pts) Program Correctness and Exceptions

1. (2 pts) F  60% code coverage implies 40% of the code is incorrect.

  1. (2 pts) T  A private visibility modifier allows us to enforce encapsulation.
  2. (2 pts) F  A Constructor cannot be defined for an abstract class.
  3. (2 pts) When we call the method Collections.sort(L) on the Collection object L the data in L is sorted. Which interface (Comparable or Comparator) is implemented by elements of object L? Answer: Comparable
  4. (2 pts) Complete the following variable declaration so we have an ArrayList with objects that implement the Comparable interface. Answer: ArrayList < Comparable > aList; or ArrayList <? extends Comparable >
  5. (2 pts) Complete the following variable declaration so we have an ArrayList with objects that belong to the Reptile class or are subtypes of this class. Answer: ArrayList <? extends Reptile > bList;
  6. (2 pts) Complete the following variable declaration so we have an ArrayList with objects that can be of any type. Answer: ArrayList <? > cList;
  7. (8 pts) Make the following class generic so that it can deal with an arbitrary class rather than only Boolean. Feel free to cross out parts of the following code. Answer(8 pts): public class MyDS { private T [] data; private int curr = 0; public MyDS(int size) { data = (T[]) new Object[size]; } public T first() { return data[0]; } public void append(T val) { data[curr++] = val; } } Problem 5 (20 pts) Sets and Maps The Assistants class maintains, for a set of courses, the set of TAs for each course.

public class Assistants { Map<String, Set> map; public Assistants() { // YOU MUST IMPLEMENT THIS METHOD } public void addTA(String course, String taName) { // YOU MUST IMPLEMENT THIS METHOD } public void displayTAsPerCourse() { // YOU MUST IMPLEMENT THIS METHOD } }

  1. (3 pts) Implement a constructor for Assistants that creates an empty map.

Answer:

map = new HashMap<String, Set>();

  1. (10 pts) Implement the addTA method that adds a teaching assistant to a specific course. A map entry for the course must be created if one does not exist. NOTE: If punting 2 pts credit

Answer:

Set tas = map.get(course); if (tas == null) { tas = new TreeSet(); map.put(course, tas); } tas.add(taName);

  1. (7 pts) Implement the displayTAsPerCourse method that prints (using System.out.println) the name of a course followed by the TAs for the course.

Answer:

for (String c : map.keySet()) { System. out .println(c); for (String taName: map.get(c)) System. out .println(taName); } Problem 6 (20 pts) Linear Data Structures

public ArrayList getList() { ArrayList result = new ArrayList(); Node temp = head; while (temp != null) { result.add(temp.data); temp = temp.next; } return result; }