Question 11
EasyWhat is the output of this code?
def p(a, b):
return 1 if b == 0 else a * p(a, b - 1)
print(p(2, 5))
- A.
16
- B.
10
- C.
25
- D.
32
Sign up free to see the answer and a step-by-step explanation.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
def p(a, b):
return 1 if b == 0 else a * p(a, b - 1)
print(p(2, 5))
16
10
25
32
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def f(n):
if n <= 1:
return 1
return f(n - 1) + f(n - 2)
print(f(5))
8
13
5
120
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def f(n):
if n == 0:
return
print(n, end=" ")
f(n - 1)
f(3)
3 2 1
3 2 1 0
1 2 3
0 1 2 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def g(n):
if n == 0:
return
g(n - 1)
print(n, end=" ")
g(3)
1 2 3
0 1 2 3
3 2 1
3
Sign up free to see the answer and a step-by-step explanation.
f(n) calls f(n − 1) and stops when n reaches 0. How many calls are made in total for f(4), including the first?
4
6
5
16
Sign up free to see the answer and a step-by-step explanation.
What is indirect recursion?
A loop inside a function
A function with no parameters
A function calling itself directly
Function A calls function B, which in turn calls A
Sign up free to see the answer and a step-by-step explanation.
Which problem is naturally solved by recursion?
Swapping two variables
Printing one line
Tower of Hanoi
Adding two numbers
Sign up free to see the answer and a step-by-step explanation.
Which technique is commonly used to solve the N-Queens problem?
Hashing
Binary search
Backtracking
Greedy selection of the first square
Sign up free to see the answer and a step-by-step explanation.
Which technique does a typical Sudoku solver use?
Breadth-first search only
Backtracking: try a digit, and undo it if it leads to a dead end
Sorting
Dynamic programming only
Sign up free to see the answer and a step-by-step explanation.
Can every recursive function be rewritten without recursion?
No, never
Only for functions with one parameter
Yes, for example by using an explicit stack and a loop
Only in Python
Sign up free to see the answer and a step-by-step explanation.