Question
What is the primary difference between recursion and
iteration in programming?Solution
Recursion involves a function calling itself until a base condition is met. Each recursive call creates a new stack frame, leading to memory usage that grows with the depth of recursion. Iteration, on the other hand, uses loops and does not involve stack frame creation, making it more memory-efficient in many cases. Why Other Options Are Incorrect: 1. Recursion uses loops for repetition, whereas iteration does not: This statement reverses the roles. Recursion relies on function calls for repetition, while iteration explicitly uses loops (for, while). 2. Iteration cannot solve problems that recursion can solve: Iteration can replicate recursion's functionality by using stacks or queues explicitly. Recursion is often a stylistic or practical choice, not a limitation of iteration. 3. Recursion is always faster than iteration: Recursion can sometimes be slower due to stack overhead and repeated function calls. Tail-call optimization can mitigate this in some languages, but generally, iteration is faster for equivalent tasks. 4. Recursion does not require a base case, while iteration requires a condition: Recursion must have a base case to terminate; otherwise, it leads to infinite calls. Iteration, too, requires a stopping condition, such as a loop exit condition.
Which of the following statement is true?
Who lives on 10th floor?
- Seven boxes, F, G, H, I, J, K and L, are kept one above other, such that bottommost box is numbered as 1 and topmost box is numbered as 7. F is a prime num...
Which of the following fruits does Tushar like?
- Five persons F, G, H, I, and J live on different floor of a five-storey building. The bottommost floor is numbered as 1 and the topmost floor is numbered a...
How many box (es) were placed above the box which contained 35 Kg of Sugar?
Who among the following person goes immediately before I?
Seven persons D, E, F, G, H, I and J went to Delhi on seven different days of a week starting from Monday. Only two persons went before I. At least two...
Which of the following box is placed two boxes above box D in the same stack?
- Which box is the lightest?