Python MCQ and Answers – Part 17
This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
1. What is the output of the following code?
def f(x, y): if(x==0): return y else: return f(x-1,x+y) print(f(2,5))
A 2
B 4
C 8
D Infinite loop
2. Which of the following statements is true?
A The method __new__()
automatically calls the method __init__
B The method __init__
is defined in the Object class
C The method __eq(obj)
is defined in the Object class
D The method __repr__()
is defined in the Object class
3. If a function contains at least one “yield” statement, it becomes ______
A An iterator
B Anonymous function
C Generator function
D None of the above
4. Which of the following statements is wrong about recursion?
A Infinite recursion can occur if the base condition is not correctly mentioned
B Each recursive function must have a base condition
C Recursive function makes the code easier to understand
D Each recursive function must have a return value
5. What will happen if you try to open a file that does not exist?
A A new file is created.
B Nothing is going to happen.
C An exception is thrown.
D None of the above
6. Which function overloads the == operator?
A __equ__()
B __eq__()
C __isequal__()
D None of the above
7. What is the output of the following code?
try: # code that can generate an error pass except (ZeroDivisionError, TypeError): print("Error message ")
A Displays “Error message” if an exception occurs (regardless of the exception).
B Displays “Error message” if no exception occurs.
C Displays “Error message” if TypeError or ZeroDivisionError exception occurs.
D Displays “Error message” only if both TypeError and ZeroDivisionError exceptions occur.
8. What is the output of the following code?
def f(x): if (x > 50): return x - 5 return f(f(x+10)); print(f(10))
A 100
B 50
C 25
D 10
9. Recursion and iteration are the same programming approach.
A True
B False
10. What is the output of the following code?
def fun(f, val): print(f(val)) fun(max, [1, 2, 3]) fun(min, [1, 2, 3])
A 3 1
B 1 3
C Error
D None of the above