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.
A trader marked an article 50% above its cost price and sold it after allowing a discount of 30%. If the transaction resulted in a profit of Rs. 26, the...
In a quadrilateral ABCD, the diagonals AC and BD intersect at O. If AO = 8 cm, OC = 12 cm, BO = 6 cm, and OD = 9 cm, find the area of the quadrilateral ...
The ratio of the angles of a quadrilateral is 3:2:7:8. What is the sum of the largest and the smallest angle?
The length of the diagonal of a rhombus is 24 cm, and the perimeter is 80 cm. Find the area of the rhombus.
What is 4(a³b ⁶ ) 16 × 16(a ⁴ b²)2 equal to?
The area (in square units) of the quadrilateral ABCD, formed by the vertices A (0, -2), B (2, 1), C (0, 4), and D (-2, 1) is: