Data Structures and Algorithms
- 32Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
In programming, what makes a function recursive?
In programming, what makes a function recursive?
32 questions
- 1
In programming, what makes a function recursive?
- A
It stores its results in an array before returning
- B
It calls a different function exactly once
- C
It repeats a statement using a loop counter
- D
It calls itself, directly or through another call
Correct answer
DIt 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.
- A
- 2
Why is an unconditional recursive call a problem?
- A
The function automatically skips its business logic
- B
The function can keep calling itself until resources are exhausted
- C
The function must return a different value after every call
- D
The function cannot be called from another method
Correct answer
BThe 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.
- A
- 3
What is the purpose of a termination condition, also called a base case?
- A
To ensure the function always executes its logic twice
- B
To stop further recursive calls when a specified condition is met
- C
To increase the input size before each recursive call
- D
To store every result produced by earlier calls
Correct answer
BTo 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.
- A
- 4
Which sequence best describes the three parts of a typical recursive function?
- A
Perform the task's logic, discard the input, call a separate program
- B
Store the input, make a loop, return without checking a condition
- C
Make a recursive call, reset the input, repeat the task's logic
- D
Check a stopping condition, perform the task's logic, make a recursive call
Correct answer
DCheck 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.
- A
- 5
When printing a name a specified number of times recursively, why can the function reduce the count by one on each call?
- A
Each call must use a smaller count to increase the output length
- B
The count represents the number of stack frames already removed
- C
Reducing the count allows the function to avoid a base case
- D
Each call prints once, so the remaining number of prints decreases
Correct answer
DEach 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.
- A
- 6
When recursively traversing an array from a starting index, which condition correctly stops processing once the index has passed the valid elements?
- A
Return when the index is less than the array length
- B
Return when the index is greater than or equal to the array length
- C
Return when the index is greater than the first array element
- D
Return when the index is equal to the first array element
Correct answer
BReturn when the index is greater than or equal to the array length
Explanation
For an array of length n, valid indices are below n. An index greater than or equal to the length means there are no more elements to process.
- A
- 7
In a recursive array traversal that prints elements in their original order, what should happen before the recursive call?
- A
Print the element at the final index
- B
Decrease the current index and return
- C
Print the element at the current index
- D
Call the function with the same index
Correct answer
CPrint the element at the current index
Explanation
Printing the current element before calling recursively on the next index produces the array's original order.
- A
- 8
How can the same recursive array traversal print elements in reverse order?
- A
Make the recursive call for the next index before printing the current element
- B
Stop when the index reaches zero and print no elements
- C
Print the current element before making the recursive call for the next index
- D
Keep the index unchanged in every recursive call
Correct answer
AMake 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.
- A
- 9
Which statement best captures how recursion differs from a simple loop in the examples discussed?
- A
Recursive calls can descend to a stopping point and then perform work while returning
- B
Recursive calls always execute each step only once and never return
- C
Recursion avoids repeating work, while loops necessarily repeat it
- D
A loop can move forward, but recursion cannot process a sequence
Correct answer
ARecursive 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.
- A
- 10
What determines whether logic runs while recursive calls are being made or while they are returning?
- A
Whether the input is stored in an array or a variable
- B
Whether the base case uses equality or inequality
- C
Whether the logic appears before or after the recursive call
- D
Whether the function is written in Java or Python
Correct answer
CWhether 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.
- A
- 11
What underlying data structure supports ordinary function calls during recursion?
- A
A call stack
- B
A hash table
- C
A priority queue
- D
A circular array
Correct answer
AA call stack
Explanation
Function calls are tracked using a call stack, which supports the nested calls made by recursion.
- A
- 12
Which stack property explains why the most recently made recursive call returns first?
- A
Smallest value, first out
- B
Last in, first out
- C
Largest value, first out
- D
First in, first out
Correct answer
BLast in, first out
Explanation
A stack is last in, first out: the most recently added call is the first one removed as execution returns.
- A
- 13
What information does a recursive call stack entry need to preserve in the array-printing example?
- A
The array and the index associated with that call
- B
Only the base case and the first array element
- C
The results of every possible future recursive call
- D
Only the final printed output and the array length
Correct answer
AThe 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.
- A
- 14
In a recursive call chain, what does reaching the base case do?
- A
It causes the deepest call to execute before the earlier calls
- B
It immediately removes every call from the stack at once
- C
It stops that call from making another recursive call and begins the return process
- D
It restarts the original call with its initial input
Correct answer
CIt 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.
- A
- 15
Why does a recursive function's return from the base case not erase all earlier calls?
- A
Each recursive call runs independently of the calls that made it
- B
The return applies to the current call, allowing its caller to resume
- C
The base case converts all pending calls into loop iterations
- D
A return from recursion is ignored until the first call finishes
Correct answer
BThe 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.
- A
- 16
If a recursive function prints an element only after calling itself on the next index, when does the first element get printed?
- A
Immediately, before the second recursive call begins
- B
It is never printed because the index has changed
- C
When the base case is first checked
- D
During the return phase, after deeper calls have finished
Correct answer
DDuring 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.
- A
- 17
Why can recursion require more memory than an iterative solution to the same task?
- A
Each active recursive call uses space in the call stack
- B
The call stack grows even after all calls have returned
- C
Recursion always stores every output twice
- D
Every recursive call permanently duplicates the input array
Correct answer
AEach 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.
- A
- 18
How can an index check using greater-than-or-equal-to the array length help make recursive traversal safer?
- A
It makes an empty array behave as though it has one element
- B
It ensures the recursive call never changes the index
- C
It also stops calls that begin with an index already beyond the last valid position
- D
It allows the function to access the array after its final position
Correct answer
CIt 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.
- A
- 19
In a preorder traversal of a tree, what order is used in the example?
- A
Traverse the left child, then the right child, then visit the current node
- B
Visit the current node, then traverse its left child, then its right child
- C
Traverse the right child, then visit the current node, then the left child
- D
Visit all leaves first, then traverse the current node's children
Correct answer
BVisit 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.
- A
- 20
What is the base case in the tree preorder example?
- A
Return when the current node has two children
- B
Return when the current node is the root
- C
Return when the current node is null
- D
Return when the left child has been visited
Correct answer
CReturn when the current node is null
Explanation
A null node represents no subtree to process, so the recursive traversal returns when it reaches one.
- A
- 21
Why can recursion make tree traversal easier to express than an iterative approach?
- A
Tree nodes cannot be stored while using an iterative algorithm
- B
Recursion guarantees that every tree traversal takes less time
- C
The recursive calls naturally follow the tree's repeated parent-and-child structure
- D
Trees contain no branches, so a loop cannot traverse them
Correct answer
CThe 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.
- A
- 22
Which set of topics was identified as using recursion in later data-structure and algorithm problems?
- A
Merge sort, quicksort, linked-list reversal, trees, graphs, and dynamic programming
- B
Array allocation, arithmetic operations, memory management, and network routing
- C
Input validation, string formatting, operating systems, and user-interface design
- D
Binary search, hashing, queues, database indexing, and file compression
Correct answer
AMerge 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.
- A
- 23
Does choosing recursion automatically give a better time complexity than an iterative solution?
- A
Yes; recursion always performs each required operation once
- B
Yes; every recursive solution has lower time complexity than a loop
- C
No; recursion may be more intuitive without improving time complexity
- D
No; recursion always has worse time complexity than iteration
Correct answer
CNo; 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.
- A
- 24
Why may recursion be a natural fit for some tree and graph problems?
- A
Their structures prevent the use of explicit data structures
- B
They always store their nodes in a single linear sequence
- C
Their branching or repeated patterns can be expressed by solving similar subproblems
- D
They require every node to be visited in the same fixed order
Correct answer
CTheir 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.
- A
- 25
What is one reason to prefer an iterative approach over recursion?
- A
Avoiding the extra memory used by recursive call-stack entries
- B
Making the input structure non-linear
- C
Ensuring that a function can call itself
- D
Allowing the program to use a base case
Correct answer
AAvoiding 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.
- A
- 26
What benefit of recursion does the session emphasize besides its use in solving problems?
- A
It ensures code has no memory costs
- B
It eliminates the need to consider edge cases
- C
It makes every algorithm independent of its input
- D
It can make code more intuitive and concise for suitable problems
Correct answer
DIt 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.
- A
- 27
Is the recursion concept tied to one particular programming language?
- A
Yes; recursion is supported only by Java
- B
Yes; recursion requires a language with a built-in tree type
- C
No; the concept can be implemented in languages such as Java, Python, and Scala
- D
No; recursion works only in languages without loops
Correct answer
CNo; 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.
- A
- 28
In the Tower of Hanoi setup, what is the objective?
- A
Move all disks from the first tower to the second using the third as an auxiliary tower
- B
Divide the disks equally among all three towers while preserving their order
- C
Move the largest disk from the first tower to the third without moving smaller disks
- D
Move all disks to the auxiliary tower and leave the destination tower empty
Correct answer
AMove 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.
- A
- 29
How are the disks arranged at the start of the Tower of Hanoi problem?
- A
All disks have the same diameter and are stacked on the first tower
- B
The disks are arranged in alternating size order from bottom to top
- C
The largest disk is at the bottom and the smallest is at the top
- D
The smallest disk is at the bottom and the largest is at the top
Correct answer
CThe 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.
- A
- 30
Which move is allowed under the Tower of Hanoi disk-size constraint?
- A
Place a larger disk on top of a smaller disk
- B
Place a smaller disk on top of a larger disk
- C
Place any disk on top of another disk of smaller size
- D
Place two disks together on top of a third disk
Correct answer
BPlace 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.
- A
- 31
How many disks may be moved in a single Tower of Hanoi move?
- A
Exactly one disk
- B
All disks that are not yet on the destination tower
- C
Exactly two disks from different towers
- D
Any number of disks from the same tower
Correct answer
AExactly one disk
Explanation
The problem's rules allow only one disk to be moved from one tower to another at a time.
- A
- 32
How is a move counted in the Tower of Hanoi problem?
- A
Moving a disk and its supporting disk counts as one move
- B
A move is counted only after all disks have been transferred
- C
Moving one disk from one tower to another counts as one move
- D
Moving a disk counts only if it reaches the destination tower
Correct answer
CMoving 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.
- A

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