Functions & Recursion MCQs with Answers
Which of the following best defines a function in programming?
a) A block of code that performs a specific task
b) A type of variable used to store data
c) A structure used to define classes
d) A way to handle errors in code
What is the main purpose of a function in programming?
a) To break code into reusable chunks
b) To handle input and output
c) To store program state
d) To manage memory allocation
What does the term “recursion” refer to in programming?
a) A function calling itself
b) A function executing in parallel
c) A method for sorting data
d) A technique for memory management
Which of the following is an example of a base case in recursion?
a) A condition that stops the recursive calls
b) A variable used to hold function results
c) A loop used to iterate through elements
d) A recursive function with no parameters
Which of the following is true about recursive functions?
a) They must have a base case
b) They cannot accept parameters
c) They always use loops internally
d) They never terminate
Which of the following is NOT a characteristic of a recursive function?
a) It calls itself directly or indirectly
b) It requires a termination condition
c) It operates with loops
d) It can result in a stack overflow if not implemented properly
What is the value of the factorial of 0 (0!)?
a) 1
b) 0
c) -1
d) Undefined
What is the time complexity of a recursive binary search algorithm?
a) O(n)
b) O(log n)
c) O(n^2)
d) O(1)
Which of the following is a disadvantage of recursion?
a) It requires more memory due to the call stack
b) It is always faster than iteration
c) It avoids the use of function calls
d) It eliminates the need for variables
What is the difference between a function and a method?
a) A function is independent, while a method is associated with an object
b) A method can never return a value, while a function always does
c) A function can only take one parameter, while a method can take multiple
d) A method is always recursive, while a function is not
What is the purpose of the “return” statement in a function?
a) To terminate the function and pass control back to the calling code
b) To declare a new variable
c) To call another function
d) To initiate a loop within the function
What does “tail recursion” mean in a recursive function?
a) The recursive call is the last operation in the function
b) The function calls itself multiple times in a loop
c) The recursive function includes a loop to break recursion
d) The function does not call itself at all
Which of the following is an example of a recursive function?
a) A function that calls itself with modified parameters
b) A function that sorts an array
c) A function that iterates over a list
d) A function that computes the sum of a list of numbers iteratively
What happens if a recursive function does not have a base case?
a) It will result in infinite recursion and eventually a stack overflow
b) It will complete normally without any errors
c) It will iterate over a loop
d) It will terminate after a fixed number of calls
What is the time complexity of a recursive Fibonacci function?
a) O(1)
b) O(n)
c) O(n^2)
d) O(2^n)
Which of the following is an example of a non-recursive function?
a) A function that uses a loop instead of recursion
b) A function that calls itself with modified parameters
c) A function that stores values in the call stack
d) A function that directly returns the result without any calculations
Which of the following is a benefit of recursion?
a) Simplifies the code by reducing the need for loops
b) Avoids the need for a termination condition
c) Automatically manages memory for function calls
d) Always results in faster execution than iteration
What is the base case for the factorial function?
a) When the input is 1
b) When the input is 0
c) When the input is negative
d) When the function exceeds a certain number of calls
In which of the following scenarios is recursion generally preferred over iteration?
a) When the problem can naturally be divided into smaller subproblems
b) When a solution requires the use of loops
c) When performance is the highest priority
d) When memory efficiency is most important
Which of the following best describes a “recursive case” in a recursive function?
a) A call to the function with modified arguments
b) A function that terminates and returns a result
c) A function that does not call itself
d) A base case that ends the recursion
Which of the following is a correct implementation of a recursive factorial function in Python?
a) def factorial(n): return n * factorial(n-1) if n > 0 else 1
b) def factorial(n): if n == 0: return 1 else: return n * factorial(n)
c) def factorial(n): if n == 1: return 0 else: return n * factorial(n-1)
d) def factorial(n): if n == 0: return n * 1 else: return n * factorial(n)
What is the primary disadvantage of using recursion?
a) It consumes more memory due to the function call stack
b) It always requires more lines of code
c) It is harder to understand
d) It is less flexible than iteration
Which of the following is a recursive function used to solve the Tower of Hanoi problem?
a) A function that moves disks from one peg to another
b) A function that sorts an array
c) A function that calculates the greatest common divisor
d) A function that finds the nth Fibonacci number
What is the output of the following recursive function for input 5 in a factorial function?
def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)
a) 120
b) 24
c) 5
d) 15
What is the time complexity of a simple recursive Fibonacci function?
a) O(1)
b) O(n)
c) O(n^2)
d) O(2^n)
Which of the following describes a function call stack in recursion?
a) The memory structure that stores local variables for each recursive call
b) The sequence of steps executed by a recursive function
c) The set of values returned by each recursive function
d) The list of base cases in a recursive function
Which of the following is an example of a recursive problem?
a) Calculating the factorial of a number
b) Sorting a list of numbers using a loop
c) Searching for an element in a list
d) Counting elements in a list iteratively