PCEP Certification Practice Test: Mastering Recursion in Python
PCEP Certification Practice Test - Questions, Answers and Explanations
Below is a set of 25 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "recursion." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: What is recursion in Python?
- A function that calls another function.
- A function that repeats a loop.
- A function that calls itself.
- A function that returns multiple values.
Answer: C) A function that calls itself.
Explanation: Recursion is when a function calls itself in order to solve smaller instances of the same problem.
Question 2: Which of the following are necessary for a recursive function to work correctly? (Choose all that apply)
- A base case to terminate recursion.
- The function must call itself.
- The function must have parameters.
- The function should always return an integer.
Answer: A) A base case to terminate recursion.
B) The function must call itself.
Explanation: A recursive function must have a base case to prevent infinite recursion and must call itself to perform recursion. Parameters and return types depend on the specific function but are not required for recursion to work.
Question 3: Complete the code to define a recursive function factorial that calculates the factorial of a number.
def factorial(n): if n == 1: return 1 return n * factorial(______)▼
Answer: n - 1
Explanation: The recursive case reduces the problem by calling factorial(n - 1) until n reaches 1, which is the base case.
Question 4: What will be the output of the following code?
def countdown(n): if n == 0: return print(n) countdown(n - 1) countdown(3)
- 3 2 1
- 3 2 1 0
- 3 2
- Error
Answer: A) 3 2 1
Explanation: The recursive countdown() function prints n and calls itself with n - 1 until n reaches 0, where the recursion stops.
Question 5: Insert the correct base case to stop the recursion in the sum_to_n function.
def sum_to_n(n): if ______: return 0 return n + sum_to_n(n - 1)▼
Answer: n == 0
Explanation: The base case n == 0 stops the recursion by returning 0. This ensures the recursion terminates.
Question 6: What will the following recursive function output when fibonacci(4) is called?
def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2) print(fibonacci(4))
- 2
- 3
- 5
- 4
Answer: B) 3
Explanation: The Fibonacci sequence is defined as fibonacci(n) = fibonacci(n-1) + fibonacci(n-2), with fibonacci(4) resulting in 3.
Question 7: Which of the following are valid base cases for a recursive function? (Choose all that apply)
- if n == 1: return 1
- if n <= 0: return
- if n == "stop": return 0
- if not condition: return
Answer: A) if n == 1: return 1
B) if n <= 0: return
C) if n == "stop": return 0
D) if not condition: return
Explanation:Valid base cases terminate the recursion when certain conditions are met. A base case can be any condition that prevents further recursive calls. The option C) is valid if the function's logic involves a condition where n can be the string "stop", and the function should return 0 in this case. This base case is context-dependent.
Question 8: Arrange the steps to define a recursive function power that calculates x raised to the power of y.
- Define the base case when y == 0.
- Multiply x by the result of the recursive call with y - 1.
- Define the function power().
- Return the result from the base case.
Answer: 3, 1, 4, 2
Explanation: First, define the function, set up the base case when y == 0, return the base case result, and perform the recursive multiplication.
Question 9: Complete the code to define a recursive function sum_digits that returns the sum of the digits of a number.
def sum_digits(n): if n < 10: return n return n % 10 + sum_digits(______)▼
Answer: n // 10
Explanation: The recursive case divides n by 10 using integer division (n // 10), and the recursion continues until n is less than 10.
Question 10: What will be the output of the following code?
def reverse_string(s): if len(s) == 0: return s return reverse_string(s[1:]) + s[0] print(reverse_string("abc"))
- abc
- cba
- Error
- ab
Answer: B) cba
Explanation: The reverse_string() function uses recursion to reverse the string by taking the first character and placing it at the end, repeatedly.
Question 11: Insert the correct recursive call to complete the factorial function.
def factorial(n): if n == 1: return 1 return n * ______▼
Answer: factorial(n - 1)
Explanation: The recursive call factorial(n - 1) multiplies n by the result of the factorial of n - 1, continuing until n == 1.
Question 12: What is the main purpose of a base case in recursion?
- To perform mathematical operations.
- To stop the recursion when a condition is met.
- To generate infinite loops.
- To return multiple values.
Answer: B) To stop the recursion when a condition is met.
Explanation: The base case is necessary to stop the recursion and prevent infinite loops. It provides the condition that halts further recursive calls.
Question 13: Which of the following functions are examples of valid recursive functions? (Choose all that apply)
- A function that calls itself.
- A function that returns after the base case is met.
- A function that does not have a base case.
- A function that returns a recursive call until a condition is met.
Answer:A) A function that calls itself.
B) A function that returns after the base case is met.
D) A function that returns a recursive call until a condition is met.
Explanation: A recursive function must call itself and have a base case. Without a base case, the function would result in infinite recursion.
Question 14: Arrange the steps to define a recursive function that calculates the sum of numbers from 1 to n.
- Define the base case when n == 1.
- Define the recursive function sum_to_n().
- Return n + sum_to_n(n - 1) in the recursive case.
- Return 1 in the base case.
Answer: 2, 1, 4, 3
Explanation: First, define the recursive function, set up the base case when n == 1, return 1, and for the recursive case, return n + sum_to_n(n - 1).
Question 15: Complete the code to define a recursive function gcd that calculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.
def gcd(a, b): if b == 0: return a return gcd(b, ______)▼
Answer: a % b
Explanation: The recursive case in the Euclidean algorithm calls gcd(b, a % b) until b == 0, at which point a is the GCD.
Question 16: What will be the output of the following code?
def count_up(n): if n == 0: return count_up(n - 1) print(n) count_up(3)
- 1 2 3
- 3 2 1
- 3 2
- Error
Answer: A) 1 2 3
Explanation: The recursive count_up() function first makes recursive calls until n == 0, then prints the numbers in reverse order after returning from the recursion.
17. Insert the correct base case to terminate the recursion in the fib_sum function, which calculates the sum of Fibonacci numbers up to n.
def fib_sum(n): if ______: return 0 return fibonacci(n) + fib_sum(n - 1)▼
Answer: n == 0
Explanation: The base case n == 0 stops the recursion by returning 0. This ensures that the recursion terminates.
Question 18: Which of the following is a risk when using recursion without a proper base case?
- Infinite recursion.
- Faster execution.
- Reduced memory usage.
- Increased readability.
Answer: A) Infinite recursion
Explanation: Without a proper base case, the recursion will not terminate, leading to infinite recursion and eventually causing a RecursionError.
Question 19: Which of the following are characteristics of recursion? (Choose all that apply)
- Involves a base case to stop the recursion.
- Calls the same function repeatedly.
- Can be used to solve problems by dividing them into smaller instances.
- Always runs faster than iterative solutions.
Answer: A) Involves a base case to stop the recursion.
B) Calls the same function repeatedly.
C) Can be used to solve problems by dividing them into smaller instances.
Explanation: Recursion solves problems by breaking them into smaller subproblems and must have a base case to stop the recursion. Recursion is not always faster than iteration.
Question 20: Arrange the steps to define a recursive function product_of_numbers that calculates the product of numbers from 1 to n.
- Define the function product_of_numbers().
- Return 1 in the base case.
- Return n * product_of_numbers(n - 1) in the recursive case.
- Set the base case when n == 1.
Answer: 1, 4, 2, 3
Explanation: First, define the function, set the base case when n == 1, return 1 in the base case, and return n * product_of_numbers(n - 1) in the recursive case.
Question 21: Complete the code to define a recursive function print_digits that prints the digits of a number from left to right.
def print_digits(n): if n < 10: print(n) else: print_digits(n // 10) print(______)▼
Answer: n % 10
Explanation: The recursive case prints the last digit (n % 10) after recursively calling print_digits(n // 10) to handle the remaining digits.
Question 22: What will be the output of the following code?
def power(x, y): if y == 0: return 1 return x * power(x, y - 1) print(power(2, 3))
- 6
- 8
- 12
- Error
Answer: B) 8
Explanation: The recursive function power() calculates x raised to the power of y. In this case, 2^3 = 8.
Question 23: Insert the correct recursive call to calculate the length of a string using recursion.
def string_length(s): if s == "": return 0 return 1 + ______▼
Answer: string_length(s[1:])
Explanation: The recursive case reduces the string by slicing it (s[1:]) and adds 1 for each character until the string is empty.
Question 24: What will be the result of the following recursive function?
def decrement(n): if n == 0: return 0 return decrement(n - 1) print(decrement(3))
- 0
- 3
- Error
- None
Answer: A) 0
Explanation: The recursive function decrement() continues until n == 0, at which point it returns 0.
Question 25: Which of the following problems can be solved using recursion? (Choose all that apply)
- Calculating factorials.
- Generating the Fibonacci sequence.
- Reversing a string.
- Counting elements in a list.
Answer: A) Calculating factorials.
B) Generating the Fibonacci sequence.
C) Reversing a string.
D) Counting elements in a list.
Explanation: Recursion can be used to solve a variety of problems, such as calculating factorials, generating Fibonacci numbers, reversing strings, and counting elements in a list.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python/certificate/functions-and-exceptions-recursion.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics