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

Similar documents
Midterm Exam 2 CS 455, Fall 2013

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

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

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

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

Midterm Exam 2 CS 455, Spring 2015

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

Midterm Exam 2 CS 455, Spring 2013

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

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

Midterm Exam 2 CS 455, Spring 2011

Midterm Exam 2 CS 455, Spring 2014

Midterm Exam 2 CS 455, Fall 2014

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 1 Spring 2013 [Bono] Feb. 21, 2013

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

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 Midterm Exam 1 Fall 2013 [Bono] Wednesday, Oct. 2, 2013

Midterm Exam 2 CS 455, Fall 2012

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

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)

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

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

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

CS 314 Midterm 2 Fall 2012

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

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

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

CS 455 MT 2 Sample Questions

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

CS 307 Midterm 2 Spring 2008

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

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

CS 314 Exam 2 Spring

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

Java Map and Set collections

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

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

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

CS 314 Midterm 2 Spring 2013

CS165 Practice Final Exam

CS 307 Final Spring 2011

CS165 Practice Final Exam Answer Key

CS 314 Exam 2 Spring 2016

CS171 Midterm Exam. October 29, Name:

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

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

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

CS 314 Exam 2 Fall 2017

Computer Science Foundation Exam

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

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

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

CS 314 Final Fall 2012

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

Give one example where you might wish to use a three dimensional array

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

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

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

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

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

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

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

CS 216 Exam 1 Fall SOLUTION

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

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

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

CS 113 MIDTERM EXAM 2 SPRING 2013

CS56 Midterm Exam. E01, W16, Phill Conrad, UC Santa Barbara Wednesday, 02/10/2015

ECSE 321 Assignment 2

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

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

Csci 102: Sample Exam

CS 314 Exam 2 Spring 2018

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

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

ECE242 Data Structures and Algorithms Fall 2008

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

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

1. Is it currently raining in Tucson (4pts) a) Yes b) No? c) Don't know d) Couldn't know (not in Tucson)

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

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

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

CS 307 Final Fall 2009

Second Examination Solution

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

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

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

CSE 143 Final Part 1, August 18, 2011 Sample Solution

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

CS16 Midterm Exam 2 E02, 09F, Phill Conrad, UC Santa Barbara Wednesday, 11/18/2009

1. Write the output generated by the following code. (8pts) 2. Write the output generated by the following code (8pts)

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

Practice Midterm 1. Problem Points Score TOTAL 50

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

Transcription:

Name: USC NetID (e.g., ttrojan): CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018 There are 7 problems on the exam, with 59 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 double-sided code handout that accompanies the exam. If you need additional space to write any answers or for scratch work, pages 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 each page of the exam. Please read over the whole test before beginning. Good luck! 1

Problem 1 [8 points] Consider the use of a stack for storing information about function activations as a program runs. As discussed in lecture this is called the run-time stack or call stack. Reminder: a stack has the operations push, pop, top, and isempty. Part A. In this application calling a function corresponds to which stack operation? Part B. In this application returning from a function corresponds to which stack operation? Part C. Consider the following program and suppose we are currently executing at the point labeled **C**. Show two different possible contents of the run-time stack at this point (Note there are more than two correct answers). Make it clear which direction the stack is going by labeling which end is the top of each stack you draw. You may denote the element for one function activation with the name of that function. public class StackProb { public static void main(string[] args) { W(); X(); Y(); public static void W() { X(); Z(); public static void X() { Y(); Z(); public static void Y() { Z(); // **C** public static void Z() { return; 2

Problem 2 [8 pts] USC NetID: Consider the following two versions of a method to traverse a List: printlist1 and printlist2. (These methods will work for both LinkedList's and ArrayList's.) public static void printlist1(list<integer> list) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); System.out.println(); public static void printlist2(list<integer> list) { ListIterator<Integer> iter = list.listiterator(); while (iter.hasnext()) { System.out.print(iter.next() + " "); System.out.println(); Next to each of the four calls to printlist1 or printlist2 below give the big-o worst case time to run the code as a function of n, the length of the list: (the calls are labeled A - D) LinkedList<Integer> alinkedlist = new LinkedList<>();... // code to populate the linked list ArrayList<Integer> anarraylist = new ArrayList<>();... // code to populate the array list printlist1(alinkedlist); // A: printlist2(alinkedlist); // B: printlist1(anarraylist); // C: printlist2(anarraylist); // D: 3

Problem 3 [6 points] Consider the following incorrect Java program that does not compile with an error related to exceptions. Note: it uses another class called DataObject you do not need to understand anything about how DataObject works to solve this problem. (Note: see code handout for more information about the Scanner class.) Fix the program and enhance it as follows: if the user gives a file name for a non-existent file, for example, flkjm, print the following message: ERROR: File does not exist: flkjm Exiting program. and exit the program immediately (without crashing). If the file does exist, the program will operate normally. Space is given below to make your modifications / additions right in the code below. public class MyProg { public static DataObject readfile(string filename) { DataObject data = new DataObject(); // create an empty data obejct File infile = new File(fileName); Scanner filescanner = new Scanner(inFile); while (filescanner.hasnext()) { //... [ reads and puts stuff in data ] filescanner.close(); return data; public static void main(string[] args) { Scanner in = new Scanner(System.in); System.out.print ("Please enter a file name: "); String filename = in.next (); DataObject data = readfile(filename); data.process(); data.printresults(system.out); 4

USC NetID: Problem 4 [6 pts. total] Consider the following binary search tree, whose root is shown nearest the top of the page (i.e., it's not a sideways tree): Part A. For this specific tree, what s the maximum number of keys we would have to compare a target key with to lookup that target? (i.e., worst case, although the answer will be a number, not a big-o expression) Part B. For this specific tree, what s the minimum number of keys we could compare a target key with to lookup that target? (i.e., best case) Part C. For this tree give an example of a target key we could look up that s not present in the tree that would entail comparing with the maximum number keys (i.e., your answer to part A) to search for it. 5

Problem 5 [6 pts.] Consider a Java Map to store student data. For each student it maps their name to their total score: (Note: the exact kind of map used doesn't matter for this problem.) Map<String, Integer> grades =... ; Read over the whole problem before answering. More information about Maps on the code handout. Part A. Suppose you put the following entry in the map: grades.put("sam", 82); Write code to correctly update this map entry so it has the value 99 instead: Part B. Suppose you then put the following entry in the map: grades.put("joe", 88); Write code to correctly update this map entry so it has the key "Zhou" instead: After your code runs, the map will have the contents: Sam 99 Zhou 88 6

Problem 6 [10 points] USC NetID: Suppose you want to create a Map<Student, String> that maps students to their letter grades (letter grades such as "A", or "B+" are represented as the String value in the map entries). One of the things we want to use this Map for is to create a class list of students and their grades in sorted order by name, so we will choose a TreeMap for this application. When we traverse the TreeMap, the results will come out in the order shown in this example (note that for students with the same name, they are ordered by student ID): last first id grade Blow Joe 3724 A Blow Sam 2475 B+ Crud Sally 4237 A- Smith John 3387 A Smith John 4520 B- The current version of the Student class is shown below. Assume the Student methods shown have already been implemented (i.e., don't implement them). Modify the Student class to enable you to use it as the KeyType in this TreeMap. Hint: see code handout. public class Student { private String lastname; private String firstname; private int id; public Student(String first, String last, int studentid) {... public String getfirst() {... public String getlast() {... public int getid() {... 7

Problem 7 [15 points] Write a recursive method decrsquaresodd which, when given an int n, returns a LinkedList of all the squares of odd values in the range [1,n] in decreasing order. A solution that doesn't use recursion will receive little to no credit. For full credit, your function must take no more than O(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 LinkedList methods. Some examples: n 1 [1] 2 [1] 3 [9, 1] 4 [9, 1] return value of decrsquaresodd (n) 7 [49, 25, 9, 1] // returns a list of the squares of odd values in the range [1,n] in decreasing order // PRE: n >= 1. public static LinkedList<Integer> decrsquaresodd(int n) { 8

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