15.
What is the output of the following pseudocode program, which first defines a function
‘whoosh’ and then calls it from a for-loop?
define whoosh(n)
if n is equal to 1
return 1
else
return n + whoosh(n-1)
endif
end define
for i = 1 to 5
print whoosh(i)
endfor
a. 1, 2, 3, 4, 5
b. 1, 1, 2, 3, 5
c. 1, 3, 6, 10, 15
d. 1, 2, 6, 24, 120

The pseudocode defines a recursive function whoosh(n) that computes the sum of the first n natural numbers (triangular numbers), and the for-loop calls it for i from 1 to 5, printing 1, 3, 6, 10, 15. This matches option c .

Function Analysis

The whoosh(n) function uses recursion: it returns 1 when n=1 (base case), otherwise returns n + whoosh(n-1). This builds cumulative sums—whoosh(1)=1whoosh(2)=2+1=3whoosh(3)=3+3=6whoosh(4)=4+6=10whoosh(5)=5+10=15. Execution trace confirms: for i=1 prints 1; i=2 prints 3; up to i=5 printing 15 .

Option Breakdown

  • a. 1, 2, 3, 4, 5: Matches simple identity (n), ignoring recursion.

  • b. 1, 1, 2, 3, 5: Resembles Fibonacci but skips additions correctly.

  • c. 1, 3, 6, 10, 15: Correct—triangular numbers from recursive sum .

  • d. 1, 2, 6, 24, 120: Factorials (n!), uses multiplication instead.

Understanding Pseudocode Recursive Function Whoosh Output

The pseudocode recursive function whoosh(n) calculates triangular numbers via recursion, printing 1, 3, 6, 10, 15 when called in a for-loop from 1 to 5—key for CSIR NET exam prep on algorithms . This differs from factorial or Fibonacci, focusing on sum patterns.

Step-by-Step Recursion Trace

Recursion unfolds backward: whoosh(5)=5+whoosh(4), down to whoosh(1)=1, yielding triangular sums. For-loop executes independently per i, avoiding state issues.

i Value whoosh(i) Calculation Output
1 1 (base) 1
2 2 + 1 = 3 3
3 3 + 3 = 6 6
4 4 + 6 = 10 10
5 5 + 10 = 15 15

Common MCQ Traps

Options like factorials mislead due to similar recursion but multiplication. Practice distinguishes sum vs product recursion for exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Courses