Sp04Midterm2

Sp04Midterm2 - Points off 1 2 3 4 5 6 Total off Net Score...

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

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

View Full Document
B. What is the output of sophie(5); public void sophie(int y) { if( y < 0 ) System.out.print( y + " " ); else { System.out.print( y + " " ); sophie( y – 1); } } C. What is the output of pollychrest(7); public void pollychrest(int z) { System.out.print( z + " " ); if( z >= 0 ) { pollychrest( z – 2 ); System.out.print( z + " " ); } } _____________________________________________________________________________ D. What is the output of System.out.println( victory(4) ); public int victory(int a) { if( a < 0 ) return 1; else return 2 + victory(a – 1) + victory(a – 2); } _____________________________________________________________________________ CS 307 – Midterm 2 – Spring 2004 2
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 total = 0; for(int i = 0; i < n; i++) { total++; } _____________________________________________________________________________ F. What is T(N), the actual number of executable statements, for the following code segment? Count the each of the initialization statements, boolean expressions, and increment statements in a for loop as separate statements, not as a single statement. (The variable n is an integer parameter sent to the method that contains this code segment.) int total = 0; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { total++; } } _____________________________________________________________________________ G. What is the Big O of the following code segment? Method lively is O(N) where N is equal to the argument sent to the method. (The variable n is an integer parameter sent to the method that contains this code segment.) int total = 0; for(int i = 0; i < n; i++) { total++; lively(n); } _____________________________________________________________________________ CS 307 – Midterm 2 – Spring 2004 3

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

View Full Document
H. 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 t1 = 0; int t2 = 0; for(int i = 0; i < n; i++) { t1++; } for(int j = 2 * n; j >= 0; j--) { t2++; } _____________________________________________________________________________ I. A What is the Big O of the following code segment? Method agamemnon
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern