Time: 30 minutes
Total Questions: 20
What will be the output of this code?
def outer():
x = 10
def inner():
nonlocal x
x += 5
return x
return inner
f = outer()
print(f())Write a function that takes a string and returns a dictionary with character counts (e.g., "hello" -> {'h': 1, 'e': 1, 'l': 2, 'o': 1}).
Explain the difference between shallow copy and deep copy in Python.
Write a class that uses @dataclass to create a simple data structure with automatic __init__, __repr__, and equality methods.
What is the purpose of __slots__ in a class? Write an example class using __slots__.
Write a function that demonstrates the use of enumerate() to iterate over a list with indices.
Create a custom iterator for the Fibonacci sequence that implements __iter__ and __next__.
Write a function that uses collections.namedtuple to create a Point class with x and y coordinates.
Explain the difference between instance methods, class methods, and static methods with examples.
Write a function that uses functools.partial to create a specialized version of an existing function.
What are closures? Write a closure example that maintains state between calls.
Write a function that demonstrates the use of *args with default parameters.
Create a class that implements the iterator protocol for a custom range-like object.
Write a function that uses itertools.chain to flatten a list of lists.
Explain what descriptors are and write a simple descriptor example.
Write a function that demonstrates the use of yield from for delegating to another generator.
What is the difference between staticmethod and classmethod? Provide examples of both.
Write a function that uses abc.ABC to create an abstract base class with an abstract method.
Create a function that demonstrates the use of vars() and dir() to inspect object attributes.
Write a class that demonstrates method resolution order (MRO) using multiple inheritance.
End of Test Set 4