Final Exam. CS2110 Spring May 12, Total. Max Score

Similar documents
Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 SOLUTION. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Prelim 2. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Total Score /1 /20 /41 /15 /23 Grader

Prelim 2. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Prelim 2 Solution. CS 2110, 24 April 2018, 5:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

Prelim 2 Solution. CS 2110, 24 April 2018, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph.

Prelim 2, CS :30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2 SOLUTION. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs

Prelim 2. CS 2110, 5:30 PM, November 17, 2016

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Final exam. CS 2110, December 15, 2016, 9:00AM

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Total Score /15 /20 /30 /10 /5 /20 Grader

Total Score /15 /20 /30 /10 /5 /20 Grader

Prelim 2, CS2110. SOLUTION

ext Total Score /20 /20 /15 /20 /25 /5 Grader

Computer Science E-22 Practice Final Exam

CS/ENGRD2110: Final Exam

CS/ENGRD2110: Prelim 2 SOLUTION

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

XC Total Max Score Grader

CS-301 Data Structure. Tariq Hanif

Data Structures Brett Bernstein

DIT960 Datastrukturer

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

Prelim 1. CS 2110, March 15, 2016, 7:30 PM Total Question Name True False. Short Answer

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Recitation 9. Prelim Review

CSE 373 Final Exam 3/14/06 Sample Solution

n 2 ( ) ( ) + n is in Θ n logn

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

( ) n 3. n 2 ( ) D. Ο

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

CMSC132, Practice Questions

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

Prelim 1. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

ext Total Score /20 /20 /20 /20 /20 /5 Grader

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Prelim 1. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

CSCE 2014 Final Exam Spring Version A

CSE332 Summer 2012 Final Exam, August 15, 2012

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

Course Review for Finals. Cpt S 223 Fall 2008

Prelim 1, Solution. CS 2110, 13 March 2018, 7:30 PM Total Question Name Short answer

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

CSE 332 Autumn 2016 Final Exam (closed book, closed notes, no calculators)

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

CS61BL. Lecture 5: Graphs Sorting

1. AVL Trees (10 Points)

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

Final Examination CSE 100 UCSD (Practice)

CSE 332 Winter 2015: Final Exam (closed book, closed notes, no calculators)

Computer Science 136 Exam 2

Exam Data structures DIT960/DAT036

COS 226 Midterm Exam, Spring 2009

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Prelim 1. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer. OO Recursion Loop invariants Max Score Grader

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CS171 Midterm Exam. October 29, Name:

COS 226 Algorithms and Data Structures Fall Midterm

COS 226 Algorithms and Data Structures Spring Midterm

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

CSE 373 Spring 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

Transcription:

Final Exam CS2110 Spring 2014 May 12, 2014 1 2 4 5 6 7 Total Question True Short Searching/ Trees/ Graphs Threads Total False Questions Sorting Heap Max 20 19 15 15 20 11 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed. You have 150 minutes. Good luck! Start by writing your name and Cornell netid on top! There are 5 questions on 13 numbered pages, front and back. Check now that you have all the pages. When you hand in your exam, make sure your booklet is still stapled together. If not, please use our stapler to reattach all your pages! We have scrap paper available, so you if you are the kind of programmer who does a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam, just so that we can make sense of what you handed in! Write your answers in the space provided. Ambiguous answers will be considered incorrect. You should be able to fit your answers easily into the space we provided, so if something seems to need more space, you might want to skip that question, then come back to your answer and see if you really have it right. In some places, we have abbreviated or condensed code to reduce the number of pages that must be printed for the exam. In others, code has been obfuscated to make the problem more difficult. This does not mean that itâăźs good style. 1 of 13

1. True / False [20 points] a) T F Consider hashing, as presented in recitation. If a value hashes to index k, quadratic probing (instead of linear probing) means looking at the values at indices k, 2k, 3k, 4k... (all mod the table size, of course). b) T F The maximum number of threads cannot exceed the number of processors available on a computer. c) T F Dijkstra s algorithm is guaranteed to find the shortest path from the start node to every reachable node in the graph. d) T F Suppose class B is a subclass of class C. After the assignment B ob = new B();, ob instance of C is false. e) T F Even though Java has priorities for threads, a high-priority thread may get stuck waiting for a low-priority thread. f) T F The worst case running time of Quicksort is O(n 2 ). g) T F Java has three kinds of variable: the field (instance variable), the parameter, and the local variable. h) T F Given the definition of the median of a population as the value that is in the middle when the population is sorted, we conclude that in a full and complete BST, the value at the root is the median. i) T F The following data structure is a Directed-Acyclic Graph (DAG). j) T F In a quantum computer, when the output is observed, the wave function collapses to a classical state. 2 of 13

2. Short Questions [15 points] 2.a Parsing (4 points) Below is a simple context-free grammar (ɛ denotes the empty string): S BS ɛ B αbβ Bβ αβ Which of the following strings are legal candidates for S in this grammar? Put Yes in the brackets if it is legal, and put No if it is not. [ ] ααββ [ ] α [ ] ββα [ ] αββαβαααβββ 2.b Graphs (5 points) A simplified automobile manufacturing process has the following subtasks, which may depend on one another. A: Parts Purchase B: Pressing Process C: Welding Process D: Tire Production E: Body Manufacturing F: Final Assembly The dependence relations are shown below: Final Assembly depends on Parts Purchase, Tire Production, and Body Manufacturing. Body Manufacturing depends on Pressing Process and Welding Process Tire Production depends on Parts Purchase and Pressing Process Draw a Directed Acyclic Graph (DAG) to represent these dependence relations, where an edge (u, v) indicates v depends on u. 3 of 13

2.c GUIs (6 points) Look at the following GUI constructor: public class GUI implements ActionListener { private final JFrame frame= new JFrame("Turing Award Winners"); private final JTextArea textarea= new JTextArea(); private JButton button = new JButton("2012-2013"); public GUI() { button.addactionlistener(this); frame.add(button, BorderLayout.NORTH); frame.add(textarea,borderlayout.center); textarea.setpreferredsize(new Dimension(400,200)); frame.pack(); frame.setvisible(true); /** Called when button is pressed. */ public void actionperformed(actionevent e) { frame.settitle("turing Award Winners: 2012-2013"); textarea.settext("2013: Leslie Lamport\n"); textarea.append("2012: Silvio Micali, Shafi Goldwasser"); frame.pack(); (a) Draw the presented object when the constructor shown above is executed. 4 of 13

(b) Draw the object after the button 2012-2013 is clicked. 2.d Invariant (4 points) The following code is to store in s the sum of b[0..n]. Complete the code. You must use the given loop invariant. int k= ; s= 0; // invariant: -1 <= k <= n && // s is the sum of b[k+1..n] while ( ) { s= ; k= ; 5 of 13

3. Searching / Sorting [15 points] 3.a Search (2 points) a) What is the complexity of binary search on an array of size n: b) For a linked list, the search time can be improved by keeping the list sorted. What is the time complexity of insertion to a sorted linked list in terms of the number of nodes n? 3.b Insertion Sort (1 point) Write the invariant for insertionsort method. /** Sort b[h..k] --put its elements in ascending order. */ public static void insertionsort(comparable[] b, int h, int k) { // Invariant:... 3.c Selection Sort (6 points) An Integer array b holds values in the following order: 5, 19, 13, 3, 9, 11, 7, 17, 2 Complete the following table such that every row contains the state of b after the currently selected finds its place. (Initial) 5 19 13 3 9 11 7 17 2 (Step1) (Step2) (Step3) (Step4) (Step5) (Step6) (Step7) (Step8) 6 of 13

3.d Merge Sort (6 points) The code for merge sort is given as follows: /** Sort b[h..k] --put its elements in ascending order. */ public static void mergesort(comparable[] b, int h, int k) { if (h >= k) return; int e= (h+k)/2; mergesort(b, h, e); // Sort b[h..e] mergesort(b, e+1, k); // Sort b[e+1..k] merge(b, h, e, k); // Merge the 2 segments a) An Integer array b holds values in the following order: 5, 19, 13, 3, 9, 11, 7, 17, 2 If we call mergesort(b, 0, b.length-1), how many calls to method merge will be made in total? b) Let s say the answer for part (a) is n. Complete the following table such that it represents the state of b just after the last 4 returning merge calls. Step(n-3) Step(n-2) Step(n-1) Step(n-0) 7 of 13

4. Heap [15 points] 4.a Insertion (5 points) Draw the heap that results from inserting the following sequence of integers, in the order given, into an empty min-heap. (Hint: Your answer should be a binary tree.) 10, 5, 7, 2, 1, 30, 15, 9 It will help us give partial credit if you show the heap after each insertion. 8 of 13

4.b Extraction (10 points) Implement the bubbledown function according to the specification below. public class MinHeap { int size; // The heap is in array[0..size-1], stored as int[] array; // described in the CS2110 class lectures/notes /** Remove and return the smallest element (throws an exception if heap is empty) */ public int extract() { if (size == 0) throw new RuntimeException("Heap is empty"); int temp= array[0]; array[0] = array[size-1]; size--; bubbledown(0); return temp; /** Bubble node k down to its heap position. * It can be implemented either recursively * or iteratively */ private void bubbledown(int k) { 9 of 13

5. Graphs [20 points] 5.a Minimum spanning tree (10 points) NYSEG has just constructed a new power plant in upstate New York, and needs your help determining how to connect it to the many small towns in the region. New power lines must be constructed to connect the towns local grids to the power plant, but some regions are more difficult to build power lines through than others. Your job is to determine the least expensive way to connect all the towns into a network that includes the power plant. The graph below shows the towns that need to be connected and the possible paths between them, along with the planning committee s estimate of the budget required to build power lines along each of those paths. The number annotating an edge represents the cost, in thousands of dollars, of building a high-tension power line along that edge. In order to find the lowest-cost set of power lines to build, you need to find the minimum spanning tree of this graph. Starting at the node labeled Power Plant, use Prim s algorithm to construct the minimum spanning tree of this graph. Show the order in which you add edges to the MST by labeling them with the letters A-Z, where A is the first edge you add, and leaving unlabeled edges you never add. Then circle the subset of the graph that is in the final MST. Trumansburg 25 Lansing 2 8 5 4 Dryden 25 Ithaca 11 12 9 7 Horseheads 5 Power Plant 10 Richford 2 7 4 11 Elmira 9 Candor 10 of 13

5.b Depth First Search (10 points) Fill in the method skeleton below to write the Depth-First Search algorithm. /** An instance is a node of a graph */ public class GraphNode { public boolean visited = false; //true iff this node has been visited during dfs public GraphNode[] neighbors; //neighbors of this node in the graph /* Other fields and methods omitted for brevity */ /** This node is unvisited. Visit all nodes reachable from this node (including * this node) along paths of unvisited nodes that start at this node. */ public void DFS() { 11 of 13

6. Threads [15 points] A growing online retailer uses a queue implementation as a linked list to process orders from customers. The requests coming to the server are dispatched to multiple threads which manipulate the queue. Some part of the code is presented below. public class OrdersQueue { // The orders are maintained in a singly linked list of objects of class OrderNode. // head points to the first order and tail to the last order. // head and tail are null if the queue is empty. private OrderNode head, tail; /** Append order to the queue */ public void add(order order) { if(tail!= null) { tail.succ= new OrderNode(order, null); tail= tail.succ; else { tail= new OrderNode(order, null); head= tail; /** If the queue is empty return null. Otherwise, remove the first order in the queue and return it */ public Order poll() { OrderNode first= head; if(head!= null) { head= head.succ; if(head == null) tail= null; return first.getorder(); else return null; /** An instance is a node of the list */ private class OrderNode { private Order order; // Information stored in the node. private OrderNode succ; // Successor of this node on the list. /** Constructor: an instance with successor s (s can be null), * and order information o. */ private OrderNode(Order o, OrderNode s) { order= o; succ= s;...... 12 of 13

a) Define what a race condition is. b) What might cause a race condition in OrdersQueue implementation? Suggest two solutions. 13 of 13