Sp08m2 - Exam Number: Points off 1 2 3 4 Total off Net...

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

View Full Document Right Arrow Icon
CS 307 – Midterm 2 – Spring 2008 1 Exam Number: Points off 1 2 3 4 Total off Net Score CS 307 – Midterm 2 – Spring 2008 Name__________________________________________ UTEID login name _______________________________ TA's Name: Mario Ruchica Vishvas (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. 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(7) ); public int a(int n){ if( n > 10 ) return 1; else return 2 + a(n + 1); } B. What is the output of System.out.println( b(4) ); public int b(int val){ if( val <= 1 ) return val; else return val + b(val - 1) + b(val - 2); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 307 – Midterm 2 – Spring 2008 2 C. What is the output when method c is called? public void c(){ int[] data = {5, 3, 2, 6, 1, -5, 3}; System.out.println( helper(data, 0) ); } public int helper(int[] list, int p){ if( p == list.length - 1 ) return list[p]; else return Math.max( helper(list, p + 1), list[p] ); } D. What is the worst case Big O of method d ? N = nums.length . public int d(int[] nums){ int total = 0; for(int i = 0; i < nums.length; i++) for(int j = nums.length - 1; j >= 0; j--) if( nums[i] == nums[j] ) total++; return total; } E. What is the Big O of method e ? N = nums.length . public int e(int[] nums){ int total = 0; for(int i = 0; i < nums.length; i++) total += nums[i]; int limit = nums.length / 2; for(int j = 0; j < limit; j++) total += nums[j] * nums[j]; return total; } F. What is the Big O of method f ? N = mat.length // pre: mat is a square matrix, mat.length > 0 public int f(int[][] mat){ int row = 1; int col = 1; int total = 0; while( row < mat.length ){ total += mat[row][col]; row *= 3; col *= 3; } return total; }
Background image of page 2
CS 307 – Midterm 2 – Spring 2008 3 G. What is the best case Big O of method g ? N = numbers.length public static void g(int[] numbers) { int limit = numbers.length; for (int pass = 1; pass < limit; pass++) {
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2010 for the course CS 307 taught by Professor Scott during the Spring '08 term at University of Texas at Austin.

Page1 / 14

Sp08m2 - Exam Number: Points off 1 2 3 4 Total off Net...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online