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

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

View Full Document Right Arrow Icon
CS 307 – Midterm 2 – Spring 2009 1 Exam Number: Points off 1 2 3 4 5 Total off Net Score CS 307 – Midterm 2 – Spring 2009 Name__________________________________________ UTEID login name _______________________________ TA's Name: Guhan Todd Xiuming (Circle One) Instructions: 1. Please turn off your cell phones and other electronic devices. 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. Do not write code to check the preconditions. 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 Big O of the following method? N = mat.length // pre: mat != null, mat is a square matrix public double methodA(double[][] mat){ int total = 0; for(int c = mat.length - 1; c >= 0; c--) for(int r = c; r >= 0; r--) total += mat[r][c]; return total; }
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 2009 2 B. What is the worst case Big O of the following method? N = data.length public int methodB(int[] data){ int count = 0; for(int i = 0; i < data.length; i++){ if( data[i] % 2 == 0 ) count++; } for(int i = 1; i < data.length; i++){ if( data[i] % 10 == 0 ) count++; } return count; } C. What is the worst case Big O of the following method? N = data.length . The length method from the String class is O(1). public int methodC(String[] data){ int sum = 0; for(int i = 0; i * i < data.length; i++){ int temp = i * i; if( data[temp] != null ) sum += data[temp].length(); } return sum; } D. What is the worst case Big O of the following method? N = data.length . public boolean methodD(int[] data, int tgt){ int temp; boolean found = false; int index = 0; int limit = data.length - 4; while( index < limit && !found ){ temp = 0; for(int off = 0; off < 5; off++) temp += data[index + off]; found = temp == tgt; index++; } return found; } E. A method is O(2 N ). It takes 5 seconds for the method to complete when given a data set with 100 elements. What is the expected time for the method to complete when given a data set with 104 elements? F.
Background image of page 2
Image of page 3
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 / 15

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

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

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