# fa04m2 - Points off 1 2 3 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 Total off Net Score CS 307 – Midterm 2 – Fall 2004 Name__________________________________________ UTEID login name _______________________________ TA's Name ___________________________ (Allen, Alison, or Peter) Instructions: 1. There are 4 questions on this test. 2. You have 2 hours to complete the test. 3. You may not use a calculator on the test. 4. When code is required, write Java code. 5. The style guide is not in effect except where noted. 6. Efficiency is not graded except where noted. 7. Ensure your answers are legible. 8. When writing code you may not use any methods or classes from the Java Standard Library except as noted and the System.out.print, System.out.println, the equals method, and native arrays. 9. In coding question you may add helper methods if you wish. 10. The last page of the test is scratch paper. 1. (2 points each, 30 points total) Short answer. Place you answers on the attached answer sheet. If an error would occur answer "syntax error" or "runtime error" depending on what type of error it is. 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 O it 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( perch(5) ); public int perch(int n) { if( n < 1 ) return 2; else return 2 + perch( n - 1); } CS 307 – Midterm 2 – Fall 2004 1

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

View Full Document
B. What is the output of tang(4); public void tang(int n) { if( n <= 0 ) System.out.print( '*' ); else { tang( n – 1); System.out.print( n + " " ); } } C. What is the output of System.out.println( strake(6)); public int strake(int n) { if( n <= 3) return 3; else return 3 + strake(n – 1) + strake(n - 1); } D. What is Big O of the following code segment? (The variable n is an integer parameter sent to the method that contains this code segment.) int s = 0; for(int i = n; i >= 0; i--) { s += i * i; } E. What is the Big O of the following code segment? (The variable n is an integer parameter sent to the method that contains this code segment.) int x = 1; int j = 1; while( j <= n ) { x *= j; j *= 2; } CS 307 – Midterm 2 – Fall 2004 2
What is the Big O of the following method? public int[][] tautog(int[][] mat) { //pre: mat != null, mat.length >= 10, // mat[0].length >= 10 int[][] result = new int[10][10]; for(int r = 0; r < result.length; r++) for(int c = 0; c < result[0].length; c++) result[r][c] = mat[r][c]; return result; } G. What is the Big O of the following code segment? Method guppy is O(1). Method shark is O(1). (The variable n is an integer parameter sent to the method that contains this code segment.) int foo = 0; for(int i = 0; i < n; i++) for(int j = 0; j < i; j++) { guppy(); shark(); foo++; } H. What is the Big O of the following code segment? Method dolphin is O(1). (The variables n and m are integer parameters sent to the method that contains this code segment.) for(int i = 0; i < n; i++)

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 ]}

### Page1 / 17

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online