final exam - university of texas at...

20
1 2 3 4 5 6 7 8 9 10 11 12 13 Exam 2 EE 422C - University of Texas at Austin - Spring 2015 Name ___________________________ EID __________________ Lab day/time_____________ Test taking instructions. No calculators, laptops or other assisting devices are allowed. Write your answers on these sheets. Wherever code is required, write JAVA statements in the blank areas provided, or by modifying the given code in place. You are not required to follow the coding style guidelines when writing code on the exam, but be as neat as possible. If you are unsure of the meaning of a specific test question, then write down your assumptions and proceed to answer the question on that basis. If you see a typo or syntax error, fix it, circle it, and if you are right you will get a bonus point for each one fixed. In your programming solutions on this test you may use any of the classes/methods that you know from the library, or JCF. Questions about the exam questions will not be answered during the test. For the binary tree related questions on this exam you may assume that this generic class has been defined for your use and is included. When the term Btnode is used on the exam it is referring to this generic class (e.g. in Question 5). class Btnode<T> { /* this generic class models a node of a binary tree */ /* here are the private data members of the binary tree node */ private T element; private Btnode<T> left; private Btnode<T> right; private Btnode<T> parent; /* the constructor given an element,left child,right child & parent */ Btnode(T theElement, Btnode<T> lt, Btnode<T> rt, Btnode<T> pt ) { element = theElement; left = lt;

Upload: duonghanh

Post on 05-Apr-2018

221 views

Category:

Documents


4 download

TRANSCRIPT

1 2 3 4 5 6 7 8 9 10 11 12 13

Exam 2EE 422C - University of Texas at Austin - Spring 2015

Name ___________________________ EID __________________ Lab day/time_____________

Test taking instructions. No calculators, laptops or other assisting devices are allowed. Write your answers on these sheets. Wherever code is required, write JAVA statements in the blank areas provided, or by modifying the given code in place. You are not required to follow the coding style guidelines when writing code on the exam, but be as neat as possible. If you are unsure of the meaning of a specific test question, then write down your assumptions and proceed to answer the question on that basis. If you see a typo or syntax error, fix it, circle it, and if you are right you will get a bonus point for each one fixed. In your programming solutions on this test you may use any of the classes/methods that you know from the library, or JCF.

Questions about the exam questions will not be answered during the test.

For the binary tree related questions on this exam you may assume that this generic class has been defined for your use and is included. When the term Btnode is used on the exam it is referring to this generic class (e.g. in Question 5).

class Btnode<T>{ /* this generic class models a node of a binary tree */ /* here are the private data members of the binary tree node */

private T element; private Btnode<T> left; private Btnode<T> right; private Btnode<T> parent;

/* the constructor given an element,left child,right child & parent */ Btnode(T theElement, Btnode<T> lt, Btnode<T> rt, Btnode<T> pt ) { element = theElement; left = lt; right = rt; parent = pt; }

/* here are the public get/set methods for the data members */ public T getElement( ){ return element;} public Btnode<T> getLeft( ) {return left;} public Btnode<T> getRight( ){return right;} public Btnode<T> getParent( ){return parent;} public void setElement( T x ){element = x;} public void setLeft( Btnode<T> t ) {left = t;} public void setRight( Btnode<T> t ){right = t;} public void setParent( Btnode<T> t ){parent = t;}

}

Page 2Question 1. Terminology. [22 pts.] Answer each of the following questions by choosing the best answer from the list below.

A. ____A search algorithm that is O(log log n) for an ordered array B. ____A data structure used to convert an infix expression to postfix notation C. ____A sorting technique that requires NO comparisons D. ____A systematic approach to trial and error solution searching E. ____A data structure that has the same data structure as a component F. ____A collection of nodes with each one linked to its parent, and a single root on top G. ____A binary tree that represents variable length character codes for compression H. ____The average case search time in a Binary Search Tree (BST) I. ____The BST traversal that produces an ordered sequence J. ____A BST is best when inserts are at the leaves and there are not too many internal of these operations. K. ____On average log n + .25 comparisons are needed to insert into this type of BST. L. ____A data structure developed to store indexes into databases on disk M. ____A best sorting method in terms of worst case number of comparisons and minimum space utilization N. ____A control abstraction that splits a process into multiple parallel processes O. ____The synchronization problem that occurs when 2 threads want to use 2 resources, but each thread has 1

resource locked and needs the other resource P. ____A synchronized data structure that implements a queue that will wait if needed to perform enqueue and

dequeue operations Q. ____A directed graph that contains no cycles R. ____The inventor of the shortest path algorithm for a weighted digraph S. ____The inventor of the minimum spanning tree algorithm for a weighted graphT. ____ Where the classes for drawing basic graphical shapes are foundU. ____ For GUI’s the screen and various windows are made up of these individual elementsV. ____ The language used to render web pages within a browser window

a. ArrayListb. ArrayBlockingQueuec. AVLd. AWTe. backtrackingf. B-Treeg. DAGh. deadlocki. deletesj. dequek. Dijkstral. duplicatesm. expression treen. fork

o. hashing functionp. heapsortq. HTMLr. Huffmans. infinite loopt. inorderu. interpolationv. LinkedListw. LISPx. Listy. load factorz. makeHeapaa. mergesortbb. Newtoncc. O(log n)

dd. O(n)ee. pixelsff. Priority Queuegg. Primhh. putii. race conditionjj. radixkk. recursivell. Stackmm. topological sortnn. treeoo. TreeSet

Page 3Question 2 - Multiple Choice. [2 pts. each - 8 pts total] For each of the following subparts, circle the best or all correct answers as indicated.

A. Which of the following sorting algorithms have a average-case performance that is better than O(n2)?i) Selection sortii) Bubble sortiii) Shell sortiv) Merge sortv) Heap sortvi) Quick sortvii) Radix (bin) sort

B. Given three arrays with L, M and N elements respectively, estimate the running time for the following algorithm in terms of the number of times that the do something step is executed:

repeat the following for items i from 1 to N for array1 repeat the following for items j from 1 to M for array2

repeat the following for items k from 1 to L for array3 do something

end repeat end repeat

end repeat

i. O (L+M+N)ii. O ( N 3 ) iii. O ( L * M * N )iv. None of the above

C. Which of the following statements are true of heaps?I. It is a binary tree

II. In terms of its shape, it must be completeIII. It can be either a maximal or a minimal heapIV. The value of the root has no relationship to the value of any other nodesV. Is useful for implementing a stack

VI. Is the most efficient representation for a priority queue

D. A method uses the selection sort algorithm to sort an array of doubles. The method takes 5 seconds to complete given an array with 10,000 distinct elements in random order. What is the expected time in seconds for the method to complete given an array with 30,000 distinct elements in random order?

I. 10II. 25

III. 100IV. 20V. None of the above

Page 4

Question 3. Recursion (6 points) A. [4 pts] Write a recursive algorithm (not code) that determines whether a specified target character is present within a string. It should return true if the target is found and false if not. The stopping conditions should be:

A string reference to null or a string of length 0, the result is false The first character in the string matches the target, the result is true

The recursive step would involve searching the rest of the string for the target.

B. [2 pts] What is returned by the method call a(2) ? _________

public int a(int x) { if(x > 20) return x; else return x + a(x * x + 1);}

Question 4 [4 Pts] You have an array with 1,000,000 distinct elements in random order.

You have to search the array 100 times to determine if a given element is present or not.What will result in less work? Sorting the array with quicksort and then doing the searches using binary search OR just doing the searches with linear search. (without sorting)

Justify your answer with calculations. Tip: log2 (1,000,000) ~= 20

Page 5Question 5. Tree questions (10 pts)

A. (2 pts) Draw the binary tree created by the following statements. Btnode is defined on page 1.

Btnode<Integer> root, a, b, c, d;d = new Btnode<Integer> (6, NULL, NULL, NULL);c = new Btnode<Integer> (9, NULL, d, NULL);b = new Btnode<Integer> (4, NULL, c, NULL);a = new Btnode<Integer> (12, NULL, NULL, NULL);root = new Btnode<Integer> (10, a, b, NULL);

B. (3pts) Fill in the statements in the method below to perform an inorder traversal of a binary tree like the above. Print out the value of the node’s element as it is visited inorder.

public static void inorder( Btnode<Integer> tree ){ if (tree == NULL) return; else {

}}

C. (5 pts) Consider the following binary search tree. Use the original tree below when answering each subpart (a) through (e).

(a) If the value 54 is inserted into this tree, which node becomes its parent? __________________(b) If the value 27 is inserted into this tree, which node becomes its parent? ___________________(c) If we delete node 70, which node should be its replacement node? ___________________(d) If we delete node 25, which node should be its replacement node? __________________(e) If we delete the root node 50, which node should be selected as its replacement node so that the fewest number of

changes are made to the tree? ______________________

Page 6

Question 6. Heaps and Graphs (16 pts)

A. (3 pts) A heap can be represented by an ArrayList with the values stored in top down, level-by-level, left to right order. Start with the following maximum heap and list the elements after each operation. Execute the operations sequentially, using the result of the previous operation. The initial ArrayList values for this heap are {50, 35, 15, 12, 3, 5}.

(a) Insert 17: The values in the ArrayList are now {____________________________________}.

(b) Delete (pop) an element from the heap: The values are now {_________________________________}.

B.(3 pts) Start with following tree and use the heapify algorithm to convert it into a maximum heap. Draw the resulting tree.

C. (2 pts) Show the minimum cost path (and the total cost) from node A to node E in the following digraph G.

D.(3 pts) Draw the adjacency list representation for the following digraph G.

Page 7

E. (5 pts) Draw the minimal spanning tree for the following graph G. What is the total cost?

Question 7 (4 pts) AVL Treesa. [2 pts] Show that the below tree is indeed an AVL tree by writing in the balance indicators for each node of the tree (hint: balance indicator = hr – hl )

b. [2 pts] Now insert the word “keen” into the above tree and rebalance the tree as necessary so that it remains an AVL tree. Draw the new tree below.

WCX

KD

Page 8Question 8 (5 Pts) For the graph shown below determine the visit sequence for a breadth-first search beginning at node 3. Show the changing queue contents for partial credit.

Visit sequence = _____________________________________________________________________________________

Question 9. [5 Pts] Given the Huffman code tree below, what does the given bit stream decode to

Given bit stream (spaces added for clarity): 1 1 0 1 0 0 0 0 1 0 0 1 1 0

Page 9

Question 10 [6 Pts].For the following digraph, use Diykstra’s algorithm to show the minimum cost path from node A to all other nodes. I have provided the initial table for A to all other vertices (v), and the initial d[v] and p[v] pairs. Recall that d[v] is the distance/cost of getting from A to v, and p[v] is the immediate predecessor node of v on the path from A to v. You will then derive the final table of d[v] and p[v] pairs which show the minimum cost paths and the immediate predecessor nodes. Show your intermediate work for getting from the initial table to the final table for partial credit.

Initial Tablev d[v] p[v]B 10 AC ∞ AD ∞ AE 40 A

Final Table

Page 10v d[v] p[v]BCDE

Page 11

Page 12Question 11 [5 pts] Consider the following applet with an embedded thread. Describe the order in which ALL the various methods are executed by the JVM.

import java.awt.*;import java.applet.*;/* <applet code="Banner" width=300 height=50> </applet> */

public class Banner extends Applet implements Runnable { String msg = " A Moving Banner."; Thread t = null; int state; boolean stopFlag;

public void init() { setBackground(Color.cyan); setForeground(Color.red); }

public void start() { t = new Thread(this); stopFlag = false; t.start(); }

public void run() { // Display banner while (true) { try { repaint(); Thread.sleep(250); if(stopFlag) break; } catch(InterruptedException e) {} } }

public void stop() { stopFlag = true; t = null; }

public void paint(Graphics g) { char ch; ch = msg.charAt(0); msg = msg.substring(1, msg.length()); msg = msg + ch; g.drawString(msg, 50, 30); }}

Page 13Question 12 [5 Pts] Consider the following mouse event handling code. Add in the code necessary to draw a small square at the position where the mouse was clicked in the applet window. Also describe in words the purpose of the highlighted code statements.

import java.applet.Applet;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.event.MouseListener;import java.awt.geom.*;

public class MouseSpyApplet extends Applet{

public MouseSpyApplet() { MouseSpy listener = new MouseSpy(); addMouseListener(listener); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // fill in the rest of this method

}

class MouseSpy implements MouseListener { public void mouseClicked(MouseEvent e) {System.out.println ("Mouse clicked. x=" + e.getX() + " y=" + e.getY());

} // assume empty bodies for other 4 mouse listener methods }}

Question 13. Multi-threading basics (4 pts)For the design of your assignment 6 ticket office program, answer the following questions.

a. (2 pts) What constitutes a race condition? How can/did you avoid that in the current assignment?

b. (2 pts) What constitutes a deadlock situation? How can/did you avoid that in the current assignment?