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

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

View Full Document Right Arrow Icon
Points off 1 2 3 4 5 6 Total off Net Score CS 307 – Midterm 2 – Spring 2004 Name__________________________________________ UTEID login name _______________________________ Section Leader's Name ___________________________ Instructions: 1. There are 4 questions on this test. 2. You have 2 hours to complete the test. 3. You may not use a calculator on the test. 4. When code is required, write Java code. 5. The style guide is not in effect except where noted. 6. Efficiency is not graded except where noted. 7. Ensure your answers are legible. 8. When writing code you may not use any methods or classes from the Java Standard Library except as noted and the System.out.print, System.out.println, the equals method, and native arrays. 9. In coding question you may add helper methods if you wish. 10. The last page of the test is scratch paper. 1. (2 points each, 30 points total) Short answer. If an error would occur answer "syntax error" or "runtime error" depending on what type of error it would be. Recall that when asked for Big O your answer should be the most precise 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). I want the most precise Big O function. (Closest without going under.) A. What is the output of System.out.println( surprise(4) ); public int surprise(int x) { if( x < 2 ) return 1; else return 1 + surprise( x – 1 ); } _____________________________________________________________________________ CS 307 – Midterm 2 – Spring 2004 1
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern