MCQ

Python MCQ and Answers – Part 19

This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
 

1. What is the output of the following code?
not(1>2)
not(1&1)

A FalseFalse

B FalseTrue

C TrueFalse

D TrueTrue

C
The function does not return “true” if the argument is “false”,
and “false” if the argument is “true”.
 

2. What is the output of 1+2**5//10

A 5

B 4

C 6

D 35

B
The order of priority is as follows: **, //, +. The expression 4 + 2 ** 5 // 10 evaluates to 1 + 32 // 10, which corresponds to 1 + 3 = 4. The result of the above expression is therefore 4.
 

3. What is the output of the following code?
d = {"alex":23, "jean":24}
print(list(d.keys()))

A (“alex”:23, “jean”:24)

B (“alex”, “jean”)

C [“alex”:23, “jean”:24]

D [“alex”, “jean”]  
 

D
The output of the above code is a list containing only the keys of dictionary d, in the form of a list.
 

4. What is the output of the following code?
>>> t=(1,2,3,4)
>>> t[1:3]

A (2, 3)

B (1, 2, 3)

C (2, 3,4)

D (1, 2)

A
 

5. What is the output of the following code?
def fun(i, l=[]):
    l.append(i)
    return l
for i in range(3):
    print(fun(i))

A [1] [1, 2] [1, 2, 3]

B [1] [2] [3]

C [0] [0, 1] [0, 1, 2]

D [0] [1] [2]  
 

C
 

6. What is the output of the following code?
t = (1, 2, 3, 4)
l = [t[i] for i in range(0, len(t), 2)]
print(l)

A [1, 3]

B [1, 3, 4]

C (1, 3)

D (1, 3, 4)

A
 

7. What is the output of the following code?
l = [1, 3, 6, 10]
n = (x**2 for x in l)
print(next(n), next(n))

A (1)

B (1, 3)

C (1, 9)

D (1, 9, 12, 20)

C
 

8. What is the output of the following code?
def fun(n):
    def multiply(x):
        return x * n
    return multiply

a = fun(2)
b = fun(2)

print(a(b(2)))

A 2

B 4

C 6

D 8

D
 

9. Which of the following statements is true?

A You cannot chain multiple decorators in Python.

B Decorators don’t work with functions that take parameters.

C The @ symbol is useless when using decorators.

D None of the above

D
Decorators are very powerful and useful tools in Python, as they allow programmers to modify the behavior of a function or class. Decorators allow us to nest another function to extend the behavior of the nested function, without changing it permanently. Let’s take an example :

@my_decorator
def say_welcome(): 
    print("Welcome to StackHowTo.com")

The above code is equivalent to :

def say_welcome():
    print("Welcome to StackHowTo.com") 
      
say_welcome = my_decorator(say_welcome)
 

10. What is the output of the following code?
sum(1,2,3)
sum([1,2,3])

A 6, 6

B 6, Error

C Error, Error

D Error, 6

D
The first function will result in an error because the “sum()” function is used to find the sum of iterable numbers. So the results will be Error and 6 respectively.
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 *