Fa09m2 - Exam Number: Points off 1 2 3 4 5 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 – Fall 2009 1 Exam Number: Points off 1 2 3 4 5 Total off Net Score CS 307 – Midterm 2 – Fall 2009 Name__________________________________________ UTEID login name _______________________________ TA's Name: Oswaldo Rashid Swati (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. 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(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 returned by the method call a(5) ? public int a(int n){ if(n <= 1) return 3; else return a(n - 2) + n; }
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 – Fall 2009 2 B. What is returned by the method call b("stack") ? 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 String b(String s){ if(s.length() <= 2) return s; else { int last = s.length() - 1; return s.charAt( s.length() - 1 ) + b(s.substring(1, last)) + s.charAt(0); } } C. What is output when method c is called? public String subC(int n){ if(n <= 0) return "*"; else return "*" + subC(n - 1) + "*" + subC(n - 1); } public void c(){ int n = 3; String result = subC(n); System.out.println( result.length() ); } D. What is output when method d is called? public void subD(int n){ if(n <= 2) System.out.print(n); else{ subD(n - n / 2); System.out.print(n); } } public void d(){ subD(7); }
Background image of page 2
CS 307 – Midterm 2 – Fall 2009 3 E. What is the Big O of method e ? N = list.length . public double e(double[] list){ double total = 0; for(int i = 0; i < list.length; i++) total += list[i]; for(int j = 1; j < list.length; j *= 2) total = total + list[j]; return total; } F. What is the best case Big O of method f ? N = list.length . Method subF is O(N) where N equals list.length . public int f(int[] list){
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.

Page1 / 18

Fa09m2 - Exam Number: Points off 1 2 3 4 5 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