What is recursion in the context of arrays and strings?
Recursion is a method where a function calls itself to solve smaller parts of a problem, like processing elements of an array or characters of a string one by one until a base case is reached.
Click to reveal answer
beginner
What is the base case in a recursive function working on an array?
The base case is the condition that stops the recursion, often when the array index reaches the end or when the size of the array segment to process is zero.
Click to reveal answer
intermediate
How does recursion help in reversing a string?
Recursion reverses a string by swapping characters from the start and end, then calling itself on the smaller substring inside, until the base case of one or zero characters is reached.
Click to reveal answer
beginner
Explain how to find the sum of elements in an array using recursion.
To find the sum recursively, add the first element to the sum of the rest of the array found by a recursive call, stopping when the array is empty (base case).
Click to reveal answer
beginner
What happens if a recursive function on arrays or strings does not have a proper base case?
Without a proper base case, the recursion will continue indefinitely, causing a stack overflow error and crashing the program.
Click to reveal answer
What is the base case when recursively printing all elements of an array?
AWhen the index reaches the array length
BWhen the array is empty
CWhen the first element is zero
DWhen the last element is reached
✗ Incorrect
The base case is when the index reaches the array length, meaning no more elements to print.
Which of these is a correct recursive step to reverse a string?
AReverse the string without changing characters
BSwap first and second characters only
CSwap characters randomly
DSwap first and last characters, then reverse the substring inside
✗ Incorrect
The recursive step swaps the first and last characters, then calls itself on the substring inside.
What does a recursive function typically do after processing one element of an array?
AProcesses the entire array again
BReturns immediately
CCalls itself with the next element
DStops execution
✗ Incorrect
After processing one element, the function calls itself with the next element to continue recursion.
Why is a base case important in recursion?
ATo stop the recursion and avoid infinite calls
BTo start the recursion
CTo increase the recursion depth
DTo print the array
✗ Incorrect
The base case stops recursion to prevent infinite calls and stack overflow.
How can recursion be used to count the number of characters in a string?
ACount all characters at once
BCount 1 for the first character plus count of the rest recursively
CUse a loop instead
DCount only vowels
✗ Incorrect
Recursion counts 1 for the current character plus the count from the rest of the string.
Describe how recursion works on arrays with an example of summing elements.
Think about stopping condition and how each call handles one element.
You got /4 concepts.
Explain the steps to reverse a string using recursion.
Focus on how the string is reduced each time.
You got /4 concepts.
Practice
(1/5)
1. What does recursion mean when applied to arrays or strings?
easy
A. A function calls itself to solve smaller parts of the problem
B. A loop runs through all elements once
C. The array or string is sorted automatically
D. The data is copied to a new array or string
Solution
Step 1: Understand recursion concept
Recursion means a function calls itself to break down a problem into smaller parts.
Step 2: Apply to arrays or strings
For arrays or strings, recursion processes one element and then calls itself on the rest.
Final Answer:
A function calls itself to solve smaller parts of the problem -> Option A
Quick Check:
Recursion = function calls itself [OK]
Hint: Recursion always involves a function calling itself [OK]
Common Mistakes:
Confusing recursion with loops
Thinking recursion sorts data automatically
Believing recursion copies data
2. Which of the following is the correct base case for a recursive function that processes a string?
easy
A. if (length < 0) return;
B. if (length == 0) return;
C. if (string == NULL) continue;
D. if (length > 0) return;
Solution
Step 1: Identify base case role
The base case stops recursion when no more characters remain, usually length 0.
Step 2: Check options for correct base case
if (length == 0) return; correctly returns when length is 0, stopping recursion safely.
Final Answer:
if (length == 0) return; -> Option B
Quick Check:
Base case stops recursion at empty string [OK]
Hint: Base case usually checks for empty string or zero length [OK]