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