This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Test 1: Compsci 100 Owen Astrachan February 13, 2008 Name: (3 points) Login: Honor code acknowledgment (signature) value grade Problem 1 12 pts. Problem 2 20 pts. Problem 3 22 pts. Problem 4 28 pts. Problem 5 15 pts. TOTAL: 97 pts. This test has 14 pages, be sure your test has them all. Do NOT spend too much time on one question — remember that this class lasts 75 minutes. In writing code you do not need to worry about specifying the proper import statements . Assume that all libraries and packages we’ve discussed are imported in any code you write. Unless indicated otherwise, here’s the Node class for this test. public class Node{ public String info; public Node next; public Node(String s, Node link){ info = s; next = link; } } 1 PROBLEM 1 : ( It Depends (12 points) ) Part A (4 points) What value is returned by the call calculate(2043) ? What is the complexity (bigOh, in terms of N) of the call calculate(N) ? Briefly justify your answers. public int calculate(int n){ int prod = 1; while (prod < n){ prod *= 2; } return prod; } Part B (8 points) Consider method zcal below, the call zcal(5) evaluates to 15. public int zcal(int n){ if (n == 0) return 0; return n + zcal(n1); } Using bigOh what is the running time of the call zcal(n) ? Justify your answer. Using bigOh what is the value returned by the call zcal(n) (note: complexity of value returned, not running time: use bigOh) Using bigOh what is the running time of the call zcal(zcal(n)) based on your answers to the previous two questions. Justify. Using bigOh what is the value returned by the call zcal(zcal(n)) (again, based on previous answers, justify). 2 PROBLEM 2 : ( Listing (20 points) ) All methods are in a class in which the Node class defined on the frontpage of this test is accessible. Part A (4 points) The method below returns the sum of the lengths of all strings in a linkedlist: public int totalLength(Node list) { if (list == null) return 0; return list.info.length() + totalLength(list.next); } Write method listLength to return the number of nodes in a linked list. Model your code on the method above, your code must be recursive. public int listLength(Node list) { if (list == null) return 0; return } Part B (6 points) The expression below correctly calculates the average length of all the strings in a list: double avg = totalLength(list)*1.0/listLength(list); A classmate argues that the method below calculates the value more efficiently: public double average(Node list){ int total = 0; int count = 0; while (list != null){ total += list.info.length(); count++; list = list.next; } return total*1.0/count; } Provide justification that your classmate is correct and justification that your classmate is wrong (two justifications, be sure to indicate which is which.) 3 Part C (4 points) Write a method sorted that returns true if its linkedlist parameter is in sorted/alphabetical order, and returns false otherwise. The call sorted(list)...
View
Full
Document
This note was uploaded on 04/27/2008 for the course COMPSCI 100 taught by Professor Astrachan during the Spring '06 term at Duke.
 Spring '06
 Astrachan

Click to edit the document details