Summer 2007 CMSC132 Midterm 1 Key, Exams of Computer Science

The answers key for the midterm 1 exam of the cmsc132 course during the summer 2007 semester. It includes questions on testing, hashing, big-o notation, java language features, recursion, and java collections framework. Students are required to answer essay-style questions related to these topics.

Typology: Exams

Pre 2010

Uploaded on 07/30/2009

koofers-user-a6p-1
koofers-user-a6p-1 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC132
Summer 2007
Midterm #1 Key
First Name: _______________________
Last Name: _______________________
Student ID: _______________________
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.
Total point value is 100 points.
Answer essay questions concisely using 1 or 2 sentences. Longer answers are not necessary
and are discouraged.
WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0
credit).
1
Grader Use Only:
#1 (6)
#2 (6)
#3 (8)
#4 (15)
#5 (15)
#6 (25)
#7 (25)
Total (100)
pf3
pf4
pf5
pf8

Partial preview of the text

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

CMSC

Summer 2007

Midterm #1 Key

First Name: _______________________

Last Name: _______________________

Student ID: _______________________

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.

 Total point value is 100 points.

 Answer essay questions concisely using 1 or 2 sentences. Longer answers are not necessary

and are discouraged.

 WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0

credit).

Grader Use Only:

Total (100)

Problem 1 (6 pts) Testing

a. (2 pts) What is clear box testing? Briefly explain.

Answer: Testing where you are allowed to examine the code

b. (2 pts) What is black box testing? Briefly explain.

Answer: Testing where you have no knowledge of the code

c. (2 pts) What is regression testing? Briefly explain.

Answer: Testing to assure function is not lost/changed as software is modified

Problem 2 (6 pts) Hashing

a. (2 pts) Name two hashing approaches discussed in class.

Answer: Open Addressing and Chaining

b. (2 pts) When defining a hashCode for a class, could two objects have the same hash code? Briefly

explain.

Answer: Yes as this does not violates the Java Contract.

c. (2 pts) Describe the Java hashCode contract.

Answer: Two objects that are considered equal must have the same hash code value.

Problem 3 (8 pts) Big-O

a. (2 pts) Which Java language construct supports procedural abstraction?

Answer: interface

b. (2 pts) Assume that class B extends class A. Is the following assignment VALID or

INVALID?

List k = new List();

Answer: INVALID

c. (2 pts) Assume that class B extends a class A. Is the following assignment VALID or

INVALID?

A k = new B();

Answer: VALID

d. (2 pts) What method must be defined for a class that implements the Iterable interface?

Answer: iterator()

e. (2 pts) An inner class has access to the private fields of the enclosing class (True or False).

Answer: True

f. (2 pts) A code segment involving a loop could potentially have an infinite number of flow

paths

(True or False).

Answer: True

g. (3 pts) Rewrite the following loop using the new for loop construct.

String[] members = {"Mary", "John", "Peter"}; for (int i=0; i

Problem 5 (15 pts) Recursion

Write a recursive function to compare two array of ints, returning true if the arrays have the same elements

and in the same order. Assume neither a nor b are null.

The static method sameArray has the following prototype:

public static boolean sameArray (int[ ] a , int[ ] b )

Feel free to add an auxiliary method. Non-recursive solutions will receive no credit.

One possible solution:

public static boolean sameArray(int[ ] a, int[ ] b) { if (a.length != b.length) return false; else { return sameArray (a, b, 0); } } public static boolean sameArray(int[] a, int[] b, int k) { if (k == a.length) return true; else { if (a[k] != b[k]) return false; else return sameArray(a,b,k+1) ; } }

3. Implement the removeStudent method. The method will return true if the specified student is found in

the map and it is removed from the map. Otherwise, the method will return false. If after removing the

student the corresponding section is empty, then the section must be removed from the map.

Answer:

public boolean removeStudent(String name) { for (Integer sectionNum : allSections.keySet()) { Set section = allSections.get(sectionNum); if (section.contains(name)) { section.remove(name); if (section.isEmpty()) { allSections.remove(sectionNum); } return true ; } } return false ; }

Problem 7 (25 pts) Linked Lists

Implement the methods below based on the following Java class definitions.

public class MyLinkedList {

private class Node {

private E data;

private Node next;

private Node head;

a. Define a constructor for the MyLinkedList class that creates an empty list.

Answer:

public MyLinkedList() { head = null ; }

b. Define a non-static method named addAtEnd that has the following signature:

public void addAtEnd(T element)

The method adds the element to the end of the list

Answer:

public void addEnd(T element) { Node newNode = new Node(); newNode.data = element; newNode.next = null ; if (head == null ) { head = newNode; } else { /* Traversing the list / Node curr = head; while (curr.next != null ) curr = curr.next; / Inserting at the end */ curr.next = newNode; } }

c. Define a non-static method named printReverse that has the following signature:

public void printReverse()

The method prints the element in reverse order. You may add an auxiliary method.

Answer:

public void printReverse() { printReverseAux(head); } public void printReverseAux(Node curr) { if (curr != null) { printReverseAux(curr.next); System. out .println(curr.data); } }