A. The recursive call uses the same n, causing infinite recursion
B. The function should use a loop instead
C. The base case is missing
D. The return statement is outside the function
Solution
Step 1: Check recursive call argument
The function calls sum_to_n(n) instead of sum_to_n(n-1), so input never decreases.
Step 2: Understand effect of error
This causes infinite recursion because n stays the same and base case is never reached.
Final Answer:
The recursive call uses the same n, causing infinite recursion -> Option A
Quick Check:
Recursive call must reduce input [OK]
Hint: Recursive call must use smaller input to reach base case [OK]
Common Mistakes:
Calling function with same argument
Missing base case
Confusing recursion with loops
5. Write a recursive R function flatten_list that takes a nested list of numbers and returns a flat vector of all numbers. What is the correct approach?
hard
A. Use recursion but forget to combine results from recursive calls
B. Check if input is a list; if yes, recursively flatten each element and combine; else return the element as vector
C. Return the input directly without any processing
D. Use a for loop to iterate and append elements without recursion
Solution
Step 1: Identify recursive structure
Nested lists require checking each element; if it's a list, call flatten_list recursively.
Step 2: Combine results properly
Combine all flattened elements into one vector to get a flat list of numbers.
Final Answer:
Check if input is a list; if yes, recursively flatten each element and combine; else return the element as vector -> Option B
Quick Check:
Recursion + combine results = flatten list [OK]
Hint: Recursively flatten each list element and combine results [OK]
Common Mistakes:
Not combining recursive results
Forgetting to check if element is a list
Using loops only without recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.