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

Similar documents
CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

Midterm Exam 2 CS 455, Spring 2015

CS 455 Midterm Exam 2 Spring 2017 [Bono] Tuesday, April 4, 2017

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

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

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

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Midterm Exam 2 CS 455, Spring 2011

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

CS 455 Final Exam Fall 2016 [Bono] December 13, 2016

CS 455 Final Exam Fall 2012 [Bono] Dec. 17, 2012

CS 455 Midterm Exam 1 Fall 2015 [Bono] Thursday, Oct. 1, 2015

Midterm Exam 2 CS 455, Fall 2013

CS 455 Midterm Exam 1 Fall 2017 [Bono] Thursday, Sep. 28, 2017

Midterm Exam 2 CS 455, Fall 2014

CS 455 Midterm Exam 1 Spring 2013 [Bono] Feb. 21, 2013

Midterm Exam 2 CS 455, Spring 2013

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Midterm Exam 2 CS 455, Spring 2014

Midterm Exam 2 CS 455, Fall 2012

CS 455 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

CS 455 Midterm Exam 1 Spring 2015 [Bono] Thursday, Feb. 19, 2015

EXAMINATIONS 2016 TRIMESTER 2

Prelim One Solution. CS211 Fall Name. NetID

CS 455 Midterm Exam 1 Spring 2011 [Bono] Feb. 17, 2011

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

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Csci 102: Sample Exam

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

CS 307 Midterm 2 Fall 2010

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

CS 307 Midterm 2 Fall 2009

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

Practice exam for CMSC131-04, Fall 2017

Prelim 2. 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

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

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

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Final Examination

DUKE UNIVERSITY Department of Computer Science. Test 1: CompSci 100

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

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

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

CS 307 Final Spring 2011

DUKE UNIVERSITY Department of Computer Science. Test 2: CompSci 100e

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

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

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

CS 61B Discussion 5: Inheritance II Fall 2014

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CSE 143 Au03 Final Exam Page 1 of 15

CS 314 Midterm 2 Fall 2012

EXAMINATIONS 2017 TRIMESTER 2

CS 455 Midterm Exam 1 Fall 2010 [Bono] Sept. 29, 2010

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

1.00/ Introduction to Computers and Engineering Problem Solving. Final / December 13, 2004

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

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

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

public static<e> List<E> removeoccurrences(list<e> origlist, E remove) {

USAL1J: Java Collections. S. Rosmorduc

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

CS 314 Exam 2 Spring

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

CS 1331 Fall 2016 Exam 3 Part 1 ANSWER KEY

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

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

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

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

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

CS 1302 Chapter 9 (Review) Object & Classes

Midterm Exam CS 251, Intermediate Programming March 6, 2015

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

CMSC 206: Data Structures Final Exam Reference May 2018

University of Maryland College Park Dept of Computer Science

IT 313 Advanced Application Development

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2011

CS/ENGRD2110: Final Exam

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

Inheritance and Interfaces

Model Solutions. COMP 103: Test May, 2013

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

CS 314 Exam 2 Spring 2016

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

CS 113 MIDTERM EXAM 2 SPRING 2013

PART A (5 pts): Assume an ArrayList named values contains the following eight numbers:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Final Examination

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

Distributed Systems Recitation 1. Tamim Jabban

CS 314 Final Fall 2012

CS 455 MT 2 Sample Questions

Transcription:

Name: USC NetID (e.g., ttrojan): CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016 There are 7 problems on the exam, with 50 points total available. There are 8 pages to the exam (4 pages double-sided), including this one; make sure you have all of them. If you need additional space to write any answers, pages 7 and 8 of the exam are left blank for that purpose. If you use these pages for answers you just need to direct us to look there. Do not detach any pages from this exam. Note: if you give multiple answers for a problem, we will only grade the first one. Avoid this issue by labeling and circling your final answers and crossing out any other answers you changed your mind about (though it s fine if you show your work). Put your name and USC username (a.k.a., NetID) at the top of the exam. Also put your NetID at the top right of the front side of each page of the exam. Please read over the whole test before beginning. Good luck! Set<ElmtType> Interface The classes that implement this interface are: TreeSet and HashSet. Selected methods: boolean contains(elmttype elmt) int size() boolean add(elmttype elmt) boolean remove(elmttype elmt) boolean isempty() Iterator<ElmtType> iterator() Returns true iff elmt is in the set Returns number of elements in the set Ensures that elmt is in the set. Returns true iff the set changed as a result of this call Removes elmt from the set. Returns true iff the set changed as a result of this call Returns true iff the set contains no elements. Returns an iterator over the elements in the set. String Class (selected methods) int length() boolean isempty() Returns the number of characters in the string. Returns true iff this is an empty string. char charat(int index) Returns the character at the specified index. PRE: 0 <= index < length() String substring(int beginindex, int endindex) Returns a new string that has the characters from positions beginindex through endindex-1 from this string. PRE: 0 <= beginindex <= length() and 0 <= endindex <= length() and beginindex <= endindex String substring(int beginindex) Returns a new string that has the characters from positions beginindex through the end of this string. PRE: 0 <= beginindex < =length() 1

Problem 1 [7 points] Give the big-o worst case time to solve each of the following problems (put your answers in the space provided to the left): 1. search for a value in a sorted array of size n 2. reverse the values in an array of size n 3. use insertion sort to sort n values in an array 4. compute the minimum and maximum values in an array of size n 5. do list.get(i) where list is a Java LinkedList object of size n 6. do list.get(i) where list is a Java ArrayList object of size n 7. determine whether a linked list is already sorted Problem 2 [4 points] Suppose you are implementing your own Stack class. For each of the following, answer one of: front, end, or doesn't matter. Part A. For a LinkedList representation of a Stack (i.e., using the Java LinkedList class), it would be most efficient to keep the top of the stack at which end of the list? Part B. For an ArrayList representation of a Stack, it would me most efficient to keep the top of the stack at which end of the array list? 2

Problem 3 [2 points] Part A. For each loop iteration of binary search in an array, we eliminate roughly how many elements from further consideration? Circle the one that matches most closely. In the following, k is the number of values that are still being considered at the start of that iteration. a. 1 b. log 2 k c. k/2 d. k-1 Part B. Answer the same question for linear search in an array. Circle the best choice: a b c d Problem 4 [4 points] In addition to a default (i.e., no-parameter) constructor, the TreeSet class has a second constructor that takes a Comparator as a parameter. Relevant documentation provided below: Comparator<Type> interface An object that can compare two objects of type Type. Has one method defined by the interface: int compare(type object1, Type object2) Must return a negative number if object1 should come before object2, 0 if object1 and object2 are equal, or a positive number if object1 should come after object2. Selected TreeSet<ElmtType> constructors: TreeSet<ElmtType>() Creates an empty TreeSet, sorted according to the natural ordering of its elements. TreeSet<ElmtType>(Comparator<ElmtType> comp) Creates an empty TreeSet, sorted according to the specified comparator. Why would the second constructor be useful? Explain by describing a scenario where we would need to use the second a constructor for a TreeSet and why. (Please limit your response to one or two sentences. Do not write code.) 3

Problem 5 [6 points] Consider the following program that uses exceptions. Note: MyException and FileNotFoundException are both subclasses of IOException: public class MyProg { public static void main(string[] args) { try { ArrayList<String> lines = readfile(); int maxsofar = 0; for (int i = 0; i < lines.size(); i++) { if (lines.get(i).length() > maxsofar) { maxsofar = lines.get(i).length(); System.out.println("Max line is " + maxsofar); catch (MyException exc) { System.out.println("Error #1"); catch (IOException exc) { System.out.println("Error #2"); public static ArrayList<String> readfile() throws IOException { ArrayList<String> lines = new ArrayList<String>(); try { Scanner infile = new Scanner(new File("inputData")); if (!infile.hasnext()) { throw new MyException(); while (infile.hasnextline()) { lines.add(infile.nextline()); catch (FileNotFoundException exc) { System.out.println("Error #3"); return lines; What is the output of the program for each of the following scenarios (show your answer to the right of each): 1. The file inputdata is empty. 2. The file inputdata has the contents: the elephant went to the zoo today 3. The file inputdata does not exist. 4

Problem 6 [7 points] Consider the following Student class that overrides equals and hashcode from Object: (Note: this is a different version of this class than we saw in lecture recently.) public class Student { private String name; private int score; public Student(String name, int score) { this.name = name; this.score = score; public boolean equals(object otherobject) { // code to check that the two objects are the same type not shown... //... Student other = (Student) otherobject; return name.equals(other.name); public int hashcode() { return name.hashcode() + score; //other methods not shown... The above code does not obey the contract for hashcode and equals, which is if a.equals(b) then a.hashcode() == b.hashcode() Give a sample code snippet using a HashSet that exposes the problem with the Student class provided, along with an explanation of what happens with the code related to this issue. The cover page of the exam has some Set documentation. Your code should use the variable defined for you below: Set<Student> section = new HashSet<Student>(); // creates an empty set Put your explanation here: 5

Problem 7 [20 points] Write a recursive boolean method to tell use whether a String is a set of matched parentheses. For the purposes of this problem we will narrowly define what is considered a set of matched parentheses, as illustrated by the examples below. A solution that doesn t use recursion or has a loop will receive little to no credit. Notes: solutions which include calls to substring can receive full credit, and (as usual) you are allowed to define a helper method to do the actual recursion. The cover page of the exam has some String documentation. Examples: str matched(str) str matched(str) true () true (() false ()) false (()) true (a) false ((((())))) true ( false )( false ()(()) false a false a(b(abc)de)fg false // returns true iff the String is a set of matched parentheses. (uses recursion) public static boolean matched(string str) 6

Extra space for answers or scratch work. (DO NOT detach this page.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 7

Extra space for answers or scratch work (cont.) (DO NOT detach this page.) If you put any of your answers here, please write a note on the question page directing us to look here. Also label any such answers here with the question number and part, and circle the answer. 8