0% found this document useful (0 votes)
8 views6 pages

Tricky Python MCQs

This document contains a series of tricky multiple-choice questions (MCQs) related to Python programming concepts. Each question includes a code snippet, the expected output, and an explanation of the answer. Topics covered include decorators, generators, argument unpacking, and mutable default arguments.

Uploaded by

balachandar4010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Tricky Python MCQs

This document contains a series of tricky multiple-choice questions (MCQs) related to Python programming concepts. Each question includes a code snippet, the expected output, and an explanation of the answer. Topics covered include decorators, generators, argument unpacking, and mutable default arguments.

Uploaded by

balachandar4010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Tricky Python MCQs

Q1: What will be the output?

def modify_list(a, b=[]):

b.append(a)

return b

print(modify_list(1))

print(modify_list(2))

Answer: B

Explanation: Default list is shared across calls.

Q2: Decorator with Side Effect

def repeat_twice(func):

def wrapper(*args, **kwargs):

func(*args, **kwargs)

return func(*args, **kwargs)

return wrapper

@repeat_twice

def say_hello():

print("Hello")

result = say_hello()

print(result)

Answer: B

Explanation: Prints twice, but print() returns None.


Q3: Generator Exhaustion

def numbers():

for i in range(3):

yield i

gen = numbers()

print(list(gen))

print(list(gen))

Answer: B

Explanation: Generator is exhausted after first full iteration.

Q4: Positional vs Keyword Arguments

def greet(name, msg="Hello"):

return f"{msg}, {name}!"

print(greet("Alice"))

print(greet("Bob", msg="Hi"))

Answer: A

Explanation: Uses default unless explicitly passed.

Q5: List Comprehension with Side Effect

data = [1, 2, 3]

squares = [data.append(x * x) for x in data]

print(data)

Answer: C

Explanation: Appending during iteration leads to infinite loop.


Q6: Decorator on a Function Returning a Function

def outer():

def inner():

return "Inner function"

return inner

def deco(fn):

def wrapper():

return "Wrapped: " + fn()

return wrapper

wrapped = deco(outer())

print(wrapped())

Answer: B

Explanation: outer() returns inner, which is wrapped.

Q7: Generator Expression Evaluation

nums = [1, 2, 3]

gen = (x * 2 for x in nums)

nums.append(4)

print(list(gen))

Answer: B

Explanation: Generator is lazy; sees updated list.

Q8: Argument Unpacking in Function Calls

def add(a, b, c):

return a + b + c
args = [1, 2, 3]

print(add(*args))

Answer: A

Explanation: *args unpacks the list into arguments.

Q9: Mutable Default Arguments Trap

def add_to_list(value, my_list=[]):

my_list.append(value)

return my_list

print(add_to_list(1))

print(add_to_list(2))

print(add_to_list(3, []))

Answer: B

Explanation: Shared list unless new one provided.

Q10: Decorator Without `@`

def shout(func):

def wrapper():

return func().upper()

return wrapper

def say_hello():

return "hello"

say_hello = shout(say_hello)

print(say_hello())

Answer: B
Explanation: Function manually wrapped.

Q11: Nested Generator Consumption

def outer():

yield from inner()

def inner():

yield 1

yield 2

print(list(outer()))

Answer: A

Explanation: yield from delegates to inner generator.

Q12: List Slice Side Effects

lst = [1, 2, 3, 4]

sub = lst[:]

sub[0] = 99

print(lst)

Answer: B

Explanation: lst[:] creates a shallow copy.

Q13: Function Scope and Global

x = 10

def change():

x = 20
change()

print(x)

Answer: B

Explanation: x = 20 is local to the function.

You might also like