CompTIA Tech+ (FC0-U71)Software Development ConceptsHard

A developer is writing a program where a function needs to call itself repeatedly to solve a problem by breaking it down into smaller, identical sub-problems until a base case is reached. For example, calculating the factorial of a number (n!) by defining it as n * (n-1)!. What programming technique is being utilized?

  1. APolymorphism
  2. BIteration
  3. CEncapsulation
  4. DRecursion
Show answer & explanation

Correct answer: D. Recursion

Recursion is a programming technique where a function calls itself, either directly or indirectly, to solve a problem. This approach is effective for problems that can be broken down into smaller, self-similar sub-problems, with a defined base case to stop the calls. The factorial example is a classic illustration of recursion.

Why the other options are wrong

  • A. Polymorphism is an OOP principle allowing objects of different classes to be treated as a common type, unrelated to self-calling functions.
  • B. Iteration involves using loops (for, while) to repeat a block of code, not a function calling itself.
  • C. Encapsulation is an OOP principle about bundling data and methods, unrelated to repetitive function calls.

Recursion

Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem by breaking it down into smaller, identical sub-problems, eventually reaching a base case.

  • Requires a base case to terminate the recursive calls.
  • Can lead to elegant and concise solutions for certain problems.
  • Can consume more memory (stack space) than iterative solutions.

Memory trick: Recursion is like Russian dolls, each one containing a smaller version of itself.

More Software Development Concepts questions