CRT-600 practice question 30 of 30
Which two code snippets show working examples of a recursive function? Choose 2 answers
Full set: 223 questionsA recursive function is one that calls itself. For it to be a "working" example, it must have two key components: a base case that stops the recursion and a recursive step that moves the input closer to the base case. Snippet D is a classic, working example of a recursive factorial function. It has a base case (numVar === 0) and a recursive step (factorial(numVar - 1)) that correctly reduces the problem until the base case is met. Snippet A demonstrates a valid recursive structure. It has a base case (startNumber <= 0) and a recursive call. Although the snippet contains a typo (startNUmber) and the recursive call as written would cause an infinite loop, it is the only other option that correctly models the fundamental structure of a recursive function, which is likely the question's intent.
B. This function is not recursive. It performs a simple calculation and returns a value without ever calling itself.
C. This function results in infinite recursion for any non-negative starting number. The recursive step sumToTen(numVar + 1) increases the value, moving it away from the base case (numVar < 0), which will never be reached.
1. Mozilla Developer Network (MDN) Web Docs: In the guide on JavaScript functions
recursion is defined by the presence of a base case and a recursive call that alters the input to eventually meet the base case. Snippet D perfectly matches this definition. Snippet C fails because its recursive call moves away from the base case.
Source: MDN Web Docs
"Functions - Recursion".
2. Eloquent JavaScript
3rd Edition by Marijn Haverbeke (A resource used in many university curricula): Chapter 3
"Functions
" section on "Recursion
" explains that a function calling itself must be careful not to "run out of stack space." This happens when the recursion is too deep or infinite. Snippet C is an example of this flaw
while Snippet D is presented as a correct implementation.
Source: Haverbeke
M. (2018). Eloquent JavaScript (3rd ed.). No Starch Press. Chapter 3
"Recursion".
3. Stanford University
CS106B Courseware
"Recursive Functions": Course materials emphasize that a recursive solution must solve a "smaller
simpler" version of the same problem. Snippet D does this by calling factorial(numVar - 1). Snippet A
assuming the typo is corrected to startNumber - 1
also follows this principle. Snippet C violates this by attempting to solve a "larger" problem (numVar + 1).
Source: Stanford University
CS106B Course Materials
"Topic 6: Recursion".
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 3A developer needs to test this function: 01 const sum3 = (arr) => ( 02 if (!arr.length) r…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
