MCQ

Python MCQ and Answers – Part 18

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 fun():
    return count + 1
count = 0
print(fun())

A 0

B 1

C Error

D None

B
It is possible to read directly the value of a global variable.
 

2. Which of the following represents the XOR operator?

A &

B |

C ^

D !

C
The ^ operator represents the XOR operation.
 

3. What happens if the base condition is not defined in a recursive program?

A The program goes into an infinite loop

B The program is executed once

C The program runs n times where n is the argument given to the function.

D An exception is thrown

A
The program will run until the system runs out of memory.
 

4. Which of the following statements is not true about recursion?

A Make the code clean

B A complex task can be divided into subproblems

C Recursive calls use less memory

D All the answers are true

C
Recursive calls occupy a lot of memory and take a lot of time because memory is used for each call of the function.
 

5. Which of the following statements is true?

A You cannot create custom exceptions in Python.

B You can create a user-defined exception by inheriting from the Exception class.

C You can create a user-defined exception by inheriting from the Error class.

D None of the above

B
We create custom or user-defined exceptions by creating a new class. The idea is to inherit the “Exception” class. Most of the built-in exceptions use the same idea to apply their exceptions. Let’s take an example :

class MyException(Exception):
   def __init__(self, arg):
      self.args = arg
 

6. What is the output of the following code?
def fun(x):
    x = ['alex', 'bob']
    return id(x)
names = ['bob', 'alex']
print(id(names) == fun(names))

A True

B False

C Error

D None

B
A new object is created in the function.
 

7. What is the output of the following code?
len(["str",1, 2, 3])

A 3

B 4

C 5

D Error

B
The len() function returns the number of elements in the list.
 

8. What is the output of the following code?
>>> d1 = {"alex":23, "jean":25}
>>> d2 = {"alex":22, "jean":25}
>>> d1 > d2

A False

B True

C None

D Error

D
The > operator cannot be used with dictionaries.

TypeError: '>' not supported between instances of 'dict' and 'dict'
 

9. What is the output of the following code?
>>> bin(10-2) + bin(12^4)

A 0b10000b10

B 0b10001000

C 0b10000b1000

D 0b10000

C
The output of bin(10-2) = 0b1000 and bin(12 ^ 4) = ob1000. So the result of the above expression is: 0b10000b1000.
 

10. Suppose that dic = {“alex”: 20, “jean”: 25}. To get the number of entries in the dictionary, which instruction do we use?

A dic.size()

B size(dic)

C dic.len()

D len(dic)

D
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 *