1. Home
  2. Explore
  3. Computer Science
  4. Data Structures and Algorithms

Study methods

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

Data Structures and Algorithms

  • 32Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursion Basics

In programming, what makes a function recursive?

32 questions

  1. 1

    In programming, what makes a function recursive?

    1. A

      It stores its results in an array before returning

    2. B

      It calls a different function exactly once

    3. C

      It repeats a statement using a loop counter

    4. D

      It calls itself, directly or through another call

    Correct answer
    D

    It calls itself, directly or through another call

    Explanation

    A recursive function is one that calls itself. This repeated self-calling is the central idea of recursion.

  2. 2

    Why is an unconditional recursive call a problem?

    1. A

      The function automatically skips its business logic

    2. B

      The function can keep calling itself until resources are exhausted

    3. C

      The function must return a different value after every call

    4. D

      The function cannot be called from another method

    Correct answer
    B

    The function can keep calling itself until resources are exhausted

    Explanation

    Without a condition that stops the calls, recursion can continue indefinitely and eventually exhaust available resources.

  3. 3

    What is the purpose of a termination condition, also called a base case?

    1. A

      To ensure the function always executes its logic twice

    2. B

      To stop further recursive calls when a specified condition is met

    3. C

      To increase the input size before each recursive call

    4. D

      To store every result produced by earlier calls

    Correct answer
    B

    To stop further recursive calls when a specified condition is met

    Explanation

    A base case tells a recursive function when to stop calling itself and return.

  4. 4

    Which sequence best describes the three parts of a typical recursive function?

    1. A

      Perform the task's logic, discard the input, call a separate program

    2. B

      Store the input, make a loop, return without checking a condition

    3. C

      Make a recursive call, reset the input, repeat the task's logic

    4. D

      Check a stopping condition, perform the task's logic, make a recursive call

    Correct answer
    D

    Check a stopping condition, perform the task's logic, make a recursive call

    Explanation

    The described pattern includes a termination condition, the required business logic, and a recursive call. The order of the logic and recursive call can vary depending on the problem.

  5. 5

    When printing a name a specified number of times recursively, why can the function reduce the count by one on each call?

    1. A

      Each call must use a smaller count to increase the output length

    2. B

      The count represents the number of stack frames already removed

    3. C

      Reducing the count allows the function to avoid a base case

    4. D

      Each call prints once, so the remaining number of prints decreases

    Correct answer
    D

    Each call prints once, so the remaining number of prints decreases

    Explanation

    After printing once, the function has one fewer print remaining. Reducing the count moves the computation toward its stopping condition.

  6. 6

    When recursively traversing an array from a starting index, which condition correctly stops processing once the index has passed the valid elements?

    1. A

      Return when the index is less than the array length

    2. B

      Return when the index is greater than or equal to the array length

    3. C

      Return when the index is greater than the first array element

    4. D

      Return when the index is equal to the first array element

    Correct answer
    B

    Return when the index is greater than or equal to the array length

    Explanation

    For an array of length nnn, valid indices are below nnn. An index greater than or equal to the length means there are no more elements to process.

  7. 7

    In a recursive array traversal that prints elements in their original order, what should happen before the recursive call?

    1. A

      Print the element at the final index

    2. B

      Decrease the current index and return

    3. C

      Print the element at the current index

    4. D

      Call the function with the same index

    Correct answer
    C

    Print the element at the current index

    Explanation

    Printing the current element before calling recursively on the next index produces the array's original order.

  8. 8

    How can the same recursive array traversal print elements in reverse order?

    1. A

      Make the recursive call for the next index before printing the current element

    2. B

      Stop when the index reaches zero and print no elements

    3. C

      Print the current element before making the recursive call for the next index

    4. D

      Keep the index unchanged in every recursive call

    Correct answer
    A

    Make the recursive call for the next index before printing the current element

    Explanation

    When the recursive call happens before printing, the calls advance to the end of the array first. As they return, the elements are printed in reverse order.

  9. 9

    Which statement best captures how recursion differs from a simple loop in the examples discussed?

    1. A

      Recursive calls can descend to a stopping point and then perform work while returning

    2. B

      Recursive calls always execute each step only once and never return

    3. C

      Recursion avoids repeating work, while loops necessarily repeat it

    4. D

      A loop can move forward, but recursion cannot process a sequence

    Correct answer
    A

    Recursive calls can descend to a stopping point and then perform work while returning

    Explanation

    A recursive computation can make calls until it reaches a base case, then resume the pending work as those calls return. A simple loop does not create this same call-and-return structure.

  10. 10

    What determines whether logic runs while recursive calls are being made or while they are returning?

    1. A

      Whether the input is stored in an array or a variable

    2. B

      Whether the base case uses equality or inequality

    3. C

      Whether the logic appears before or after the recursive call

    4. D

      Whether the function is written in Java or Python

    Correct answer
    C

    Whether the logic appears before or after the recursive call

    Explanation

    Logic placed before the recursive call runs as the calls descend. Logic placed after it runs as calls return.

  11. 11

    What underlying data structure supports ordinary function calls during recursion?

    1. A

      A call stack

    2. B

      A hash table

    3. C

      A priority queue

    4. D

      A circular array

    Correct answer
    A

    A call stack

    Explanation

    Function calls are tracked using a call stack, which supports the nested calls made by recursion.

  12. 12

    Which stack property explains why the most recently made recursive call returns first?

    1. A

      Smallest value, first out

    2. B

      Last in, first out

    3. C

      Largest value, first out

    4. D

      First in, first out

    Correct answer
    B

    Last in, first out

    Explanation

    A stack is last in, first out: the most recently added call is the first one removed as execution returns.

  13. 13

    What information does a recursive call stack entry need to preserve in the array-printing example?

    1. A

      The array and the index associated with that call

    2. B

      Only the base case and the first array element

    3. C

      The results of every possible future recursive call

    4. D

      Only the final printed output and the array length

    Correct answer
    A

    The array and the index associated with that call

    Explanation

    Each call must retain its relevant state, such as the array and its current index, so execution can resume correctly after a deeper call returns.

  14. 14

    In a recursive call chain, what does reaching the base case do?

    1. A

      It causes the deepest call to execute before the earlier calls

    2. B

      It immediately removes every call from the stack at once

    3. C

      It stops that call from making another recursive call and begins the return process

    4. D

      It restarts the original call with its initial input

    Correct answer
    C

    It stops that call from making another recursive call and begins the return process

    Explanation

    The base case ends further descent. The deepest call returns, after which earlier calls resume in reverse order.

  15. 15

    Why does a recursive function's return from the base case not erase all earlier calls?

    1. A

      Each recursive call runs independently of the calls that made it

    2. B

      The return applies to the current call, allowing its caller to resume

    3. C

      The base case converts all pending calls into loop iterations

    4. D

      A return from recursion is ignored until the first call finishes

    Correct answer
    B

    The return applies to the current call, allowing its caller to resume

    Explanation

    A return ends the current function invocation, not every invocation. The caller's saved state remains available on the call stack.

  16. 16

    If a recursive function prints an element only after calling itself on the next index, when does the first element get printed?

    1. A

      Immediately, before the second recursive call begins

    2. B

      It is never printed because the index has changed

    3. C

      When the base case is first checked

    4. D

      During the return phase, after deeper calls have finished

    Correct answer
    D

    During the return phase, after deeper calls have finished

    Explanation

    The first call pauses at its print statement while deeper calls proceed. It prints the first element only after those calls return.

  17. 17

    Why can recursion require more memory than an iterative solution to the same task?

    1. A

      Each active recursive call uses space in the call stack

    2. B

      The call stack grows even after all calls have returned

    3. C

      Recursion always stores every output twice

    4. D

      Every recursive call permanently duplicates the input array

    Correct answer
    A

    Each active recursive call uses space in the call stack

    Explanation

    Active recursive calls must retain their state on the call stack, so recursion uses additional space while those calls are pending.

  18. 18

    How can an index check using greater-than-or-equal-to the array length help make recursive traversal safer?

    1. A

      It makes an empty array behave as though it has one element

    2. B

      It ensures the recursive call never changes the index

    3. C

      It also stops calls that begin with an index already beyond the last valid position

    4. D

      It allows the function to access the array after its final position

    Correct answer
    C

    It also stops calls that begin with an index already beyond the last valid position

    Explanation

    Checking for an index greater than or equal to the length handles both the usual stopping point and an index that is already beyond the valid range.

  19. 19

    In a preorder traversal of a tree, what order is used in the example?

    1. A

      Traverse the left child, then the right child, then visit the current node

    2. B

      Visit the current node, then traverse its left child, then its right child

    3. C

      Traverse the right child, then visit the current node, then the left child

    4. D

      Visit all leaves first, then traverse the current node's children

    Correct answer
    B

    Visit the current node, then traverse its left child, then its right child

    Explanation

    The described preorder traversal processes the current node first, followed by its left subtree and then its right subtree.

  20. 20

    What is the base case in the tree preorder example?

    1. A

      Return when the current node has two children

    2. B

      Return when the current node is the root

    3. C

      Return when the current node is null

    4. D

      Return when the left child has been visited

    Correct answer
    C

    Return when the current node is null

    Explanation

    A null node represents no subtree to process, so the recursive traversal returns when it reaches one.

  21. 21

    Why can recursion make tree traversal easier to express than an iterative approach?

    1. A

      Tree nodes cannot be stored while using an iterative algorithm

    2. B

      Recursion guarantees that every tree traversal takes less time

    3. C

      The recursive calls naturally follow the tree's repeated parent-and-child structure

    4. D

      Trees contain no branches, so a loop cannot traverse them

    Correct answer
    C

    The recursive calls naturally follow the tree's repeated parent-and-child structure

    Explanation

    A tree is organized as nodes with child subtrees, and recursion can apply the same traversal pattern to each subtree.

  22. 22

    Which set of topics was identified as using recursion in later data-structure and algorithm problems?

    1. A

      Merge sort, quicksort, linked-list reversal, trees, graphs, and dynamic programming

    2. B

      Array allocation, arithmetic operations, memory management, and network routing

    3. C

      Input validation, string formatting, operating systems, and user-interface design

    4. D

      Binary search, hashing, queues, database indexing, and file compression

    Correct answer
    A

    Merge sort, quicksort, linked-list reversal, trees, graphs, and dynamic programming

    Explanation

    The session connected recursion to merge sort, quicksort, reversing linked lists, tree and graph problems, and dynamic programming.

  23. 23

    Does choosing recursion automatically give a better time complexity than an iterative solution?

    1. A

      Yes; recursion always performs each required operation once

    2. B

      Yes; every recursive solution has lower time complexity than a loop

    3. C

      No; recursion may be more intuitive without improving time complexity

    4. D

      No; recursion always has worse time complexity than iteration

    Correct answer
    C

    No; recursion may be more intuitive without improving time complexity

    Explanation

    The content explicitly notes that recursion does not always provide better time complexity. Its advantage may instead be a clearer or more intuitive solution.

  24. 24

    Why may recursion be a natural fit for some tree and graph problems?

    1. A

      Their structures prevent the use of explicit data structures

    2. B

      They always store their nodes in a single linear sequence

    3. C

      Their branching or repeated patterns can be expressed by solving similar subproblems

    4. D

      They require every node to be visited in the same fixed order

    Correct answer
    C

    Their branching or repeated patterns can be expressed by solving similar subproblems

    Explanation

    The session emphasizes that recursion can express repeated, non-linear patterns in structures such as trees and graphs more naturally than forcing them into a linear process.

  25. 25

    What is one reason to prefer an iterative approach over recursion?

    1. A

      Avoiding the extra memory used by recursive call-stack entries

    2. B

      Making the input structure non-linear

    3. C

      Ensuring that a function can call itself

    4. D

      Allowing the program to use a base case

    Correct answer
    A

    Avoiding the extra memory used by recursive call-stack entries

    Explanation

    Because recursion uses call-stack space, memory constraints can be a reason to consider an iterative solution.

  26. 26

    What benefit of recursion does the session emphasize besides its use in solving problems?

    1. A

      It ensures code has no memory costs

    2. B

      It eliminates the need to consider edge cases

    3. C

      It makes every algorithm independent of its input

    4. D

      It can make code more intuitive and concise for suitable problems

    Correct answer
    D

    It can make code more intuitive and concise for suitable problems

    Explanation

    For suitable problems, recursion can express the solution simply and intuitively, sometimes with fewer lines of code.

  27. 27

    Is the recursion concept tied to one particular programming language?

    1. A

      Yes; recursion is supported only by Java

    2. B

      Yes; recursion requires a language with a built-in tree type

    3. C

      No; the concept can be implemented in languages such as Java, Python, and Scala

    4. D

      No; recursion works only in languages without loops

    Correct answer
    C

    No; the concept can be implemented in languages such as Java, Python, and Scala

    Explanation

    The session describes recursion as language-neutral and applicable in languages including Java, Python, and Scala.

  28. 28

    In the Tower of Hanoi setup, what is the objective?

    1. A

      Move all disks from the first tower to the second using the third as an auxiliary tower

    2. B

      Divide the disks equally among all three towers while preserving their order

    3. C

      Move the largest disk from the first tower to the third without moving smaller disks

    4. D

      Move all disks to the auxiliary tower and leave the destination tower empty

    Correct answer
    A

    Move all disks from the first tower to the second using the third as an auxiliary tower

    Explanation

    The stated task is to transfer all disks from tower one to tower two, using tower three as an auxiliary tower.

  29. 29

    How are the disks arranged at the start of the Tower of Hanoi problem?

    1. A

      All disks have the same diameter and are stacked on the first tower

    2. B

      The disks are arranged in alternating size order from bottom to top

    3. C

      The largest disk is at the bottom and the smallest is at the top

    4. D

      The smallest disk is at the bottom and the largest is at the top

    Correct answer
    C

    The largest disk is at the bottom and the smallest is at the top

    Explanation

    The starting stack places disks in decreasing diameter from bottom to top: the largest is at the bottom and the smallest at the top.

  30. 30

    Which move is allowed under the Tower of Hanoi disk-size constraint?

    1. A

      Place a larger disk on top of a smaller disk

    2. B

      Place a smaller disk on top of a larger disk

    3. C

      Place any disk on top of another disk of smaller size

    4. D

      Place two disks together on top of a third disk

    Correct answer
    B

    Place a smaller disk on top of a larger disk

    Explanation

    A smaller disk may be placed on a larger one, but a larger disk cannot be placed on a smaller one.

  31. 31

    How many disks may be moved in a single Tower of Hanoi move?

    1. A

      Exactly one disk

    2. B

      All disks that are not yet on the destination tower

    3. C

      Exactly two disks from different towers

    4. D

      Any number of disks from the same tower

    Correct answer
    A

    Exactly one disk

    Explanation

    The problem's rules allow only one disk to be moved from one tower to another at a time.

  32. 32

    How is a move counted in the Tower of Hanoi problem?

    1. A

      Moving a disk and its supporting disk counts as one move

    2. B

      A move is counted only after all disks have been transferred

    3. C

      Moving one disk from one tower to another counts as one move

    4. D

      Moving a disk counts only if it reaches the destination tower

    Correct answer
    C

    Moving one disk from one tower to another counts as one move

    Explanation

    Each transfer of a single disk between towers is counted as one move.

Question 1 of 32
Question 1 of 32

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent