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

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

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

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

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

Midterm Exam 2 CS 455, Fall 2013

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

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

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2014

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

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

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

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

Midterm Exam 2 CS 455, Spring 2015

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

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

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

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

Midterm Exam 2 CS 455, Fall 2014

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

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

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

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

Midterm Exam 2 CS 455, Fall 2012

Midterm Exam 2 CS 455, Spring 2013

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

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

COS 226 Algorithms and Data Structures Spring Midterm Exam

CS 455 MT 2 Sample Questions

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

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)

CS 307 Midterm 2 Spring 2008

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Java Map and Set collections

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

CS 314 Exam 2 Spring

CS 314 Midterm 2 Fall 2012

CS 307 Midterm 2 Fall 2008

CSE332 Summer 2010: Final Exam

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

CSCE 2014 Final Exam Spring Version A

Csci 102: Sample Exam

ArrayList; Names example

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

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

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

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

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

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

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

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

University of Massachusetts Amherst, Electrical and Computer Engineering

McGill University Faculty of Science School of Computer Science. MIDTERM EXAMINATION - Sample solutions. COMP-250: Introduction to Computer Science

CS 216 Exam 1 Fall SOLUTION

CS 10, Fall 2015, Professor Prasad Jayanti

Second Examination Solution

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CS171 Midterm Exam. October 29, Name:

CS 314 Final Fall 2012

CS 305j Midterm 2 Fall 2006

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

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

CS 307 Final Fall 2009

Data Structures and Algorithms, Winter term 2018 Practice Assignment 3

Final Exam. COMP Summer I June 26, points

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

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

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

CS 113 MIDTERM EXAM 2 SPRING 2013

CS 104 (Spring 2014) Final Exam 05/09/2014

Largest Online Community of VU Students

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

CIS 110 Introduction to Computer Programming 8 October 2013 Midterm

CS 307 Midterm 2 Fall 2009

PRACTICE MIDTERM EXAM #2

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

/** * finds the index of v in a if present * * * * */ public static int findinsortedarray(int[] a, int v) {...

We've tried to include the common errors and grading standard for every question.

CS 112 Midterm Exam Fall 2016

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Midterm #2a Fall minutes

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

Computer Science 62. Midterm Examination

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

SYSC Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

York University. AP/ITEC Section M INTRODUCTION TO DATA STRUCTURES Winter Midterm Test

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

CS 314 Exam 2 Fall 2017

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

Midterm Exam (REGULAR SECTION)

More on class design

CMPSCI 187 Midterm 1 (Feb 17, 2016)

ECE242 Data Structures and Algorithms Fall 2008

CIS Fall Data Structures Midterm exam 10/16/2012

CSE 143 Section Handout #13 Practice Midterm #5

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

Transcription:

Name: USC NetID (e.g., ttrojan): CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017 There are 6 problems on the exam, with 62 points total available. There are 10 pages to the exam (5 pages double-sided), including this one; make sure you have all of them. There is also a one-page code handout that accompanies the exam. If you need additional space to write any answers or for scratch work, pages 8, 9, and 10 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 every page of the exam. Please read over the whole test before beginning. Good luck! 1

Problem 1 [3 pts] Consider the following array of integers (array index shown above corresponding array value): 0 1 2 3 4 5 6 15 7 6 12 5 4 9 Show the contents of the same array after the first three passes of insertion sort (sorting into increasing order). Show each value under its array index. Hint: For an n-element array, it s completely sorted after n-1 passes. 0 1 2 3 4 5 6 Problem 2 [3 points] Recall that the method entryset that's part of the Map interface returns a Set view of the Map whose elements are type Map.Entry<KeyType, ValueType>. All the Map.Entry methods are shown below: KeyType getkey() ValueType getvalue() void setvalue(newval) Return the key of the entry Return the value of the entry Replace the current value with newval Why is there no setkey method for Map.Entry? (limit your answer to one or two sentences we will not read beyond that) 2

Problem 3 [8 pts.] USC NetID: Implement the boolean method sortdecreasingbyscore, which sorts an ArrayList of Student's so they are in decreasing order by score. For students with the same score, they must be in increasing order by name. You must use the Java sort utility (more info about that on the code handout), and include any additional code necessary to make it work (outside of the sortbyscore method). Note: your answer must be written using Java 7 features (e.g., no lambda expressions). Here is an example of contents of such an ArrayList shown in the order it would be in after a call to sortdecreasingbyscore: Jan 98 Lin 98 Ann 84 Fred 84 Moe 84 Zhou 80 Aarti 72 Here is the Student class it uses (only shows interface, since that's all we are using here). This is an already completed class; you may not modify the Student class for this problem. public class Student {... // private data not shown public Student(String name, int score) {... } public String getname() {... } public int getscore() {... } public void changescore(int newscore) {... } } public static void sortdecreasingbyscore(arraylist<student> students) { 3

Problem 4 [19 pts. total] Part A (15). Write the complete class definition for a Stack of ints that uses an array representation (not ArrayList) such that all stack operations run in amortized constant time or better. Hint: Use the strategy such that if you run out of space in the array, you double its size. (The "amortized" part of the time is because of the need to increase the array size periodically.) You may not use any Java library classes or methods in your code, except the the Arrays.copyOf method, described on the code handout. You do not have to write method comments for your class. The complete interface for the Stack class for this problem is shown by example below (Note: method names shown in bold): Stack s = new Stack(); s.push(3); s.push(0); int val1 = s.pop(); int val = s.top(); // creates an empty stack // pushes 3 onto the stack (has no return value) // pushes 0 onto the stack // pops top element from the stack and returns its value. // only legal on non-empty stack // returns the top element (stack is unchanged) // only legal on non-empty stack if (s.isempty()) { // returns true if stack is empty, otherwise false } System.out.println("stack is empty."); Space for your answer is given on this and the next page. 4

USC NetID: Problem 4 (cont.) (additional space for your answer to Part A; Part B) Part B (4). Write a representation invariant for your class implementation: 5

Problem 5 [14 points] Part A (5). Earlier in the semester we discussed in detail a Names class that used a partially-filled array representation. For each of the methods of this class, to the left of the method, write the big-o worstcase time for doing that method using the partially-filled array representation. // Stores a list of unique names in alphabetical order. Allows // look-up, insert, and removal of elements in the list. (PART A) BIG-O USING PARTIALLY- FILLED ARRAY (on left side) (PART C) BIG-O USING REP. FROM PART B (on right side) public class Names {... [instance variables not shown] // Creates an empty names object public Names() {... } // Returns the number of names in the list public int numnames() {... } 1) [done J] // Returns true iff target is present in names public boolean lookup(string target) {... } // Removes target from names, and returns true. // If target wasn't present in names, returns false and no change // made to names. public boolean remove(string target) {... } // Inserts newname into alphabetical names list. // Returns false and no change is made to names if name is already // present. public boolean insert(string newname) {... } } // Prints the names in alpha order, one per line public void printnames() {... } Part B (4). What Java class could we use to represent a Names object (i.e., type for the private data in Names) that is a big improvement over our original representation? Part C (5). To the right of each of the method headers above, write the big-o worst-case time or doing each operation using the representation you gave in part B. 6

USC NetID: Problem 6 [15 points] Write a recursive method oddvals, which returns an ArrayList of all the odd values from an ArrayList of integers. The values returned must be in the same relative order as they were in the original array list, and the original array list must be unchanged by the method. A solution that doesn't use recursion will receive little to no credit. For full credit, your function must take no more than n) time total. Note: you may create a helper method that does the actual recursion. Please refer to the code handout for a reminder of some ArrayList methods. Some examples: vals [] [] [3] [3] [2, 4] [] [5, 3, 10, 3, 4, 9, 2] [5, 3, 3, 9] [7, 13] [7, 13] return value of oddvals(vals) // returns a list of all the odd values from vals // vals is unchanged by this method. public static ArrayList<Integer> oddvals(arraylist<integer> vals) { 7

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. 8

USC NetID: 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. 9

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. 10