TomoLink
TCS NQT GuideTCS NQT Programming Logic & CS FundamentalsFunctions, Iteration, and Recursion Logic Tracing

Functions, Iteration, and Recursion Logic Tracing

Learn core concepts, essential formulas, and attempt practice questions designed on the latest TCS NQT testing patterns.

Key Concepts & Formulas

  • 1Recursion must have a base case to prevent stack overflow.
  • 2Trace recursion by drawing execution tree layers.

TCS NQT Style Practice Questions

Practice Question 1

What is output of: int f(int n) { if(n<=1) return 1; return n * f(n-1); } for f(3)?

A) 6
B) 3
C) 2
D) 1

Correct Answer: A) 6

Step-by-step Solution: f(3) = 3 * f(2) = 3 * (2 * f(1)) = 3 * 2 * 1 = 6. (Factorial sequence).

Practice Question 2

What error occurs if a recursive function lacks a base case?

A) Stack Overflow
B) Compilation Error
C) Division by zero
D) Out of Memory

Correct Answer: A) Stack Overflow

Step-by-step Solution: Infinite recursion exhausts the system call stack memory, triggering a Stack Overflow crash.

Study Pro-Tip

Trace function variables on paper step-by-step for recursion tracing questions.