All AP Computer Science A Resources
Example Questions
Example Question #71 : Program Implementation
public int foo(int n)
{
if (n < 0)
return 1;
else
return foo(n-2) + foo(n-1)
}
What is the value returned by foo(3)?
Possible Answers:
6
5
8
3
12
Correct answer:
8
Explanation:
The answer is 8.
This question tests knowledge of recursion. The correct strategy is to build a tree of function calls. Since the base case of foo returns 1, we can just add up all the function calls that return the base case. In the tree below, the green functions represent the base cases, with an argument less than 0.
All AP Computer Science A Resources
Popular Subjects
Statistics Tutors in Washington DC, Chemistry Tutors in Chicago, GRE Tutors in Atlanta, Reading Tutors in Miami, French Tutors in Seattle, Physics Tutors in San Francisco-Bay Area, Computer Science Tutors in Seattle, ISEE Tutors in Chicago, Physics Tutors in Philadelphia, English Tutors in Houston
Popular Courses & Classes
GMAT Courses & Classes in Atlanta, SAT Courses & Classes in Chicago, LSAT Courses & Classes in New York City, Spanish Courses & Classes in Phoenix, LSAT Courses & Classes in Washington DC, Spanish Courses & Classes in Miami, SAT Courses & Classes in Los Angeles, GMAT Courses & Classes in Phoenix, SSAT Courses & Classes in Miami, GMAT Courses & Classes in Seattle
![Learning Tools by Varsity Tutors](https://vt-vtwa-app-assets.varsitytutors.com/assets/problems/og_image_practice_problems-9cd7cd1b01009043c4576617bc620d0d5f9d58294f59b6d6556fd8365f7440cf.jpg)