Python MCQ

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

C
The line f(x-1, x + y) keep calling the function until the base condition is satisfied.
 

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

C
The method __eq(obj) is called if a comparison is made and is defined in the Object’s 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

C
Generator function contains one or more yield statements. Let’s take an example :

def mygen():
    n = 100
    yield n
    ...

a = mygen()
print(next(a))
 

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

D
Recursive function does not need 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

C
 

6. Which function overloads the == operator?

A __equ__()

B __eq__()

C __isequal__()

D None of the above

B
 

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.

C
 

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

B
The f (f (x + 10)) part of the code keep executing until the value of x becomes greater than 50, after x-5 is returned and displayed.
 

9. Recursion and iteration are the same programming approach.

A True

B False

B
In recursion, the function calls itself until the base condition is reached, while iteration means repeating the process, for example in for loops.
 

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

A
It is possible to pass a function as an argument to other functions.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *