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

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

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

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

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

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

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score 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, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

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

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

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

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

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

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

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, 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. 5:30 PM, 25 April Total Question Name Short Search/ Collections Trees Graphs. Max Score Grader

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

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

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

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

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

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

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

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

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

XC Total Max Score Grader

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

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

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

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

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

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

Largest Online Community of VU Students

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

CS/ENGRD2110: Final Exam

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

COS 226 Algorithms and Data Structures Fall Midterm

Final Examination CSE 100 UCSD (Practice)

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

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

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

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

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Midterm Exam 2 CS 455, Spring 2011

CS 112 Final May 8, 2008 (Lightly edited for 2011 Practice) Name: BU ID: Instructions GOOD LUCK!

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

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

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

Recitation 9. Prelim Review

About this exam review

Prelim 2, CS2110. SOLUTION

Midterm Exam 2 CS 455, Fall 2013

CSL 201 Data Structures Mid-Semester Exam minutes

This examination has 11 pages. Check that you have a complete paper.

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

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

COS 226 Algorithms and Data Structures Fall Final

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Points off Total off Net Score. CS 314 Final Exam Fall 2016

CMSC132, Practice Questions

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS 307 Final Spring 2009

Points off Total off Net Score. CS 314 Final Exam Spring 2017

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

Computer Science E-22 Practice Final Exam

Lecture Notes on Tries

ECE 250 Algorithms and Data Structures

Second Examination Solution

CSE 331 Final Exam 12/9/13

Midterm Exam 2 CS 455, Spring 2015

Lecture Notes on Tries

Comp Intermediate Programming EXAM #2 March 30, 2005 Rice University - Instructors: Cox & Nguyen

COS 226 Algorithms and Data Structures Spring Midterm

COS 226 Midterm Exam, Spring 2009

Midterm II Exam Principles of Imperative Computation Frank Pfenning. March 31, 2011

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

COS 226 Algorithms and Data Structures Fall Midterm

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

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

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

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

Question Paper Code : 97044

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Points off Total off Net Score. CS 314 Final Exam Spring 2016

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

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CSE wi Midterm Exam 2/8/18. Name UW ID #

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

Transcription:

Final exam CS 2110, December 15, 2016, 9:00AM Question Short Trie Loop Exp Rec Gen Span Mon BigO Data Total Max 20 7 9 10 9 9 8 10 10 8 100 Score Grader The exam is closed book and closed notes. Do not begin until instructed. You have 150 minutes. Good luck! Write your name and Cornell NetID at the top of every page! There are 10 questions on 14 numbered pages, front and back. Check that you have all the pages. When you hand in your exam, make sure your pages are still stapled together. If not, please use our stapler to reattach all your pages! We have scrap paper available. If you do a lot of crossing out and rewriting, you might want to write code on scrap paper first and then copy it to the exam 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 provided. 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. Academic Integrity Statement: I pledge that I have neither given nor received any unauthorized aid on this exam. (signature) 1 of 14

1. Short Questions [20 pts] (a) [3 pts] Hash tables. Consider hashing using linear probing. Write the definition of load factor. (b) [4 pts] Exceptions. Consider the method given below. Suppose the try-block throws an ArithmeticException, so the catch-block is executed. Write down how execution proceeds in two cases (1) The catch-block throws another ArithmeticException and (2) the catch-block does not throw another exception. public void m() { try {... catch (ArithmeticException e) {...... (c) [3 pts] Local variables. Consider method m declared below. To the right of the method, write down when during execution of the call m(5) space is allocated for variable x. public static int m(int p) { p= Math.abs(p); while (p > 0) { int x= 2 p; 2 of 14 return x;

(d) [3 pts] Function equals. Let C be a class. The fields of an object of class C are not changed except by the constructor. Describe a situation in which the following expression is false. (new C()).equals(new C()) (e) [3 pts] Generics. Give an example of what could go wrong if Java allowed LinkedList<Integer> to be a subtype of LinkedList<Object> (f) [4 pts] Constructors. Write the constructor that Java inserts in class C if one is not declared in C. Write the statement that Java inserts as the first statement of a constructor if the first statement is not a call on another constructor. 3 of 14

2. Tries vs BSTs [7 pts] The trie 1 data structure is a tree that maintains a sorted set of strings. Each edge in a trie is labeled with a single character. To look up a string, begin at the root and follow the path of characters in the string. Each node contains a boolean value to indicate whether the path from the root to that node represents a complete string that is in the set, and not just a partial prefix. This allows a word that is a substring of another to be in the set, for example, in and inn shown below. Here, the black nodes represent words in the set {"in", "inn", "tea", "ten", "to". i t n e o n a n Assume that words can contain only the 26 letters a..z. Assume that the child of a node corresponding to a letter can be referenced in constant time (the child is either a node or null, in case the child is empty). (a) [4 pts] Complexity of tries. Consider a trie with n strings of maximum length m. What is the tightest Big-O worst-case complexity (in terms of n and m) of determining whether the trie contains a given word? (b) [3 pts] Complexity of a BST containing strings. Assume strings use only the characters in a..z, as in the previous question. Consider a balanced BST containing n strings of maximum length m. What is the tightest Big-O worst-case complexity (in terms of n and m) of determining whether the BST contains a given word? 1 from retrieval, a pun on tree 4 of 14

3. Loops [9 pts] Given is n > 0 and array segment b[0..n], which contains a sorted list of integers, possibly containing duplicates. We want to place the set of integers in b at the beginning of b; the rest of b[0..n] remains unchanged. For example, below is one possibility for b[0..n], with 5 distinct values, and below it is the result of the algorithm. At the end, variable k will contain the size of the set. [2, 5, 5, 5, 7, 7, 8, 8, 9] [2, 5, 7, 8, 9, 7, 8, 8, 9] k = 5 ------------- ---------- set unchanged Here are the precondition, postcondition, and an invariant derived from them, using x[0..n] to denote the original list of values in b[0..n]. dups stands for duplicates. Pre Q: Post R: Inv P: b b b 0 n x[0..n], which is sorted 0 k n x[0..n] with no dups x[k..n] 0 k h n x[0..h-1] with no dups x[k..h-1] x[h..n] (a) [2 pts] Initialization To the right, write the initialization that truthifies the invariant. (b) [2 pts] Termination To the right, write the condition B such that B and P imply R. Do not write while and things like that. Just write the condition. (c) [1 pt] Progress To the right, write a statement that makes progress toward termination. (d) [4 pts] Maintain invariant To the right, write a statement that, when executed before the statement written in c), will ensure that the invariant is true after the statement in (c) is executed. 5 of 14

4. Expressions [10 pts] To represent expressions, you have built the classes listed on page 7, which you may remove from the exam booklet. For simplicity, we are not representing expressions. (a) [3 pts]! Write a Java expression using these classes that represents this tree: && Expr e = new && true true false (b) [7 pts] Write a visitor class that, when applied, returns a String containing a preorder traversal of an expression. For example, e.apply(new PreorderVisitor()) should return &&! T && T F (where e is the expression from part (a)). Hint: making the code below compile will get you started. public class PreorderVisitor implements ExprVisitor<String> { 6 of 14

/ Represents a boolean expression. / interface Expr { / apply the visitor to this object and return what it returns. / <R> R apply (ExprVisitor<R> visitor); / Represents an operation to be performed on an Expr @param R is the return type of the operation / interface ExprVisitor<R> { public R visitand(and a); public R visitnot(not e); public R visitconstant(constant c); / Represents the expression left && right / class And implements Expr { public Expr left, right; public And(Expr left, Expr right) { this.left = left; this.right = right; public @Override <R> R apply(exprvisitor<r> visitor) { return visitor.visitand(this); / Represents the expression! child / class Not implements Expr { public Expr child; public Not(Expr child) { this.child = child; public @Override <R> R apply(exprvisitor<r> visitor) { return visitor.visitnot(this); / Represents the expression T or F / class Constant implements Expr { public boolean value; // true for T ; false for F public Constant(boolean value) { this.value = value; public @Override <R> R apply(exprvisitor<r> visitor) { return visitor.visitconstant(this); 7 of 14

This page intentionally left blank 8 of 14

5. Recursion [9 pts] Consider a binary search tree whose values are integers and whose nodes are objects of class BST: public class BST { private int value; // Value in this node private BST left; // Left child, or null if none private BST right; // Right child, or null if none // additional code for methods add, lookup, etc. (a) [3 pts] Complete function size: / Return the number of nodes in t (0 if t is null). / public static int size(bst t) { (b) [6 pts] Complete function numbigger, doing only as much computation as necessary: / Return the number of values in t that are greater than v (0 if t is null). / public static int numbigger(bst t, int v) { 9 of 14

6. Generics [9 pts] Consider the following class hierarchy: interface Flier { void fly(); abstract class Animal { void getweight() {... abstract String getname(); abstract class Bird extends Animal implements Flier { interface List<E> { E get(int i); void add(e e); int size(); class Mammal extends Animal {... class Dog extends Mammal {... class Parrot extends Bird {... (a) [2 pts] What methods must Parrot implement? (b) [1 pt] What methods must Dog implement? (c) [3 pts] What is the most general type for flock in the following method (i.e. the one that will allow it to be called on as many different types as possible but still compile). public void migrate(list< > flock) { for (int i = 0; i < flock.size(); i++) flock.get(i).fly(); (d) [3 pts] What is the most general type for herd in the following method? public void populate(list< > herd) { herd.add(new Mammal()); 10 of 14

7. Graphs [8 pts] (a) [4 pts] Execute Kruskal s algorithm on the following graph. Fill in the edges as you select them and number them: write 1 next to the first edge you select, 2 next to the second, and so on. If the algorithm requires a starting node, use a. 12 b 11 a 10 c 6 3 1 4 d 8 e 2 5 f 7 9 h g (b) [4 pts] Similarly, execute Prim s algorithm on the following graph. If the algorithm requires a starting node, use a. 12 b 11 a 10 c 6 3 1 4 d 8 e 2 5 f 7 9 h g 11 of 14

8. Monitors [10 pts] Complete the following implementation of a Monitor, which maintains two counters: / An XYMonitor maintains two counters, x and y, and ensures that x < y. / class XYMonitor { / FILL IN / private int x, y; / Constructor: an instance with counters x and y. Precondition: x < y / public XYMonitor(int x, int y) { this.x = x; this.y = y; / Block until it is safe to increment counter x, then increment x / public synchronized void incrementx() throws InterruptedException { // FILL IN / Decrement counter x. / public synchronized void decrementx() { // FILL IN / Increment counter y. / public synchronized void incrementy() { // FILL IN / Block until it is safe to decrement counter y, then decrement y. / public synchronized void decrementy() throws InterruptedException { // FILL IN 12 of 14

9. Complexity [10 pts] Consider the worst-case execution of the algorithm below on a graph with N vertices and E edges. (a) [6 pts] Next to each line, write an asymptotic bound for the total time it takes to execute that line, assuming first that G is stored as an adjacency list and second that it is stored as an adjacency matrix. Write your answers in terms of E and N. Assume that each line is implemented as efficiently as is possible. The first two lines are completed for you. total asymptotic run time (adj. list) (adj. matrix) O(N) O(N) for each vertex v O(N) O(N) print v copy the vertices into an array b sort b for each entry u in the array print u for each neighbor v of u find the index i of v in b print i (b) [2 pts] What is the total asymptotic running time for each? Adjacency list: Adjacency matrix: (c) [2 pts] Based on your answer to (b), which representation will cause the algorithm to run faster on a particular graph with 100 vertices and 8000 edges? i. The adjacency matrix representation will run faster ii. The adjacency list representation will run faster iii. There is not enough information to answer Explain your answer in 1-2 sentences. 13 of 14

10. Data structures [8 pts] Identify which of the following data structures A. Adjacency list B. Adjacency matrix C. Min heap D. Singly linked list E. Doubly linked list F. Array G. Hash table H. (Balanced) binary search tree is best for storing the following kinds of data: a. A contact list for looking up your friends phone numbers b. Players waiting for their turn to start a game, who may get tired and leave c. Reminders that are displayed at a specified time d. Print jobs that are printed in the order they are submitted e. Cached results from a function that computes the Fibonacci numbers f. A contact list for displaying the name of someone who is calling you g. Web pages and the links between them h. Permissions for determining whether two users are allowed to speak with each other Note: each data structure is matched with exactly one scenario. 14 of 14