1. Home
  2. Explore
  3. Computer Science
  4. Understanding Recursion

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Understanding Recursion

  • 77Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion Foundations

Which description best defines recursion?

77 questions

  1. 1

    Which description best defines recursion?

    1. A

      A method solves a problem by repeating a fixed sequence of loop instructions

    2. B

      A method stores every intermediate result in an external data structure

    3. C

      A method divides a problem into parts that are always solved independently

    4. D

      A method solves a problem by calling itself on a progressively smaller or otherwise advanced subproblem

    Correct answer
    D

    A 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.

  2. 2

    When designing a recursive solution, which two questions form the basic blueprint?

    1. A

      What is the smallest useful work, and what condition stops further calls?

    2. B

      How can every case be handled in one call, and how can output be printed?

    3. C

      Which data structure is largest, and where should it be stored?

    4. D

      Which loop is fastest, and how many variables should it use?

    Correct answer
    A

    What 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.

  3. 3

    In the line-counting example, why does each person ask the person ahead rather than count the entire line?

    1. A

      Each person can ask the person behind them to return a complete count immediately

    2. B

      Each person can skip the people ahead and infer the answer from the last person

    3. C

      Each person can count everyone behind them and subtract that number from the total

    4. D

      Each person can solve a smaller version of the same question and add one for themselves

    Correct answer
    D

    Each 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.

  4. 4

    Why must a recursive call usually receive a changed parameter?

    1. A

      The changed parameter prevents the method from returning a value

    2. B

      The changed parameter makes the call stack unnecessary

    3. C

      The changed parameter should move the call closer to the base case

    4. D

      The changed parameter ensures that every call uses a different method

    Correct answer
    C

    The 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.

  5. 5

    In the essay-revision example, why can recursion apply even though each call handles the same essay object?

    1. A

      The recursive call happens only after the professor has finished grading

    2. B

      Each revision changes the essay's state and moves it closer to needing no further revisions

    3. C

      Recursion requires every call to create a completely different object

    4. D

      The essay is considered complete as soon as the first revision begins

    Correct answer
    B

    Each 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.

  6. 6

    Why can recursion make algorithms on trees or graphs more concise?

    1. A

      Their recursive structure lets a method handle one smaller part at a time

    2. B

      They eliminate the need to check conditions or handle stopping cases

    3. C

      They always contain fewer values than arrays

    4. D

      They require every traversal to use fewer method calls than a loop

    Correct answer
    A

    Their 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.

  7. 7

    What is one potential performance cost of recursive method calls compared with iteration?

    1. A

      Each call prevents the program from storing local variables

    2. B

      Each call necessarily scans the entire input from the beginning

    3. C

      Each call can add method-call overhead and consume call-stack memory

    4. D

      Each call makes all operations take constant time

    Correct answer
    C

    Each 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.

  8. 8

    How can recursion reduce the need for a separately managed stack in an algorithm?

    1. A

      The program's call stack implicitly stores pending calls and their local state

    2. B

      A return value stores every pending method invocation

    3. C

      The base case converts every recursive call into a loop

    4. D

      A recursive method automatically removes all intermediate data from memory

    Correct answer
    A

    The 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.

  9. 9

    What can happen if recursive calls keep accumulating without reaching a base case?

    1. A

      The calls are automatically replaced by a faster loop

    2. B

      The call stack can exceed its available memory and cause a stack overflow

    3. C

      The program skips directly to the final return value

    4. D

      The method's parameters are combined into one shared frame

    Correct answer
    B

    The 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.

  10. 10

    When is recursion more likely to create unnecessary complexity?

    1. A

      When a problem does not break naturally into meaningful smaller subproblems

    2. B

      When a recursive call makes progress toward its base case

    3. C

      When a problem has a clear stopping condition

    4. D

      When a method processes one node of a recursive data structure

    Correct answer
    A

    When 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.

  11. 11

    What does a call-stack frame typically help a program keep track of?

    1. A

      The complete history of every loop iteration in the program

    2. B

      Only the final output of the entire program

    3. C

      Only the memory addresses of values in global variables

    4. D

      The current method invocation, its parameters, local variables, and return context

    Correct answer
    D

    The 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.

  12. 12

    In a chain where method A depends on B and B depends on C, when can A finish its result?

    1. A

      After C returns and B has used C's result

    2. B

      As soon as A starts, before B is called

    3. C

      Only after the program has removed every stack frame

    4. D

      Before C begins, because A can infer C's return value

    Correct answer
    A

    After 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.

  13. 13

    Why does a method call made during another method's execution get handled before the caller finishes?

    1. A

      The called method always returns before its own instructions execute

    2. B

      The caller's local variables are discarded as soon as the call begins

    3. C

      The new method permanently replaces the caller on the stack

    4. D

      The caller is suspended while the called method runs, then resumes when that call returns

    Correct answer
    D

    The 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.

  14. 14

    What happens to a recursive call chain after its deepest call reaches a base case?

    1. A

      Calls return in reverse order, allowing each waiting call to finish

    2. B

      The deepest call is removed while the others remain permanently suspended

    3. C

      All calls restart from the original input

    4. D

      The calls are reevaluated from the first call in forward order

    Correct answer
    A

    Calls 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.

  15. 15

    Why can an endlessly self-calling method cause a stack overflow?

    1. A

      The method repeatedly clears its local variables

    2. B

      The method skips all memory allocation during execution

    3. C

      Each call starts another invocation before earlier calls can return

    4. D

      Each call immediately removes the frame beneath it

    Correct answer
    C

    Each call starts another invocation before earlier calls can return

    Explanation

    Without a stopping condition, earlier invocations remain unfinished while new frames are added.

  16. 16

    What role does a base case play in controlling the call stack?

    1. A

      It stores the output permanently in the method parameters

    2. B

      It stops further recursive calls so existing frames can return and be removed

    3. C

      It ensures that every recursive call uses the original input

    4. D

      It creates a new stack frame for every possible input

    Correct answer
    B

    It 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.

  17. 17

    For a recursive string-reversal method, why is the empty string a valid base case?

    1. A

      Reversing an empty string produces the empty string, so no further work is needed

    2. B

      An empty string cannot be passed as a method parameter

    3. C

      The empty string is already in reverse order only when its length is one

    4. D

      An empty string contains one character that must be moved

    Correct answer
    A

    Reversing 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.

  18. 18

    Which recursive strategy reverses a string by removing one character at a time?

    1. A

      Reverse the suffix after the first character, then append the first character

    2. B

      Sort the characters, then reverse their alphabetical order

    3. C

      Copy the first character repeatedly until the string is empty

    4. D

      Reverse the prefix before the last character, then append the last character

    Correct answer
    A

    Reverse 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.

  19. 19

    Why does appending each removed first character after the recursive result produce a reversed string?

    1. A

      The removed characters are reattached in the opposite order from which they were removed

    2. B

      The base case moves every character to the beginning at once

    3. C

      The method reverses the order of characters before each recursive call

    4. D

      The recursive call sorts the remaining characters before returning

    Correct answer
    A

    The 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.

  20. 20

    During recursive reversal, where are the characters removed by earlier calls kept while deeper calls run?

    1. A

      In a sorted copy of the input string

    2. B

      In the local state of the waiting call-stack frames

    3. C

      In the empty-string base case

    4. D

      In a mandatory global array shared by every call

    Correct answer
    B

    In 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.

  21. 21

    How can a recursive palindrome check reduce its input after matching the outer characters?

    1. A

      It removes the last character but keeps the first unchanged

    2. B

      It checks the substring between the first and last characters

    3. C

      It checks only the first half and discards the middle

    4. D

      It sorts the substring and compares it with the original

    Correct answer
    B

    It 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.

  22. 22

    What should a recursive palindrome check do as soon as the outer characters do not match?

    1. A

      Return true if the string has an odd number of characters

    2. B

      Reverse the string and compare only its length

    3. C

      Return false, because the palindrome condition has already been violated

    4. D

      Continue inward, because later matches can cancel the mismatch

    Correct answer
    C

    Return 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.

  23. 23

    Why are strings of length zero or one suitable successful base cases for a palindrome check?

    1. A

      They always contain matching first and last characters at different positions

    2. B

      They cannot be reduced by any recursive call

    3. C

      They must be compared with a second string before checking

    4. D

      They have no pair of distinct outer characters that can violate the palindrome condition

    Correct answer
    D

    They 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.

  24. 24

    How does a mismatch discovered deep in a recursive palindrome check affect the original call?

    1. A

      False returns through the waiting calls until the original call also returns false

    2. B

      The mismatch changes the string into its reversed form

    3. C

      The original call returns true if its first comparison matched

    4. D

      The mismatch is ignored when the call stack unwinds

    Correct answer
    A

    False 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.

  25. 25

    In the repeated-division method for converting a positive decimal integer to binary, what information does each remainder provide?

    1. A

      The final binary value before any divisions are performed

    2. B

      The position of the next decimal digit in the original input

    3. C

      The next binary digit associated with whether the current value is even or odd

    4. D

      The number of binary digits that remain to be produced

    Correct answer
    C

    The 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.

  26. 26

    Why does the recursive decimal-to-binary method divide the current value by two?

    1. A

      Each division reduces the problem while producing a binary digit as its remainder

    2. B

      Division by two converts the entire decimal number to binary in one step

    3. C

      Each division increases the value until it reaches the base case

    4. D

      Division by two removes the need to track any remainders

    Correct answer
    A

    Each 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.

  27. 27

    In the described decimal-to-binary recursion, what stopping condition ends further division?

    1. A

      The current remainder becomes one

    2. B

      The current decimal value reaches zero

    3. C

      The binary result contains exactly one digit

    4. D

      The current decimal value becomes odd

    Correct answer
    B

    The current decimal value reaches zero

    Explanation

    Once the value reaches zero, there is no further division to perform, so the recursive process stops.

  28. 28

    Why does the described binary-conversion method prepend each new remainder to its result?

    1. A

      It turns each remainder back into a decimal digit

    2. B

      It ensures that the base case is reached more slowly

    3. C

      It removes the need to divide the input by two

    4. D

      It places the remainders in the order needed to form the binary string

    Correct answer
    D

    It 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.

  29. 29

    Which recursive relationship describes the sum of the natural numbers from one through nnn?

    1. A

      S(n)=S(n)+1S(n)=S(n)+1S(n)=S(n)+1, with S(1)=0S(1)=0S(1)=0

    2. B

      S(n)=n+S(n−1)S(n)=n+S(n-1)S(n)=n+S(n−1), with S(1)=1S(1)=1S(1)=1

    3. C

      S(n)=n×S(n−1)S(n)=n\times S(n-1)S(n)=n×S(n−1), with S(1)=1S(1)=1S(1)=1

    4. D

      S(n)=n+S(n+1)S(n)=n+S(n+1)S(n)=n+S(n+1), with S(1)=1S(1)=1S(1)=1

    Correct answer
    B

    S(n)=n+S(n−1)S(n)=n+S(n-1)S(n)=n+S(n−1), with S(1)=1S(1)=1S(1)=1

    Explanation

    The sum through nnn consists of nnn plus the sum through the preceding number; the described base case returns one for an input of one.

  30. 30

    What value does recursive summation return for an input of ten?

    1. A

      50

    2. B

      45

    3. C

      100

    4. D

      55

    Correct answer
    D

    55

    Explanation

    Adding the natural numbers from one through ten gives 555555.

  31. 31

    In recursive summation, why does a call with a larger input wait before it can return its answer?

    1. A

      It waits for a later input to be added to the total

    2. B

      It must repeat its own addition until the input becomes zero

    3. C

      It needs the result of summing all values through the preceding input

    4. D

      It cannot return until every earlier stack frame is deleted

    Correct answer
    C

    It 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.

  32. 32

    Which rule defines the Fibonacci values described in the content?

    1. A

      Each value is the product of the two preceding values

    2. B

      Each value is the sum of the current index and the preceding value

    3. C

      Each value is twice the value two positions earlier

    4. D

      Each value after the two initial values is the sum of the two preceding values

    Correct answer
    D

    Each 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.

  33. 33

    What are the two Fibonacci base-case values described?

    1. A

      F(0)=0F(0)=0F(0)=0 and F(1)=1F(1)=1F(1)=1

    2. B

      F(0)=1F(0)=1F(0)=1 and F(1)=1F(1)=1F(1)=1

    3. C

      F(0)=0F(0)=0F(0)=0 and F(1)=0F(1)=0F(1)=0

    4. D

      F(0)=1F(0)=1F(0)=1 and F(1)=2F(1)=2F(1)=2

    Correct answer
    A

    F(0)=0F(0)=0F(0)=0 and F(1)=1F(1)=1F(1)=1

    Explanation

    The described implementation returns zero at index zero and one at index one.

  34. 34

    In the straightforward recursive Fibonacci method, when is the call for the second preceding value evaluated?

    1. A

      After the first recursive call has returned its result

    2. B

      Before the first recursive call begins

    3. C

      At the same time as the first call, without waiting

    4. D

      Only after the original call reaches its base case

    Correct answer
    A

    After 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.

  35. 35

    Why is the straightforward recursive Fibonacci method inefficient for large inputs?

    1. A

      It recomputes the same Fibonacci values in multiple branches

    2. B

      Its base cases require the entire sequence to be generated first

    3. C

      It stores every result in a constant-time cache before computing it

    4. D

      It avoids making recursive calls for repeated subproblems

    Correct answer
    A

    It recomputes the same Fibonacci values in multiple branches

    Explanation

    Different branches request overlapping values, so the basic implementation performs redundant calculations.

  36. 36

    What makes binary search applicable to the array described?

    1. A

      The array has an even number of elements, so it has no midpoint

    2. B

      The values are sorted, so comparisons can eliminate one side of the search range

    3. C

      The values are unsorted, so every half can be checked independently

    4. D

      The target is guaranteed to be at the first or last index

    Correct answer
    B

    The 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.

  37. 37

    After comparing the target with the midpoint in binary search, how does the method shrink its search space?

    1. A

      It sorts the current range again before searching it

    2. B

      It keeps the half that could contain the target and discards the other half

    3. C

      It moves the midpoint one position toward the beginning regardless of the comparison

    4. D

      It scans both halves in full before making another call

    Correct answer
    B

    It 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.

  38. 38

    If the target is greater than the midpoint value in an ascending sorted array, which range should binary search examine next?

    1. A

      The entire original range, including the midpoint

    2. B

      The range to the right of the midpoint

    3. C

      Only the midpoint and the first element

    4. D

      The range to the left of the midpoint

    Correct answer
    B

    The range to the right of the midpoint

    Explanation

    In ascending order, values greater than the midpoint must lie to its right.

  39. 39

    If the target is less than the midpoint value in an ascending sorted array, how should binary search change its bounds?

    1. A

      Move the left bound to just after the midpoint and keep the right bound

    2. B

      Move both bounds to the far right of the array

    3. C

      Keep the left bound and move the right bound to just before the midpoint

    4. D

      Keep both bounds unchanged and search the midpoint again

    Correct answer
    C

    Keep 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.

  40. 40

    What should binary search return when the midpoint value equals the target?

    1. A

      The midpoint value multiplied by the array length

    2. B

      The index of the matching value

    3. C

      The first value in the original array

    4. D

      The number of values remaining in the search range

    Correct answer
    B

    The index of the matching value

    Explanation

    The described search returns the position, or index, at which the target is found.

  41. 41

    What are the three central stages of divide-and-conquer algorithms as described?

    1. A

      Create a loop, store every state, and return the first result

    2. B

      Sort the input, discard the result, and restart the problem

    3. C

      Divide the problem, solve the subproblems, and merge their results

    4. D

      Copy the problem, solve one part, and ignore the remaining parts

    Correct answer
    C

    Divide 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.

  42. 42

    What is the merge-sort stopping condition described for a subarray?

    1. A

      The subarray contains two values in descending order

    2. B

      The subarray midpoint equals the first index of the whole input

    3. C

      The subarray has one value or its bounds no longer describe work to split

    4. D

      The subarray has been copied into a temporary array once

    Correct answer
    C

    The 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.

  43. 43

    In the described recursive merge sort, why is the right half processed after the left half?

    1. A

      The left recursive call must finish before execution continues to the right call

    2. B

      The left half is discarded as soon as its first value is examined

    3. C

      The algorithm compares the right half only after it reaches the final array index

    4. D

      The right half cannot be sorted until the original array is copied twice

    Correct answer
    A

    The 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.

  44. 44

    How does merge sort combine two sorted subarrays?

    1. A

      Compare only the first values and then concatenate both subarrays unchanged

    2. B

      Choose values at random and sort them after copying

    3. C

      Repeatedly compare their next unmerged values and place the smaller value next

    4. D

      Append the entire right subarray before the entire left subarray

    Correct answer
    C

    Repeatedly 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.

  45. 45

    During a merge, what should happen when one subarray runs out of values to compare?

    1. A

      Restart the merge from the beginning of both subarrays

    2. B

      Copy the remaining values from the other subarray into the result

    3. C

      Move the remaining values to the end without preserving their order

    4. D

      Discard the remaining values because they are already sorted

    Correct answer
    B

    Copy 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.

  46. 46

    Why does the described merge operation use a temporary array?

    1. A

      It stores only the midpoint indices for later recursive calls

    2. B

      It ensures that the original array never receives the sorted result

    3. C

      It reverses both subarrays before they are compared

    4. D

      It holds merged values without overwriting input values that are still needed for comparison

    Correct answer
    D

    It 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.

  47. 47

    After merging values into a temporary array, which part of the original array should be updated?

    1. A

      Every position outside the current recursive subproblem

    2. B

      The subsection covered by the current merge's start and end bounds

    3. C

      The entire array, even when the merge covers only a subsection

    4. D

      Only the first position of the entire array

    Correct answer
    B

    The 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.

  48. 48

    In recursive linked-list reversal, which inputs can be returned immediately as base cases?

    1. A

      A null head or a head whose next node is null

    2. B

      A head whose next node has a different value

    3. C

      A head with exactly two following nodes

    4. D

      A head whose value is smaller than its next node

    Correct answer
    A

    A 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.

  49. 49

    What does the recursive call in linked-list reversal typically receive?

    1. A

      The node after the current head

    2. B

      The node before the current head

    3. C

      A copy of the entire list in reverse order

    4. D

      The final node and no information about the remaining list

    Correct answer
    A

    The node after the current head

    Explanation

    The method recurses on the next node, advancing toward the end of the list and the base case.

  50. 50

    When does the described linked-list reversal change the links between nodes?

    1. A

      Before the first recursive call, by swapping every node's value

    2. B

      When the base case changes the final node's value

    3. C

      Only after the original head has been returned to the caller

    4. D

      As recursive calls return, each node links its successor back to itself

    Correct answer
    D

    As 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.

  51. 51

    Why must the old forward link be cleared during the described reversal?

    1. A

      The old link would prevent the base case from being reached

    2. B

      Otherwise the reversed links can form a cycle between adjacent nodes

    3. C

      The old link would make every node's value null

    4. D

      Otherwise the list cannot contain a new head node

    Correct answer
    B

    Otherwise 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.

  52. 52

    What does the recursive reversal return to the original caller?

    1. A

      The first null link encountered, without a node reference

    2. B

      The original head, which remains the list's first node

    3. C

      The original last node, which becomes the new head

    4. D

      A new node containing a copy of the original head's value

    Correct answer
    C

    The 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.

  53. 53

    What are suitable base cases for recursively merging two sorted linked lists?

    1. A

      If either list has two nodes, return its first node

    2. B

      If both lists have different lengths, return the longer list

    3. C

      If the first values match, return null

    4. D

      If either list is null, return the other list

    Correct answer
    D

    If 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.

  54. 54

    How does recursive merge choose the next node from two sorted linked lists?

    1. A

      It selects the smaller current value and recursively merges the rest

    2. B

      It alternates nodes from the lists without comparing their values

    3. C

      It selects the node with more successors and returns the other list

    4. D

      It selects the larger current value and discards the smaller one

    Correct answer
    A

    It 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.

  55. 55

    Where is the result of each recursive merge call used?

    1. A

      As the left and right bounds of an array search

    2. B

      As the value stored in a newly created copy of the entire list

    3. C

      As the next pointer of the smaller node selected at that call

    4. D

      As a replacement for the visited set

    Correct answer
    C

    As 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.

  56. 56

    Why can merging sorted linked lists produce a sorted result without sorting every node again?

    1. A

      Each list is reversed before its values are compared

    2. B

      The base case sorts all remaining nodes in one operation

    3. C

      The recursive calls place all larger values before smaller ones

    4. D

      The smaller current head is always the smallest remaining value across the two lists

    Correct answer
    D

    The 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.

  57. 57

    What ordering does the described merge of two sorted linked lists produce?

    1. A

      A single list with values in alternating ascending and descending order

    2. B

      A single list in ascending order

    3. C

      Two lists whose original order is preserved separately

    4. D

      A single list in the reverse of both input orders

    Correct answer
    B

    A single list in ascending order

    Explanation

    Repeatedly choosing the smaller head and connecting the recursive result yields one ascending sorted list.

  58. 58

    What ordering property defines a binary search tree in the described example?

    1. A

      All values in the tree must be larger than the root

    2. B

      Values in a node's left subtree are smaller, and values in its right subtree are larger

    3. C

      Values at every level must be arranged from largest to smallest

    4. D

      Every node must have exactly two children with equal values

    Correct answer
    B

    Values 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.

  59. 59

    When inserting a value into a binary search tree, what should happen if the current node is null?

    1. A

      Replace the root with the value, regardless of the current subtree

    2. B

      Restart the search from the opposite side of the root

    3. C

      Discard the value because it has no comparison partner

    4. D

      Create and return a new node at that position

    Correct answer
    D

    Create 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.

  60. 60

    How does a recursive binary search tree insertion choose its next subtree?

    1. A

      It chooses the subtree with fewer nodes without comparing values

    2. B

      It always recurses left until reaching a null node

    3. C

      It inserts the value at the root and moves the old root downward

    4. D

      It compares the inserted value with the current node and recurses left or right accordingly

    Correct answer
    D

    It 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.

  61. 61

    Why does a recursive insertion return the original root after inserting a node below it?

    1. A

      The root contains a copy of every node in the tree

    2. B

      The root still represents the entry point to the updated tree

    3. C

      Returning the root removes the recursive calls from memory before insertion

    4. D

      The newly inserted leaf must replace the root

    Correct answer
    B

    The 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.

  62. 62

    What property identifies a leaf node when recursively printing tree leaves?

    1. A

      It has no left child and no right child

    2. B

      It has exactly one child

    3. C

      It has a parent but no stored value

    4. D

      It is the root, regardless of its children

    Correct answer
    A

    It 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.

  63. 63

    In the described left-to-right depth-first traversal of a tree, when is the right subtree explored?

    1. A

      Only if the left subtree contains no leaf nodes

    2. B

      Before the left subtree is visited

    3. C

      At the same time as the left subtree, independently

    4. D

      After the left subtree has been fully explored

    Correct answer
    D

    After the left subtree has been fully explored

    Explanation

    The traversal completes the left recursive exploration before proceeding to the right subtree.

  64. 64

    Why does graph depth-first search maintain a visited set?

    1. A

      To replace the graph's links with a tree structure

    2. B

      To ensure that every neighbor is explored more than once

    3. C

      To avoid revisiting nodes and becoming trapped by cycles

    4. D

      To sort all neighboring nodes before exploring them

    Correct answer
    C

    To 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.

  65. 65

    How does depth-first search behave when a node has several unvisited neighbors?

    1. A

      It always chooses the neighbor with the largest value

    2. B

      It returns to the starting node after each single edge

    3. C

      It visits all neighbors at once and then stops

    4. D

      It explores one neighbor's path deeply before moving to another neighbor

    Correct answer
    D

    It 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.

  66. 66

    In the described graph search, what should happen when the current node equals the goal?

    1. A

      Restart the search with an empty visited set

    2. B

      Remove the node from the graph before continuing

    3. C

      Return true immediately

    4. D

      Return false and continue searching its neighbors

    Correct answer
    C

    Return true immediately

    Explanation

    A node matching the goal means the sought value has been found, so the search can return true.

  67. 67

    What should graph search return if it finishes exploring all reachable neighbors without finding the goal?

    1. A

      True, because every neighbor was examined

    2. B

      The original starting node

    3. C

      False

    4. D

      The value of the last visited node

    Correct answer
    C

    False

    Explanation

    If the traversal exhausts the reachable nodes without finding the goal, the goal was not found in that search.

  68. 68

    What is memoization intended to do in a recursive algorithm?

    1. A

      Add more recursive calls to ensure every result is independently recomputed

    2. B

      Replace every base case with a loop

    3. C

      Store previously computed results so repeated subproblems can reuse them

    4. D

      Store only the input values that caused a stack overflow

    Correct answer
    C

    Store 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.

  69. 69

    How does the described memoized Fibonacci method handle a value already in its cache?

    1. A

      It stores a second copy and continues recursing

    2. B

      It changes the base case to the cached value's index

    3. C

      It returns the cached value without making the recursive calculation again

    4. D

      It deletes the value and recomputes both recursive branches

    Correct answer
    C

    It 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.

  70. 70

    Why does memoizing Fibonacci reduce redundant computation?

    1. A

      It makes every call compute both branches simultaneously

    2. B

      It prevents the algorithm from making any recursive calls

    3. C

      It changes every Fibonacci value into a base case

    4. D

      A value such as F(2)F(2)F(2) can be computed once and reused by later calls

    Correct answer
    D

    A value such as F(2)F(2)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.

  71. 71

    What does the content state about retrieving a value from the described hashmap cache?

    1. A

      It is available only after the program has returned from all calls

    2. B

      It provides constant-time access, described as O(1)O(1)O(1)

    3. C

      It takes time proportional to the number of recursive calls

    4. D

      It requires scanning the entire map in every case

    Correct answer
    B

    It provides constant-time access, described as O(1)O(1)O(1)

    Explanation

    The described hashmap uses hashing to provide constant-time access to stored values.

  72. 72

    What should a memoized recursive method do after computing a result that was not already cached?

    1. A

      Clear the cache before returning the result

    2. B

      Store the result in the cache and then return it

    3. C

      Discard the result so the next call can recompute it

    4. D

      Store only the input and return a base-case value

    Correct answer
    B

    Store the result in the cache and then return it

    Explanation

    Saving each newly computed result lets subsequent calls reuse it.

  73. 73

    Why can a tail-recursive call sometimes allow a compiler to reduce call-stack growth?

    1. A

      The recursive call is the final operation, so no pending work is needed afterward

    2. B

      The method makes no recursive calls at all

    3. C

      The recursive call always runs before the current method begins

    4. D

      The method stores every result in a global variable

    Correct answer
    A

    The 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.

  74. 74

    Why is the described factorial method that returns a value multiplied by a recursive call not tail recursive?

    1. A

      Multiplication remains to be performed after the recursive call returns

    2. B

      It reaches a base case before making any call

    3. C

      Its final result is only a recursive call

    4. D

      It passes an accumulator as an additional parameter

    Correct answer
    A

    Multiplication 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.

  75. 75

    How can an accumulator-style factorial method make its recursive call tail recursive?

    1. A

      Create a second recursive call for every multiplication

    2. B

      Move the base case after the final multiplication

    3. C

      Multiply the recursive result after it returns from the next call

    4. D

      Pass the accumulated result into the next call and return that call directly

    Correct answer
    D

    Pass 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.

  76. 76

    Which statement about tail-call optimization support is consistent with the content?

    1. A

      Only Java and Python guarantee tail-call optimization

    2. B

      Support depends on the language and execution environment and is not universal

    3. C

      Tail-call optimization is performed by the operating system for every method

    4. D

      Every programming language always optimizes tail-recursive calls

    Correct answer
    B

    Support 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.

  77. 77

    What is the central idea of backtracking as described?

    1. A

      Continue along one choice even after it has failed

    2. B

      Sort all possible choices before making a single decision

    3. C

      Cache every result and never revisit a previous decision

    4. D

      Explore choices recursively, evaluate whether they work, and return to try alternatives when needed

    Correct answer
    D

    Explore 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.

Question 1 of 77
Question 1 of 77

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent