Understanding Recursion
- 77Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
Which description best defines recursion?
Which description best defines recursion?
77 questions
- 1
Which description best defines recursion?
- A
A method solves a problem by repeating a fixed sequence of loop instructions
- B
A method stores every intermediate result in an external data structure
- C
A method divides a problem into parts that are always solved independently
- D
A method solves a problem by calling itself on a progressively smaller or otherwise advanced subproblem
Correct answer
DA method solves a problem by calling itself on a progressively smaller or otherwise advanced subproblem
Explanation
Recursion is a method calling itself, typically with an input that moves the problem closer to a stopping condition.
- A
- 2
When designing a recursive solution, which two questions form the basic blueprint?
- A
What is the smallest useful work, and what condition stops further calls?
- B
How can every case be handled in one call, and how can output be printed?
- C
Which data structure is largest, and where should it be stored?
- D
Which loop is fastest, and how many variables should it use?
Correct answer
AWhat is the smallest useful work, and what condition stops further calls?
Explanation
A recursive design identifies a small unit of work that advances the solution and a base case that stops recursion.
- A
- 3
In the line-counting example, why does each person ask the person ahead rather than count the entire line?
- A
Each person can ask the person behind them to return a complete count immediately
- B
Each person can skip the people ahead and infer the answer from the last person
- C
Each person can count everyone behind them and subtract that number from the total
- D
Each person can solve a smaller version of the same question and add one for themselves
Correct answer
DEach person can solve a smaller version of the same question and add one for themselves
Explanation
Each person delegates the smaller problem to the next person in line, then adds one to that person's count.
- A
- 4
Why must a recursive call usually receive a changed parameter?
- A
The changed parameter prevents the method from returning a value
- B
The changed parameter makes the call stack unnecessary
- C
The changed parameter should move the call closer to the base case
- D
The changed parameter ensures that every call uses a different method
Correct answer
CThe changed parameter should move the call closer to the base case
Explanation
A recursive call needs to progress toward the stopping condition; repeating the same state without a stopping condition can continue indefinitely.
- A
- 5
In the essay-revision example, why can recursion apply even though each call handles the same essay object?
- A
The recursive call happens only after the professor has finished grading
- B
Each revision changes the essay's state and moves it closer to needing no further revisions
- C
Recursion requires every call to create a completely different object
- D
The essay is considered complete as soon as the first revision begins
Correct answer
BEach revision changes the essay's state and moves it closer to needing no further revisions
Explanation
The input object can remain the same while its state changes; each revision advances it toward the base case of being accepted.
- A
- 6
Why can recursion make algorithms on trees or graphs more concise?
- A
Their recursive structure lets a method handle one smaller part at a time
- B
They eliminate the need to check conditions or handle stopping cases
- C
They always contain fewer values than arrays
- D
They require every traversal to use fewer method calls than a loop
Correct answer
ATheir recursive structure lets a method handle one smaller part at a time
Explanation
Recursively defined structures can be handled one node or substructure at a time, often avoiding complicated loops and bookkeeping.
- A
- 7
What is one potential performance cost of recursive method calls compared with iteration?
- A
Each call prevents the program from storing local variables
- B
Each call necessarily scans the entire input from the beginning
- C
Each call can add method-call overhead and consume call-stack memory
- D
Each call makes all operations take constant time
Correct answer
CEach call can add method-call overhead and consume call-stack memory
Explanation
Recursive calls use stack frames and incur method-call overhead, which can make them slower or more memory-intensive than iteration.
- A
- 8
How can recursion reduce the need for a separately managed stack in an algorithm?
- A
The program's call stack implicitly stores pending calls and their local state
- B
A return value stores every pending method invocation
- C
The base case converts every recursive call into a loop
- D
A recursive method automatically removes all intermediate data from memory
Correct answer
AThe program's call stack implicitly stores pending calls and their local state
Explanation
The call stack manages pending invocations and their local variables, providing an implicit stack that some algorithms can use.
- A
- 9
What can happen if recursive calls keep accumulating without reaching a base case?
- A
The calls are automatically replaced by a faster loop
- B
The call stack can exceed its available memory and cause a stack overflow
- C
The program skips directly to the final return value
- D
The method's parameters are combined into one shared frame
Correct answer
BThe call stack can exceed its available memory and cause a stack overflow
Explanation
Each unfinished call occupies a stack frame. If calls continue to accumulate, they can exceed the program's allocated stack space.
- A
- 10
When is recursion more likely to create unnecessary complexity?
- A
When a problem does not break naturally into meaningful smaller subproblems
- B
When a recursive call makes progress toward its base case
- C
When a problem has a clear stopping condition
- D
When a method processes one node of a recursive data structure
Correct answer
AWhen a problem does not break naturally into meaningful smaller subproblems
Explanation
Recursion is not automatically a good fit; forcing it onto a problem without meaningful subproblems can make code unnecessarily complex.
- A
- 11
What does a call-stack frame typically help a program keep track of?
- A
The complete history of every loop iteration in the program
- B
Only the final output of the entire program
- C
Only the memory addresses of values in global variables
- D
The current method invocation, its parameters, local variables, and return context
Correct answer
DThe current method invocation, its parameters, local variables, and return context
Explanation
A stack frame represents an active method invocation and contains information such as its parameters, local variables, and where execution should return.
- A
- 12
In a chain where method A depends on B and B depends on C, when can A finish its result?
- A
After C returns and B has used C's result
- B
As soon as A starts, before B is called
- C
Only after the program has removed every stack frame
- D
Before C begins, because A can infer C's return value
Correct answer
AAfter C returns and B has used C's result
Explanation
A cannot complete its dependent work until B returns, and B cannot complete until C returns.
- A
- 13
Why does a method call made during another method's execution get handled before the caller finishes?
- A
The called method always returns before its own instructions execute
- B
The caller's local variables are discarded as soon as the call begins
- C
The new method permanently replaces the caller on the stack
- D
The caller is suspended while the called method runs, then resumes when that call returns
Correct answer
DThe caller is suspended while the called method runs, then resumes when that call returns
Explanation
A caller with unfinished work remains suspended. When the called method returns, execution resumes in the caller.
- A
- 14
What happens to a recursive call chain after its deepest call reaches a base case?
- A
Calls return in reverse order, allowing each waiting call to finish
- B
The deepest call is removed while the others remain permanently suspended
- C
All calls restart from the original input
- D
The calls are reevaluated from the first call in forward order
Correct answer
ACalls return in reverse order, allowing each waiting call to finish
Explanation
Once the deepest call returns, its caller can continue; this unwinding proceeds back through the waiting frames.
- A
- 15
Why can an endlessly self-calling method cause a stack overflow?
- A
The method repeatedly clears its local variables
- B
The method skips all memory allocation during execution
- C
Each call starts another invocation before earlier calls can return
- D
Each call immediately removes the frame beneath it
Correct answer
CEach call starts another invocation before earlier calls can return
Explanation
Without a stopping condition, earlier invocations remain unfinished while new frames are added.
- A
- 16
What role does a base case play in controlling the call stack?
- A
It stores the output permanently in the method parameters
- B
It stops further recursive calls so existing frames can return and be removed
- C
It ensures that every recursive call uses the original input
- D
It creates a new stack frame for every possible input
Correct answer
BIt stops further recursive calls so existing frames can return and be removed
Explanation
A base case prevents another recursive call, allowing the active calls to return and the stack to shrink.
- A
- 17
For a recursive string-reversal method, why is the empty string a valid base case?
- A
Reversing an empty string produces the empty string, so no further work is needed
- B
An empty string cannot be passed as a method parameter
- C
The empty string is already in reverse order only when its length is one
- D
An empty string contains one character that must be moved
Correct answer
AReversing an empty string produces the empty string, so no further work is needed
Explanation
The empty string is unchanged by reversal, making it a minimal input that can be returned immediately.
- A
- 18
Which recursive strategy reverses a string by removing one character at a time?
- A
Reverse the suffix after the first character, then append the first character
- B
Sort the characters, then reverse their alphabetical order
- C
Copy the first character repeatedly until the string is empty
- D
Reverse the prefix before the last character, then append the last character
Correct answer
AReverse the suffix after the first character, then append the first character
Explanation
The recursive call reverses the string without its first character; appending that first character afterward places it at the end.
- A
- 19
Why does appending each removed first character after the recursive result produce a reversed string?
- A
The removed characters are reattached in the opposite order from which they were removed
- B
The base case moves every character to the beginning at once
- C
The method reverses the order of characters before each recursive call
- D
The recursive call sorts the remaining characters before returning
Correct answer
AThe removed characters are reattached in the opposite order from which they were removed
Explanation
Calls remove characters from the front, then return in reverse order; appending each saved character places the original first character last.
- A
- 20
During recursive reversal, where are the characters removed by earlier calls kept while deeper calls run?
- A
In a sorted copy of the input string
- B
In the local state of the waiting call-stack frames
- C
In the empty-string base case
- D
In a mandatory global array shared by every call
Correct answer
BIn the local state of the waiting call-stack frames
Explanation
Each waiting frame retains its own pending character and operation until the recursive call returns.
- A
- 21
How can a recursive palindrome check reduce its input after matching the outer characters?
- A
It removes the last character but keeps the first unchanged
- B
It checks the substring between the first and last characters
- C
It checks only the first half and discards the middle
- D
It sorts the substring and compares it with the original
Correct answer
BIt checks the substring between the first and last characters
Explanation
After the outer characters match, the remaining question is whether the interior substring is a palindrome.
- A
- 22
What should a recursive palindrome check do as soon as the outer characters do not match?
- A
Return true if the string has an odd number of characters
- B
Reverse the string and compare only its length
- C
Return false, because the palindrome condition has already been violated
- D
Continue inward, because later matches can cancel the mismatch
Correct answer
CReturn false, because the palindrome condition has already been violated
Explanation
A palindrome must match at every pair of opposite positions, so one mismatch is enough to reject it.
- A
- 23
Why are strings of length zero or one suitable successful base cases for a palindrome check?
- A
They always contain matching first and last characters at different positions
- B
They cannot be reduced by any recursive call
- C
They must be compared with a second string before checking
- D
They have no pair of distinct outer characters that can violate the palindrome condition
Correct answer
DThey have no pair of distinct outer characters that can violate the palindrome condition
Explanation
An empty string has no mismatching pair, and a one-character string reads the same forward and backward.
- A
- 24
How does a mismatch discovered deep in a recursive palindrome check affect the original call?
- A
False returns through the waiting calls until the original call also returns false
- B
The mismatch changes the string into its reversed form
- C
The original call returns true if its first comparison matched
- D
The mismatch is ignored when the call stack unwinds
Correct answer
AFalse returns through the waiting calls until the original call also returns false
Explanation
A false result from a deeper substring propagates back through the recursive calls to the initial invocation.
- A
- 25
In the repeated-division method for converting a positive decimal integer to binary, what information does each remainder provide?
- A
The final binary value before any divisions are performed
- B
The position of the next decimal digit in the original input
- C
The next binary digit associated with whether the current value is even or odd
- D
The number of binary digits that remain to be produced
Correct answer
CThe next binary digit associated with whether the current value is even or odd
Explanation
Division by two produces a remainder of zero or one, which contributes a binary digit and reflects whether the value is even or odd.
- A
- 26
Why does the recursive decimal-to-binary method divide the current value by two?
- A
Each division reduces the problem while producing a binary digit as its remainder
- B
Division by two converts the entire decimal number to binary in one step
- C
Each division increases the value until it reaches the base case
- D
Division by two removes the need to track any remainders
Correct answer
AEach division reduces the problem while producing a binary digit as its remainder
Explanation
The quotient becomes a smaller input for the next call, and the remainder is retained as part of the binary result.
- A
- 27
In the described decimal-to-binary recursion, what stopping condition ends further division?
- A
The current remainder becomes one
- B
The current decimal value reaches zero
- C
The binary result contains exactly one digit
- D
The current decimal value becomes odd
Correct answer
BThe current decimal value reaches zero
Explanation
Once the value reaches zero, there is no further division to perform, so the recursive process stops.
- A
- 28
Why does the described binary-conversion method prepend each new remainder to its result?
- A
It turns each remainder back into a decimal digit
- B
It ensures that the base case is reached more slowly
- C
It removes the need to divide the input by two
- D
It places the remainders in the order needed to form the binary string
Correct answer
DIt places the remainders in the order needed to form the binary string
Explanation
The method constructs the output string by placing each remainder at the front of the result.
- A
- 29
Which recursive relationship describes the sum of the natural numbers from one through n?
- A
S(n)=S(n)+1, with S(1)=0
- B
S(n)=n+S(n−1), with S(1)=1
- C
S(n)=n×S(n−1), with S(1)=1
- D
S(n)=n+S(n+1), with S(1)=1
Correct answer
BS(n)=n+S(n−1), with S(1)=1
Explanation
The sum through n consists of n plus the sum through the preceding number; the described base case returns one for an input of one.
- A
- 30
What value does recursive summation return for an input of ten?
- A
50
- B
45
- C
100
- D
55
Correct answer
D55
Explanation
Adding the natural numbers from one through ten gives 55.
- A
- 31
In recursive summation, why does a call with a larger input wait before it can return its answer?
- A
It waits for a later input to be added to the total
- B
It must repeat its own addition until the input becomes zero
- C
It needs the result of summing all values through the preceding input
- D
It cannot return until every earlier stack frame is deleted
Correct answer
CIt needs the result of summing all values through the preceding input
Explanation
Each call adds its current value to the result returned by the recursive call on the smaller input.
- A
- 32
Which rule defines the Fibonacci values described in the content?
- A
Each value is the product of the two preceding values
- B
Each value is the sum of the current index and the preceding value
- C
Each value is twice the value two positions earlier
- D
Each value after the two initial values is the sum of the two preceding values
Correct answer
DEach value after the two initial values is the sum of the two preceding values
Explanation
The Fibonacci recurrence forms each later value by adding the two values immediately before it.
- A
- 33
What are the two Fibonacci base-case values described?
- A
F(0)=0 and F(1)=1
- B
F(0)=1 and F(1)=1
- C
F(0)=0 and F(1)=0
- D
F(0)=1 and F(1)=2
Correct answer
AF(0)=0 and F(1)=1
Explanation
The described implementation returns zero at index zero and one at index one.
- A
- 34
In the straightforward recursive Fibonacci method, when is the call for the second preceding value evaluated?
- A
After the first recursive call has returned its result
- B
Before the first recursive call begins
- C
At the same time as the first call, without waiting
- D
Only after the original call reaches its base case
Correct answer
AAfter the first recursive call has returned its result
Explanation
The first recursive expression must finish before its result can be added to the result of the second recursive call.
- A
- 35
Why is the straightforward recursive Fibonacci method inefficient for large inputs?
- A
It recomputes the same Fibonacci values in multiple branches
- B
Its base cases require the entire sequence to be generated first
- C
It stores every result in a constant-time cache before computing it
- D
It avoids making recursive calls for repeated subproblems
Correct answer
AIt recomputes the same Fibonacci values in multiple branches
Explanation
Different branches request overlapping values, so the basic implementation performs redundant calculations.
- A
- 36
What makes binary search applicable to the array described?
- A
The array has an even number of elements, so it has no midpoint
- B
The values are sorted, so comparisons can eliminate one side of the search range
- C
The values are unsorted, so every half can be checked independently
- D
The target is guaranteed to be at the first or last index
Correct answer
BThe values are sorted, so comparisons can eliminate one side of the search range
Explanation
Sorted order lets binary search determine which half can contain the target and discard the other half.
- A
- 37
After comparing the target with the midpoint in binary search, how does the method shrink its search space?
- A
It sorts the current range again before searching it
- B
It keeps the half that could contain the target and discards the other half
- C
It moves the midpoint one position toward the beginning regardless of the comparison
- D
It scans both halves in full before making another call
Correct answer
BIt keeps the half that could contain the target and discards the other half
Explanation
Because the array is sorted, the comparison identifies which half may contain the target.
- A
- 38
If the target is greater than the midpoint value in an ascending sorted array, which range should binary search examine next?
- A
The entire original range, including the midpoint
- B
The range to the right of the midpoint
- C
Only the midpoint and the first element
- D
The range to the left of the midpoint
Correct answer
BThe range to the right of the midpoint
Explanation
In ascending order, values greater than the midpoint must lie to its right.
- A
- 39
If the target is less than the midpoint value in an ascending sorted array, how should binary search change its bounds?
- A
Move the left bound to just after the midpoint and keep the right bound
- B
Move both bounds to the far right of the array
- C
Keep the left bound and move the right bound to just before the midpoint
- D
Keep both bounds unchanged and search the midpoint again
Correct answer
CKeep the left bound and move the right bound to just before the midpoint
Explanation
The left portion may contain a smaller target, while the midpoint and right portion can be discarded.
- A
- 40
What should binary search return when the midpoint value equals the target?
- A
The midpoint value multiplied by the array length
- B
The index of the matching value
- C
The first value in the original array
- D
The number of values remaining in the search range
Correct answer
BThe index of the matching value
Explanation
The described search returns the position, or index, at which the target is found.
- A
- 41
What are the three central stages of divide-and-conquer algorithms as described?
- A
Create a loop, store every state, and return the first result
- B
Sort the input, discard the result, and restart the problem
- C
Divide the problem, solve the subproblems, and merge their results
- D
Copy the problem, solve one part, and ignore the remaining parts
Correct answer
CDivide the problem, solve the subproblems, and merge their results
Explanation
Divide and conquer splits a large problem into smaller problems, solves them, and combines their results into an overall solution.
- A
- 42
What is the merge-sort stopping condition described for a subarray?
- A
The subarray contains two values in descending order
- B
The subarray midpoint equals the first index of the whole input
- C
The subarray has one value or its bounds no longer describe work to split
- D
The subarray has been copied into a temporary array once
Correct answer
CThe subarray has one value or its bounds no longer describe work to split
Explanation
A single value is already sorted, so merge sort stops dividing when the subproblem can no longer be split usefully.
- A
- 43
In the described recursive merge sort, why is the right half processed after the left half?
- A
The left recursive call must finish before execution continues to the right call
- B
The left half is discarded as soon as its first value is examined
- C
The algorithm compares the right half only after it reaches the final array index
- D
The right half cannot be sorted until the original array is copied twice
Correct answer
AThe left recursive call must finish before execution continues to the right call
Explanation
Recursive execution follows the call order: the left subproblem completes before the method proceeds to the right subproblem.
- A
- 44
How does merge sort combine two sorted subarrays?
- A
Compare only the first values and then concatenate both subarrays unchanged
- B
Choose values at random and sort them after copying
- C
Repeatedly compare their next unmerged values and place the smaller value next
- D
Append the entire right subarray before the entire left subarray
Correct answer
CRepeatedly compare their next unmerged values and place the smaller value next
Explanation
A linear comparison of the next available values from each sorted subarray produces a merged sequence in sorted order.
- A
- 45
During a merge, what should happen when one subarray runs out of values to compare?
- A
Restart the merge from the beginning of both subarrays
- B
Copy the remaining values from the other subarray into the result
- C
Move the remaining values to the end without preserving their order
- D
Discard the remaining values because they are already sorted
Correct answer
BCopy the remaining values from the other subarray into the result
Explanation
The unexhausted subarray is already sorted, so its remaining values can be copied into the result in order.
- A
- 46
Why does the described merge operation use a temporary array?
- A
It stores only the midpoint indices for later recursive calls
- B
It ensures that the original array never receives the sorted result
- C
It reverses both subarrays before they are compared
- D
It holds merged values without overwriting input values that are still needed for comparison
Correct answer
DIt holds merged values without overwriting input values that are still needed for comparison
Explanation
A temporary buffer protects the original values while the two subarrays are being compared and merged.
- A
- 47
After merging values into a temporary array, which part of the original array should be updated?
- A
Every position outside the current recursive subproblem
- B
The subsection covered by the current merge's start and end bounds
- C
The entire array, even when the merge covers only a subsection
- D
Only the first position of the entire array
Correct answer
BThe subsection covered by the current merge's start and end bounds
Explanation
Each recursive merge updates only the range it is responsible for, copying the temporary result back into those positions.
- A
- 48
In recursive linked-list reversal, which inputs can be returned immediately as base cases?
- A
A null head or a head whose next node is null
- B
A head whose next node has a different value
- C
A head with exactly two following nodes
- D
A head whose value is smaller than its next node
Correct answer
AA null head or a head whose next node is null
Explanation
An empty list and a one-node list are already reversed, so the method can return them directly.
- A
- 49
What does the recursive call in linked-list reversal typically receive?
- A
The node after the current head
- B
The node before the current head
- C
A copy of the entire list in reverse order
- D
The final node and no information about the remaining list
Correct answer
AThe node after the current head
Explanation
The method recurses on the next node, advancing toward the end of the list and the base case.
- A
- 50
When does the described linked-list reversal change the links between nodes?
- A
Before the first recursive call, by swapping every node's value
- B
When the base case changes the final node's value
- C
Only after the original head has been returned to the caller
- D
As recursive calls return, each node links its successor back to itself
Correct answer
DAs recursive calls return, each node links its successor back to itself
Explanation
The recursive calls reach the end first; during unwinding, each node redirects its successor's link back toward itself.
- A
- 51
Why must the old forward link be cleared during the described reversal?
- A
The old link would prevent the base case from being reached
- B
Otherwise the reversed links can form a cycle between adjacent nodes
- C
The old link would make every node's value null
- D
Otherwise the list cannot contain a new head node
Correct answer
BOtherwise the reversed links can form a cycle between adjacent nodes
Explanation
After the successor points back to the current node, leaving the original forward link would create a cycle.
- A
- 52
What does the recursive reversal return to the original caller?
- A
The first null link encountered, without a node reference
- B
The original head, which remains the list's first node
- C
The original last node, which becomes the new head
- D
A new node containing a copy of the original head's value
Correct answer
CThe original last node, which becomes the new head
Explanation
The base case returns the original last node, and that reference is propagated back through the recursive calls as the new head.
- A
- 53
What are suitable base cases for recursively merging two sorted linked lists?
- A
If either list has two nodes, return its first node
- B
If both lists have different lengths, return the longer list
- C
If the first values match, return null
- D
If either list is null, return the other list
Correct answer
DIf either list is null, return the other list
Explanation
Merging an empty list with another list leaves the nonempty list unchanged; if both are empty, returning the other null reference also works.
- A
- 54
How does recursive merge choose the next node from two sorted linked lists?
- A
It selects the smaller current value and recursively merges the rest
- B
It alternates nodes from the lists without comparing their values
- C
It selects the node with more successors and returns the other list
- D
It selects the larger current value and discards the smaller one
Correct answer
AIt selects the smaller current value and recursively merges the rest
Explanation
Since both lists are sorted, the smaller of their current head values belongs next in the merged list.
- A
- 55
Where is the result of each recursive merge call used?
- A
As the left and right bounds of an array search
- B
As the value stored in a newly created copy of the entire list
- C
As the next pointer of the smaller node selected at that call
- D
As a replacement for the visited set
Correct answer
CAs the next pointer of the smaller node selected at that call
Explanation
The selected node's next pointer is set to the result of merging the remaining list portions.
- A
- 56
Why can merging sorted linked lists produce a sorted result without sorting every node again?
- A
Each list is reversed before its values are compared
- B
The base case sorts all remaining nodes in one operation
- C
The recursive calls place all larger values before smaller ones
- D
The smaller current head is always the smallest remaining value across the two lists
Correct answer
DThe smaller current head is always the smallest remaining value across the two lists
Explanation
Because each input list is already sorted, comparing their current heads identifies the smallest remaining value.
- A
- 57
What ordering does the described merge of two sorted linked lists produce?
- A
A single list with values in alternating ascending and descending order
- B
A single list in ascending order
- C
Two lists whose original order is preserved separately
- D
A single list in the reverse of both input orders
Correct answer
BA single list in ascending order
Explanation
Repeatedly choosing the smaller head and connecting the recursive result yields one ascending sorted list.
- A
- 58
What ordering property defines a binary search tree in the described example?
- A
All values in the tree must be larger than the root
- B
Values in a node's left subtree are smaller, and values in its right subtree are larger
- C
Values at every level must be arranged from largest to smallest
- D
Every node must have exactly two children with equal values
Correct answer
BValues in a node's left subtree are smaller, and values in its right subtree are larger
Explanation
The binary search tree property places smaller values to the left and larger values to the right, and this ordering applies within subtrees as well.
- A
- 59
When inserting a value into a binary search tree, what should happen if the current node is null?
- A
Replace the root with the value, regardless of the current subtree
- B
Restart the search from the opposite side of the root
- C
Discard the value because it has no comparison partner
- D
Create and return a new node at that position
Correct answer
DCreate and return a new node at that position
Explanation
Reaching a null child means the correct insertion position has been found; a new node can be placed there.
- A
- 60
How does a recursive binary search tree insertion choose its next subtree?
- A
It chooses the subtree with fewer nodes without comparing values
- B
It always recurses left until reaching a null node
- C
It inserts the value at the root and moves the old root downward
- D
It compares the inserted value with the current node and recurses left or right accordingly
Correct answer
DIt compares the inserted value with the current node and recurses left or right accordingly
Explanation
A value smaller than the current node is directed left; a larger value is directed right.
- A
- 61
Why does a recursive insertion return the original root after inserting a node below it?
- A
The root contains a copy of every node in the tree
- B
The root still represents the entry point to the updated tree
- C
Returning the root removes the recursive calls from memory before insertion
- D
The newly inserted leaf must replace the root
Correct answer
BThe root still represents the entry point to the updated tree
Explanation
The insertion updates links along the path but leaves the original root as the tree's entry point.
- A
- 62
What property identifies a leaf node when recursively printing tree leaves?
- A
It has no left child and no right child
- B
It has exactly one child
- C
It has a parent but no stored value
- D
It is the root, regardless of its children
Correct answer
AIt has no left child and no right child
Explanation
A leaf is a node with no children, so both its left and right child references are absent.
- A
- 63
In the described left-to-right depth-first traversal of a tree, when is the right subtree explored?
- A
Only if the left subtree contains no leaf nodes
- B
Before the left subtree is visited
- C
At the same time as the left subtree, independently
- D
After the left subtree has been fully explored
Correct answer
DAfter the left subtree has been fully explored
Explanation
The traversal completes the left recursive exploration before proceeding to the right subtree.
- A
- 64
Why does graph depth-first search maintain a visited set?
- A
To replace the graph's links with a tree structure
- B
To ensure that every neighbor is explored more than once
- C
To avoid revisiting nodes and becoming trapped by cycles
- D
To sort all neighboring nodes before exploring them
Correct answer
CTo avoid revisiting nodes and becoming trapped by cycles
Explanation
Graphs can contain cycles, so recording visited nodes prevents the search from repeatedly following the same nodes.
- A
- 65
How does depth-first search behave when a node has several unvisited neighbors?
- A
It always chooses the neighbor with the largest value
- B
It returns to the starting node after each single edge
- C
It visits all neighbors at once and then stops
- D
It explores one neighbor's path deeply before moving to another neighbor
Correct answer
DIt explores one neighbor's path deeply before moving to another neighbor
Explanation
Depth-first search follows one path as far as possible before backtracking to explore other neighbors.
- A
- 66
In the described graph search, what should happen when the current node equals the goal?
- A
Restart the search with an empty visited set
- B
Remove the node from the graph before continuing
- C
Return true immediately
- D
Return false and continue searching its neighbors
Correct answer
CReturn true immediately
Explanation
A node matching the goal means the sought value has been found, so the search can return true.
- A
- 67
What should graph search return if it finishes exploring all reachable neighbors without finding the goal?
- A
True, because every neighbor was examined
- B
The original starting node
- C
False
- D
The value of the last visited node
Correct answer
CFalse
Explanation
If the traversal exhausts the reachable nodes without finding the goal, the goal was not found in that search.
- A
- 68
What is memoization intended to do in a recursive algorithm?
- A
Add more recursive calls to ensure every result is independently recomputed
- B
Replace every base case with a loop
- C
Store previously computed results so repeated subproblems can reuse them
- D
Store only the input values that caused a stack overflow
Correct answer
CStore previously computed results so repeated subproblems can reuse them
Explanation
Memoization caches results so a later call with the same subproblem can return the stored answer instead of recomputing it.
- A
- 69
How does the described memoized Fibonacci method handle a value already in its cache?
- A
It stores a second copy and continues recursing
- B
It changes the base case to the cached value's index
- C
It returns the cached value without making the recursive calculation again
- D
It deletes the value and recomputes both recursive branches
Correct answer
CIt returns the cached value without making the recursive calculation again
Explanation
The method checks the cache first and immediately reuses a result that has already been computed.
- A
- 70
Why does memoizing Fibonacci reduce redundant computation?
- A
It makes every call compute both branches simultaneously
- B
It prevents the algorithm from making any recursive calls
- C
It changes every Fibonacci value into a base case
- D
A value such as F(2) can be computed once and reused by later calls
Correct answer
DA value such as F(2) can be computed once and reused by later calls
Explanation
Overlapping Fibonacci subproblems occur repeatedly; storing their results avoids recalculating the same values.
- A
- 71
What does the content state about retrieving a value from the described hashmap cache?
- A
It is available only after the program has returned from all calls
- B
It provides constant-time access, described as O(1)
- C
It takes time proportional to the number of recursive calls
- D
It requires scanning the entire map in every case
Correct answer
BIt provides constant-time access, described as O(1)
Explanation
The described hashmap uses hashing to provide constant-time access to stored values.
- A
- 72
What should a memoized recursive method do after computing a result that was not already cached?
- A
Clear the cache before returning the result
- B
Store the result in the cache and then return it
- C
Discard the result so the next call can recompute it
- D
Store only the input and return a base-case value
Correct answer
BStore the result in the cache and then return it
Explanation
Saving each newly computed result lets subsequent calls reuse it.
- A
- 73
Why can a tail-recursive call sometimes allow a compiler to reduce call-stack growth?
- A
The recursive call is the final operation, so no pending work is needed afterward
- B
The method makes no recursive calls at all
- C
The recursive call always runs before the current method begins
- D
The method stores every result in a global variable
Correct answer
AThe recursive call is the final operation, so no pending work is needed afterward
Explanation
When the recursive call is the final operation, an eligible compiler can reuse a frame because the caller has no additional work to perform.
- A
- 74
Why is the described factorial method that returns a value multiplied by a recursive call not tail recursive?
- A
Multiplication remains to be performed after the recursive call returns
- B
It reaches a base case before making any call
- C
Its final result is only a recursive call
- D
It passes an accumulator as an additional parameter
Correct answer
AMultiplication remains to be performed after the recursive call returns
Explanation
The caller must still multiply the returned recursive result, so the recursive call is not the final operation.
- A
- 75
How can an accumulator-style factorial method make its recursive call tail recursive?
- A
Create a second recursive call for every multiplication
- B
Move the base case after the final multiplication
- C
Multiply the recursive result after it returns from the next call
- D
Pass the accumulated result into the next call and return that call directly
Correct answer
DPass the accumulated result into the next call and return that call directly
Explanation
An accumulator carries the pending product forward, allowing the method to return the recursive call directly.
- A
- 76
Which statement about tail-call optimization support is consistent with the content?
- A
Only Java and Python guarantee tail-call optimization
- B
Support depends on the language and execution environment and is not universal
- C
Tail-call optimization is performed by the operating system for every method
- D
Every programming language always optimizes tail-recursive calls
Correct answer
BSupport depends on the language and execution environment and is not universal
Explanation
The content notes that support varies; it is common in some functional languages but not inherent across languages such as Python, Java, and JavaScript.
- A
- 77
What is the central idea of backtracking as described?
- A
Continue along one choice even after it has failed
- B
Sort all possible choices before making a single decision
- C
Cache every result and never revisit a previous decision
- D
Explore choices recursively, evaluate whether they work, and return to try alternatives when needed
Correct answer
DExplore choices recursively, evaluate whether they work, and return to try alternatives when needed
Explanation
Backtracking recursively explores a decision space and returns to earlier choices when a path does not lead to a solution.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.