All AP Computer Science A Resources
Example Questions
Example Question #71 : Programming Constructs
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:
8
3
5
12
6
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
GMAT Tutors in Los Angeles, Algebra Tutors in Denver, Computer Science Tutors in San Diego, Algebra Tutors in New York City, GMAT Tutors in San Francisco-Bay Area, SAT Tutors in Phoenix, GRE Tutors in Seattle, Algebra Tutors in San Diego, SSAT Tutors in Atlanta, Physics Tutors in Los Angeles
Popular Courses & Classes
SAT Courses & Classes in Atlanta, ISEE Courses & Classes in Washington DC, ACT Courses & Classes in Boston, SAT Courses & Classes in Los Angeles, ACT Courses & Classes in Philadelphia, GMAT Courses & Classes in Denver, GRE Courses & Classes in San Diego, LSAT Courses & Classes in Houston, GRE Courses & Classes in Phoenix, GRE Courses & Classes in San Francisco-Bay Area