University of Maryland College Park Dept of Computer Science

Similar documents
University of Maryland College Park Dept of Computer Science

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2016 Midterm I

CMSC132, Practice Questions

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

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

CSE 247 Data Structures and Algorithms Fall Exam I

CS171 Midterm Exam. October 29, Name:

COMP 103 : Test. 2018, Sept 12//(Corrected) ** WITH SOLUTIONS

Collections, Maps and Generics

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

CSC Java Programming, Fall Java Data Types and Control Constructs

1.00/ Introduction to Computers and Engineering Problem Solving. Final Exam / December 21, 2005

CS61BL: Data Structures & Programming Methodology Summer 2014

CSE373 Fall 2013, Midterm Examination October 18, 2013

EXAMINATIONS 2016 TRIMESTER 2

COMP 103 : Test. 2018, Sept 12//(Corrected)

EXAMINATIONS 2017 TRIMESTER 2

Model Solutions. COMP 103: Test April, 2013

CSE wi: Practice Midterm

CS61BL: Data Structures & Programming Methodology Summer 2014

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

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

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

COS 226 Algorithms and Data Structures Fall Midterm

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Problem Score Max Score 1 Syntax directed translation & type

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

COMP 103 : Test. 2019, Jan 9 ** WITH SOLUTIONS **

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

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

CS 314 Midterm 2 Fall 2012

CSE373 Winter 2014, Midterm Examination January 29, 2014

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

(the bubble footer is automatically inserted into this space)

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

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

CSE 143 Au03 Final Exam Page 1 of 15

CONTAINS SOLUTIONS!!!

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

Section 05: Midterm Review

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

WITH SOLUTIONS!!! WARNING: THIS COPY CONTAINS SOLUTIONS!!! COMP103 Introduction to Data Structures and Algorithms

Examination Questions Midterm 1

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Midterm Exam 2 CS 455, Spring 2011

COS 226 Algorithms and Data Structures Spring Midterm

Midterm Exam 2 CS 455, Fall 2014

Section 05: Solutions

PIC 20A Collections and Data Structures

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

Csci 102: Sample Exam

COMP 103. Data Structures and Algorithms

Practice exam for CMSC131-04, Fall 2017

CS/ENGRD2110: Final Exam

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2018 TRIMESTER 2 COMP 103 PRACTICE EXAM

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

CS 101 Spring 2006 Final Exam Name: ID:

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

NATIONAL UNIVERSITY OF SINGAPORE

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

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

Computer Science Foundation Exam

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

Prelim CS410, Summer July 1998 Please note: This exam is closed book, closed note. Sit every-other seat. Put your answers in this exam. The or

COS 126 Midterm 1 Written Exam, Fall 2009

CS 314 Exam 2 Spring

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Model Solutions. COMP 103: Test May, 2013

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

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

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

ECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)

CIS 110 Introduction to Computer Programming Summer 2018 Final. Recitation # (e.g., 201):

Tables and Priority Queues

MCS 360 Exam 2 11 November In order to get full credit, you need to show your work.

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Family Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

EXAMINATIONS 2010 END YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

CS 314 Exam 2 Spring 2016

Tables and Priority Queues!

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

CMSC132 Summer 2018 Midterm 1

Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You ll learn much less if you just look at

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Practice Midterm 1 Answer Key

Model Solutions. COMP 103: Mid-term Test. 21st of August, 2014

EXAMINATIONS 2006 SUMMER TRIMESTER. COMP103 Introduction to Data Structures and Algorithms

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

CS 520 Theory and Practice of Software Engineering Fall 2017

Transcription:

University of Maryland College Park Dept of Computer Science CMSC132H Fall 2009 Midterm First Name (PRINT): Last Name (PRINT): University ID: I pledge on my honor that I have not given or received any unauthorized assistance on this examination. Your signature: Instructions This exam is a closed-book and closed-notes exam. Total point value is 100 points, 50 minutes exam. Please use a pencil to complete the exam. PUNT RULE: For any question, you may write PUNT, and you will get ¼ of the points for the question (rounded down). If you feel totally lost on a question, you are encouraged to punt rather than write down an incorrect answer in hopes of getting some partial credit. WRITE NEATLY. If we cannot understand your answer, we will not grade it (i.e., 0 credit). Grader Use Only #1 Algorithmic Complexity (12) #2 Program Correctness (10) #3 Hashing (6) #4 Language Features (22) #5 Recursion (20) #6 Sets and Maps (20) #7 Linear Data Structures (20) Total Total (110) (110) 1

Problem 1 (12 pts) Algorithmic Complexity 1. (6 pts) Calculate the asymptotic complexity of the code snippets below (using big-o notation) with respect to the problem size n. a. for (int i=1; i<=n/2; i++) { f(n) = O( ) System.out.println("Hello"); b. for (i =1; i<=10; i++) { f(n) = O( ) for (t =1; t<= n; t++) { System.out.println("Hello"); c. for (int i=1; i<=n; i=i*2) { f(n) = O( ) for (int k=1; k<=n; k++) { System.out.println("Hello"); 2. (4 points) Give the asymptotic bound of the following functions: a. 6n 3 + n + nlog(n) f(n) = O( ) b. n + n 2 + log(n) f(n) = O( ) 3. (2 pts) List the following big-o expressions in order of asymptotic complexity (lowest complexity first) O(nlog(n)) O(n 2 ) O(2 n ) O(log(n)) O(1) 2

Problem 2 (10 pts) Program Correctness and Exceptions 1. (6 pts) The following code fragment throws a NumberFormatException when the user enters a non-integer value. Modify the following code fragment if the exception is thrown, the code calls JOptionPane.showMessageDialog(null, "That wasn't an integer"); and then displays the input dialog again, until it successfully gets, parses and returns an int value. static int getintfromdialog() { int val = Integer.parseInt( JOptionPane.showInputDialog("Enter an integer")); return val; 2. (2 pts) When is a finally block executed? 3. (2 pts) What does it mean for a piece of code to have 100% code coverage? 3

Problem 3 (6 pts) Hashing 1. (2 pts) Name two properties of a good hash function. 2. (2 pts) Describe the Java hashcode Contract (i.e., what is required of a hashcode() implementation)? 3. (1 pt) What is a collision in a hash table? 4. (1 pt) What is usually returned by the default implementation of the hashcode() method? 4

Problem 4 (22 pts) Java Language Features 1. (2 pts) T or F An inner class can only access public variables and methods of the enclosing class. 2. (2 pts) T or F Java enum values are represented as integers. 3. (2 pts) T or F Java break statements should be avoided in code written for CMSC 132H 4. (2 pts) T or F An abstract class cannot have any constructors. 5. (2 pts) T or F A class extending an abstract class will become abstract if abstract method(s) from the super class are not defined in the subclass. 6. (2 pts) T or F You can pass a ArrayList<String> to a method that expects a List<String> 7. (2 pts) T or F You can pass a List<String> to a method that expects a List<Object> 8. (6 pts) For each of the 6 table cells, state true or false as to whether the operation described can be performed on a parameter of the specified type: List<? extends String> lst List<? super String> lst List<String> lst String a = lst.remove(0); lst.add( foo ); 9. (2 pts) The Game interface defines a single method with the following signature: public void move(); Complete the following assignment where y is assigned an object that implements the Game interface and the method play() will print (using System.out.println) the message Game Move. Game y = 5

Problem 5 (20 pts) Recursion Nim is a two-player mathematical game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap. In some games, there is a limit on the number of objects that can be taken in one turn. Nim is usually played as a misère game, in which the player to take the last object loses. a b win move 0 1 no 1 1 yes 0,1 0 2 yes 0,1 1 3 yes 1,0 1 5 no 2 2 no 2 3 yes 2,2 We consider a misère nim game with two heaps where in a turn you must take 1, 2 or 3 objects. The table to the right shows some winning/loosing positions (by the way, nim heaps have nothing to do with the heaps used for priority queues) a) Write a recursive function for determining the winning position for a nim game with two heaps. static boolean nim(int a, int b) { that returns true if (a,b) is a winning position. Do not use any looping constructs. You donʼt need to compute the move that should be taken if the position is a winning one. b) What is the base case for your recursive function? c) Write a more efficient solution that using dynamic programming and/or caching. 6

7

Problem 6 (20 pts) Sets and Maps The StateRoads class keeps tracks of roads that are in a state. Each road is identified by a unique number. public class StateRoads { Map<Integer, Set<String>> map; public StateRoads() { // YOU MUST IMPLEMENT public void associatestatewithroadnumber(int roadnumber, String state) { // YOU MUST IMPLEMENT public Set<String> getstateswithroad(int roadnumber) { // YOU MUST IMPLEMENT What You Must Implement 1. (4 pts) Implement a constructor for the class that creates an empty map. 2. (13 pts) Implement the associatestatewithroadnumber method that associates the state with the specified road. 3. (3 pts) Implement the getstateswithroad method which returns the set of states associated with the specified road. We should be able to print the elements present in the returned set in sorted order. It should return an empty set if no states are associated with the road. You may find the following Map methods helpful: V get(object key) - Returns the value to which this map maps the specified key. V put(k key,v value) - Associates the specified value with the specified key in this map. Set<K> keyset() - Returns a set view of the keys contained in this map. boolean isempty() - Returns true if this map contains no key-value mappings. You may find the following Set methods helpful: boolean contains(object o) - Returns true if this set contains the specified element. boolean add(e o) - Adds the specified element to this set if it is not already present V remove(object key) - Removes the element from the set boolean isempty() - Returns true if this set contains no elements. 8

9

Problem 7 (20 pts) Linear Data Structures Implement the methods below based on the following Java class definitions. You may not add any instance variables, static variables or auxiliary methods to the LinkedList class. In addition, you may not use the Java API LinkedList class. public class LinkedList<T extends Comparable<T>> { private class Node { private T data; private Node next; public Node(T data) { this.data = data; next = null; private Node head; /* List head pointer */ public LinkedList() { // YOU MUST IMPLEMENT THIS METHOD public T getelementatindex(int index) { // YOU MUST IMPLEMENT THIS METHOD public boolean delete(t targetelement) { // YOU MUST IMPLEMENT THIS METHOD 1. (2 pts) Implement a constructor that defines an empty list. 2. (6 pts) Implement the method getelementatindex that returns the element at the specified index position (e.g., first element is at index 0, second element is at index 1, etc.). The method will return null if the index is larger than or equal to the number of elements in the list. 3. (12 pts) ) Implement the method delete that removes the first instance of targetelement from the list. The method will return true if the element is deleted; false otherwise. 10

11