Unformatted text preview:

Points off 1 2 3 4 Total off Net ScoreCS 307 – Midterm 2 – Spring 2006Name__________________________________________UTEID login name _______________________________TA's Name ___________________________ (Chendi or Vinod)Instructions: 1. Please turn off your cell phones2. There are 4 questions on this test. 3. You have 2 hours to complete the test.4. You may not use a calculator on the test. 5. When code is required, write Java code. 6. When writing methods, assume the preconditions of the method are met.7. In coding question you may add helper methods if you wish.8. After completing the test please turn it in to one of the test proctors and show them your UTID.1. (2 points each, 30 points total) Short answer. Place you answers on the attached answer sheet. - If the code contains a syntax error or other compile error, answer “compile error”.- If the code would result in a runtime error / exception answer “Runtime error”.- If the code results in an infinite loop answer “Infinite loop”.Recall that when asked for Big O your answer should be the most precise Big O function. For example Selection Sort has an average case Big O of O(N^2), but per the formal definition of Big Oit is correct to say Selection Sort also has a Big O of O(N^3). I want the most precise Big O function. (Closest without going under.)A. What is the output of System.out.println( fuji(5) );public int fuji(int n){if( n <= 0 )return 1;elsereturn n + fuji( n – 1 );}CS 307 – Midterm 2 – Spring 2006 1B. What is the output of System.out.println( giant(8) );public int giant(int n){int result = 0;if( n <= 2 )result = 1;elseresult = 2 + giant( n – 3 );return result;}C. What is the output of System.out.println( trek(5) );public int trek(int n){ if( n <= 1 )return 3;else return 1 + trek( n – 2 ) + trek( n - 1 );} Consider a SinglyLinkedList that uses Node objects as discussed in class. The SinglyLinkedList class has references to the first node in the list, myHead, and the last node in the list, myTail. Assume the data in every node in the list is an integer. Consider the following two methods which are instance methods in the SinglyLinkedList class.// pre: none// post: return the total of all integers in the list.public int getTotal(){return helper(myHead, 0);}private int helper(Node n, int total){if( n == null )return total;elsereturn helper( n.getNext(), total + n.getData() );}D. Given N elements in the singly linked list what is the Big O time for method getTotal?E. Given N elements in the singly linked list what is the Big O space for method getTotal?CS 307 – Midterm 2 – Spring 2006 2F. Consider the following methods for a List class. The List behaves like a List from the Java standard library as discussed in class.Method Summary void add(Object o) Appends the specified element to the end of this list. void add(int index, Object element) Inserts the specified element at the specified position in this list. Object get(int index) Returns the element at the specified position in this list.Object remove(int index) Removes the element at the specified position in this list. Return the element that is removed from the list. Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element. Returns the element that was previously at index. int size() Returns the number of elements in this list.What is the output of method bmc assuming the precondition is met?// pre: s.size() == 0public void bmc(List s){assert s.size() == 0 : “Failed precondition: bmc”;s.add(“L”);s.add(“T”);s.add(1, “F”);s.add(0, “G”);s.add(“J”);s.add( 1, s.set( 3, s.remove(2) ) );s.remove(1);for(int i = 0; i < s.size(); i++)System.out.print( s.get(i) );}G. Given an array based list that may have extra capacity in its internal storage container what is the best case Big O for adding an element to the end of the list?H. Given an array based list that may have extra capacity in its internal storage container what is the worst case Big O for adding an element to the end of the list?CS 307 – Midterm 2 – Spring 2006 3I. What is the Big O of method bianchi? Assume println is O(1).//pre: data != nullpublic void bianchi(int[] data){assert data != null : “Failed precondition: bianchi”;int total = 0;for(int i = 0; i < data.length; i++)total += data[i];int diffs = 0;for(int j = 1; j < data.length; j++)diffs += data[j-1] – data[j];System.out.println( total + “ “ + diffs );}J. What is the Big O of method cervelo? Assume println is O(1).// pre: list != nullpublic void cervelo(int[] list){assert list != null : “Failed precondition: cervelo”;int total = 0;for(int i = list.length - 1; i >= 0; i--)for(int j = i - 1; j >= 0; j--)if( list[j] == list[i] ) total++;System.out.println( total );}K. What is the worst case Big O of method orba? Assume method pinarello is O(log N) where N is the magnitude of the integer it is sent. public int orba(int limit){int total = 0;for(int i = 0; i < limit; i++)for(int j = 0; j < limit; j++)total += pinarello( i * j );return total;}L. A program is O(N^3). It takes 3 seconds for the program to complete with a data set of 10,000 items. What is the expected run time of the program with 20,000 data items?CS 307 – Midterm 2 – Spring 2006 4M. A program is O(2^N). It takes 5 seconds for the program to complete with a data set of 40 items. What is the expected run time of the program with 43 data items?N. Assume a sorted set is implemented with an array based list as its internal storage container. What is the expected Big O of the contains method? The contains method determinesif a given element is present in the sorted set.O. Assume a sorted set is implemented with a doubly linked list as its internal storage container. What is the expected Big O of the contains method? The contains method determines if a given element is present in the sorted set.CS 307 – Midterm 2 – Spring 2006 52. (Using data structures / Implementing data structures 20 points) Write a method to reverse the elements in an array based list. You may not use any of the other methods in the ArrayList class.For example if the list contains the following items before the call to reverse:[A, B, C, D, E]After the call to reverse the list would contain the following elements.[E, D, C, B, A]You method shall be


View Full Document

UT CS 307 - CS 307 – Midterm 2

Documents in this Course
Midterm 2

Midterm 2

15 pages

Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download CS 307 – Midterm 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 307 – Midterm 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 307 – Midterm 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?