Spring 08 CS 100 Test 1

Spring 08 CS 100 Test 1 - Test 1 Compsci 100 Owen Astrachan...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PROBLEM 1 : ( It Depends (12 points) ) Part A (4 points) What value is returned by the call calculate(2043) ? What is the complexity (big-Oh, 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(n-1); } Using big-Oh what is the running time of the call zcal(n) ? Justify your answer. Using big-Oh what is the value returned by the call zcal(n) (note: complexity of value returned, not running time: use big-Oh) Using big-Oh what is the running time of the call zcal(zcal(n)) based on your answers to the previous two questions. Justify. Using big-Oh what is the value returned by the call zcal(zcal(n)) (again, based on previous answers, justify). 2
Image of page 2
PROBLEM 2 : ( Listing (20 points) ) All methods are in a class in which the Node class defined on the front-page of this test is accessible. Part A (4 points) The method below returns the sum of the lengths of all strings in a linked-list: 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
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Part C (4 points) Write a method sorted that returns true if its linked-list parameter is in sorted/alphabetical order, and returns false otherwise. The call sorted(list)
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern