{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Sp07m2 - Points off 1 2 3 4 4 Total off Net Score CS 307...

This preview shows pages 1–4. Sign up to view the full content.

Points off 1 2 3 4 4 Total off Net Score CS 307 – Midterm 2 – Spring 2007 Name__________________________________________ UTEID login name _______________________________ TA's Name Alison Aparajit Vineet (Circle One) Instructions: 1. Please turn off your cell phones 2. There are 5 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 restrictive correct Big O function. For example Selection Sort has an average case Big O of O(N^2), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N^3) or O(N^4). I want the most restrictive correct Big O function. (Closest without going under.) A. What is the output of System.out.println( a(-3) ); public int a(int n){ if( n > 5 ) return n; else return n + a(n + 3); } B. What is the output of the method call b("masters_open_open_champ", 0, 1 ); public void b(String s, int n, int m){ if( n >= 0 && n < s.length() ){ System.out.print( s.charAt(n) ); b(s, n + m, m + 2); System.out.print( s.charAt(n) ); } } CS 307 – Midterm 2 – Spring 2007 1

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

View Full Document
C. What is the output of System.out.println( c(5) ); public int c(int n) { if( n <= 2 ) return 2; else return 1 + c( n – 1 ) + c( n - 2 ); } D. What is the Big O of method d ? public int d(int N){ int tot = 0; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) for(int k = 0; k < N; k++) tot += N % (i + j + k + 1); return tot; } E. What is the average case Big O of method e ? N = data.length . public int e(int[] data){ int tot = 0; for(int i = 0; i < data.length; i += 4) tot += data[i]; for(int i = 1; i < data.length; i *= 2) tot += data[i]; return tot; } F. What is the Big O of method f ? Method foo has a Big O of O(N) where N is the length of the array sent as a parameter. N = data.length . public int f(char[] data){ int tot = 0; for(int i = 0; i < data.length; i++) for(int j = data.length - 1; j >= i; j--) tot += foo(data, i, j); return tot; } G. What is the worst case Big O of method g ? N = data1.length . M = data2.length. // pre: data1.length > 10 public int g(double[] data1, boolean[] data2){ int tot = 0; for(int i = 0; i < data1.length - 10; i++) for(int j = 0; j < 10; j++) tot += data1[i+j] * 2; for(int i = 0; i < data2.length - 1; i++) if( data2[i] && data2[i+1] ) tot++; return tot; } CS 307 – Midterm 2 – Spring 2007 2
H. For an array based list class what is the expected, average case Big O of the get method?

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}