# Fa09m2 - Exam Number Points off 1 2 3 4 5 Total off Net...

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

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

View Full Document
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); }
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){

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 / 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
Ask a homework question - tutors are online