Midterm Exam with Key for Object-Oriented Programming II | CMSC 132, Exams of Computer Science

Material Type: Exam; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 02/13/2009

koofers-user-g8s-1
koofers-user-g8s-1 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC132
Summer 2007
Midterm #2 KEY
First Name: _______________________
Last Name: _______________________
Student ID: _______________________
Section time ___________ TA: __________________________
General Rules (Read):
This exam is closed book and closed notes.
If you have a question, please raise your hand.
Total point value is 100 points.
Answer True/False questions by circling the True or False at the end of the question.
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. (20 pts) Heaps/Trees
Use the following binary tree/heap to answer the questions that follow.
1
Grader Use Only:
#1 (20)
#2 (10)
#3 (12)
#4 (12)
#5 (10)
#6 (36)
Total (100)
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam with Key for Object-Oriented Programming II | CMSC 132 and more Exams Computer Science in PDF only on Docsity!

CMSC

Summer 2007

Midterm #2 KEY

First Name: _______________________

Last Name: _______________________

Student ID: _______________________

Section time ___________ TA: __________________________

General Rules (Read):  This exam is closed book and closed notes.  If you have a question, please raise your hand.  Total point value is 100 points.  Answer True/False questions by circling the True or False at the end of the question.  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. (20 pts) Heaps/Trees Use the following binary tree/heap to answer the questions that follow.

Grader Use Only:

Total (100)

a. What is the big O notation for heapsort? Answer: O(nlog(n)) b. Heaps are balanced trees. True/False. Answer: True c. A heap is a binary search tree. True/False. Answer: False d. List the order nodes are traversed in an depth-search traversal of the tree Answer: 8,9,11,13,10,12,14  One possible traversal e. List the order nodes are traversed in a breadth-first traversal of the tree Answer: 8, 9, 10, 11, 13, 12, 14  One possible traversal f. (4 pts) Draw the heap that would result from inserting 5 in the above heap. g. (4 pts) Draw the heap that would result by deleting 8 from the original heap.

True/False. Answer: True b. (2 pts) What is the load factor? i. Number of entries in the hash table divided by table size ii. Maximum number of entries that can be placed in the table iii. The load generated by the multiplicative congruency method iv. None of the above. Answer: i c. (2 pts) The hashCode method for a class always returns the number 14. Is this a valid hashCode method for the class? True/False. Answer: True d. (2 pts) Hashing is efficient for a load factor that is less than: i. 99% ii. 90% iii. 80% iv. None of the above. Answer: ii e. (4 pts) Mentioned two properties of a good hash function? Notice we are not asking about the properties of the Java hashCode method. Answer: Any two valid ones. Possible ones:

  1. Cheap to compute
  2. Good distribution of values
  3. (16 pts) Dijkstras’ Algorithm Run Dijkstra’s shortest path algorithm on the previous graph using B as the start vertex. Show the entries in the following table after adding the first 3 nodes ( B & 2 other nodes) to the set S of processed nodes (as defined by Djikstra’s algorithm). Keep in mind that after adding a node to the set S you must adjust the cost/predecessor of the appropriate successor nodes. Answer: A B C D E F A F D B E C

LowestCost 3 0 ∞ 2 5 5 Predecessor D none none B A B

  1. (10 pts) GUIs Consider the following Java Swing code that uses a JSlider object. A JSlider object let’s the user graphically select a value by sliding a knob. The following is a snapshot of a slider: The stateChanged method is called when we move the knob. public class JSliderPanel extends JPanel { private JSlider jSlider; public JSliderPanel() { add(jSlider= new JSlider(JSlider. HORIZONTAL )); jSlider.addChangeListener( new Handler()); } class Handler implements ChangeListener { public void stateChanged(ChangeEvent e) { System. out .println("Slider action"); } } } a. (2 pts) In the above code, which class represents the view? Answer: JSliderPanel b. (2 pts) In the above code, which class represents the controller? Answer: Handler c. (6 pts) Write a new JSliderPanel constructor that uses an anonymous inner class to implement the same ChangeListener for the JSlider. Answer: (Code that should appear in // YOUR CODE HERE)

jSlider.addChangeListener( new ChangeListener() {

public void stateChanged(ChangeEvent e) {

System. out .println("Slider action");

public class JSliderPanel extends JPanel {

private JSlider jSlider;

public JSliderPanel() {

add(jSlider=new JSlider(JSlider. HORIZONTAL ));

// YOUR CODE HERE

  1. (36 pts) Binary Trees a. (2 pts) We generate a balanced binary search tree when elements are inserted in sorted order. True/False

if (currRoot != null) {

if (currRoot.left == null && currRoot.right == null)

return 1;

else if (currRoot.left == null)

return numberOfLeavesAux(currRoot.right);

else if (currRoot.right == null)

return numberOfLeavesAux(currRoot.left);

else

return numberOfLeavesAux(currRoot.left) +

numberOfLeavesAux(currRoot.right);

return 0;

iv. (14 pts) decreasingOrder – Implement a method named decreasingOrder. It returns an array with the data elements of the tree in decreasing order.

public ArrayList decreasingOrder() {

ArrayList result = new ArrayList();

decreasingOrderAux(root, result);

return result;

private void decreasingOrderAux(Node currRoot,

ArrayList result) {

if (currRoot != null) {

if (currRoot.right != null)

decreasingOrderAux(currRoot.right, result);

result.add(currRoot.data);

if (currRoot.left != null)

decreasingOrderAux(currRoot.left, result);

YOU CAN IMPLEMENT THE METHODS ON THE NEXT PAGE