DOC PREVIEW
UT CS 307 - Midterm 2

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 307 – Midterm 2 – Fall 2010 1 Exam Number: Points off 1 2 3 4 Total off Net Score CS 307 – Midterm 2 – Fall 2010 Name__________________________________________ UTEID login name _______________________________ TA's Name: Harsh Yi-Chao (Circle One) Instructions: 1. Please turn off your cell phones and other electronic devices. 2. 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. Do not write code to check the preconditions. 7. On 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 restrictive correct Big O function. For example Selection Sort has an average case Big O of O(N2), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N3) or O(N4). I want the most restrictive correct Big O function. (Closest without going under.) A. What is returned by the method call a(3)? public static int a(int x) { if(x <= 0) return 2; else return x * 2 + a(x - 1); }CS 307 – Midterm 2 – Fall 2010 2 B. What will happen when the method call b(5) is made? public static int b(int x) { if(x == 1) return 3; else return x + b(x - 3); } C. What is printed when the method call c1() is made? Recall the substring method: public String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. public static String c2(String s) { String es = ""; // an empty String; String result = es; if(s.length() > 1) { if( s.charAt(0) == s.charAt(s.length() - 1) ) result = es + s.charAt(0); result = c2(s.substring(1, s.length() - 1)) + result; } return result; } public static void c1(){ String start = "AABBABBCA"; System.out.print(c2(start)); } D. What is returned by the method call d(7)? public static int d(int x){ if(x <= 2) return x * 2; else return 2 + d(x - 1) + d(x - 3); }CS 307 – Midterm 2 – Fall 2010 3 E. What is the order (Big O) of method e? N = data.length public static int e(int[] data, int mult) { int accum = 0; for(int i = 0; i < data.length; i++) for(int j = 0; j < data.length; j++) accum += mult * data[i] * data[j]; return accum; } F. What is the worst case order (Big O) of method f? N = list.length. public int f(int[] list, int tgt){ int total = 0; for(int i = 0; i < list.length; i++) if(list[i] == tgt) total++; for(int i = 1; i < list.length; i *= 2) total += list[i]; return total; } G. What is the best case order (Big O) of method g? N = list.size() public static int g(LinkedList<Double> list){ int total = 0; for(int i = 0; i < list.size(); i++) { double hundreds = ((int) (list.get(i) * 100)) / 100; if(hundreds != 0 && hundreds != 5) total += hundreds; } return total; } H. What is the worst case order (Big O) of method h? N = words.size(). Assume the String methods length and charAt are O(1). public static void h(ArrayList<String> words, char ch) { int index = 0; while( index < words.size() ){ String temp = words.get(index); if(temp != null && temp.length() > 0 && temp.charAt(0) == ch ) words.remove(index); else index++; } }CS 307 – Midterm 2 – Fall 2010 4 I. What is the order (Big O) of method i_ ? N = org.length. The LinkedList constructor is O(1). public static LinkedList<Double> i_ (double[] org){ LinkedList<Double> result = new LinkedList<Double>(); for(int i = 0; i < org.length; i++) result.add(0, org[i]); // 0 is the position to insert in the list return result; } J. What is the order (Big O) of method j? N = mat.length. mat is a square matrix. // pre: mat is a square matrix public static int j(int[][] mat) { int total = 0; for(int r = 0; r < mat.length; r++) for(int c = 0; c < mat.length; c++) for(int i = 0; i < mat.length; i++) total += i * mat[r][c]; return total; } K. A sorting method uses the merge sort algorithm. It takes 19 seconds for the method to sort 500,000 distinct integers in random order. What is the expected time for the method to sort 1,000,000 distinct integers in random order? L. A method is O(N3). It takes the method 2 seconds to run when N = 10,000. What is the expected time for the method to run when N = 30,000 M. A method is O(N). When N = 100,000 the method takes 10 seconds to run. Given 0.2 seconds, how many pieces of data can the method process?CS 307 – Midterm 2 – Fall 2010 5 Consider the following Node class for questions N and O. public class Node { public Object data; public Node next; public Node(Object d, Node n) { data = d; next = n; } } N. What is output by the following code? Node n1 = new Node(2, null); n1.next = new Node(7, new Node(5, n1)); Node n2 = n1.next; n2 = n2.next.next; System.out.print(n2.data); O. Draw the resulting variables and objects after the following code executes. Draw node objects as shown below. (The example has all 2 instance variables set to null. Any variables that refer to the example below are not shown. You must show them in your drawing.) Use arrows to show the references that exist and a forward slash to indicate variables that store null. data next Node n3 = new Node("A", null); Node n4 = new Node(n3, n3); n3.next = new Node(null, n4);CS 307 – Midterm 2 – Fall 2010 6 2. (Data Structures 25 points) Write a method for the AbstractSet class from assignment 8 that removes all of the elements present in an ISet passed as a parameter from the calling object. • The only method you can use


View Full Document

UT 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

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download 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 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 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?