MCQs on Advanced Python Concepts | Python programming

1. List, Set, Dictionary Comprehensions

  1. Which of the following is the correct syntax for a list comprehension in Python?
    • a) [x for x in range(5)]
    • b) {x for x in range(5)}
    • c) [x: for x in range(5)]
    • d) for x in range(5) [x]
  2. What is the output of the following list comprehension? [x**2 for x in range(5)]
    • a) [0, 1, 2, 3, 4]
    • b) [1, 4, 9, 16, 25]
    • c) [0, 1, 4, 9, 16]
    • d) [0, 2, 4, 6, 8]
  3. How is a set comprehension written in Python?
    • a) {x for x in range(5)}
    • b) [x for x in range(5)]
    • c) set(x for x in range(5))
    • d) (x for x in range(5))
  4. Which of the following creates a dictionary comprehension in Python?
    • a) {key: value for key, value in zip(range(5), range(5))}
    • b) dict{key: value for key, value in zip(range(5), range(5))}
    • c) {key, value for key, value in zip(range(5), range(5))}
    • d) zip{key: value for key, value in zip(range(5), range(5))}

2. Decorators

  1. What is a decorator in Python?
    • a) A function that modifies the behavior of another function.
    • b) A method that defines a class.
    • c) A type of lambda function.
    • d) A function used for arithmetic operations.
  2. What is the purpose of the @decorator syntax in Python?
    • a) To create a decorator function.
    • b) To decorate a class method.
    • c) To decorate an object.
    • d) To call a decorator function on a function.
  3. Which of the following is true about decorators in Python?
    • a) Decorators can only be used with functions, not methods.
    • b) Decorators are used to modify or enhance functions or methods without changing their actual code.
    • c) Decorators are executed before the function.
    • d) Decorators can only be used with classes.
  4. Which built-in function is commonly used as a decorator in Python?
    • a) lambda()
    • b) staticmethod()
    • c) setattr()
    • d) property()

3. Generators and Iterators

  1. What is a generator in Python?
    • a) A type of function that returns a sequence of values.
    • b) A type of collection that holds multiple values.
    • c) A function that allows iteration through a list.
    • d) A type of object used for performing arithmetic operations.
  2. How does a generator differ from a normal function?
    • a) It uses return instead of yield.
    • b) It returns a list of all values at once.
    • c) It yields values one at a time using the yield keyword.
    • d) It executes only once.
  3. What does the next() function do in Python?
    • a) It initializes a generator function.
    • b) It returns the next value from the generator.
    • c) It creates a new generator.
    • d) It returns the previous value in the iterator.
  4. Which of the following is a valid generator expression?
    • a) (x**2 for x in range(5))
    • b) [x**2 for x in range(5)]
    • c) {x**2 for x in range(5)}
    • d) (x for x in range(5))

4. Regular Expressions

  1. Which module is used for working with regular expressions in Python?
    • a) regex
    • b) re
    • c) regexp
    • d) re.pattern
  2. What does the re.search() function do?
    • a) It returns the first match of the pattern in the string.
    • b) It checks if a pattern exists in a string.
    • c) It replaces all occurrences of a pattern with a new string.
    • d) It splits a string based on a regular expression.
  3. How would you match any digit in a string using regular expressions?
    • a) \w
    • b) \d
    • c) \D
    • d) \b
  4. What does the re.sub() function do in Python?
    • a) It replaces a matched pattern with a new string.
    • b) It splits a string into multiple strings.
    • c) It returns the index of a matched pattern.
    • d) It checks if the pattern is found in a string.

5. Context Managers (with statement)

  1. What is the purpose of a context manager in Python?
    • a) To manage database connections.
    • b) To automatically handle resource cleanup like file handling or network connections.
    • c) To define global variables.
    • d) To manage class methods.
  2. Which statement is used to create a context manager in Python?
    • a) with
    • b) try
    • c) finally
    • d) except
  3. What does the __enter__() method of a context manager do?
    • a) Initializes the context and sets up the resource.
    • b) Cleans up the resource.
    • c) Returns the context manager object.
    • d) Exits the context manager.
  4. What does the __exit__() method of a context manager do?
    • a) Exits the context manager and handles exceptions.
    • b) Initializes the context and sets up the resource.
    • c) Returns the context manager object.
    • d) Triggers the start of the resource.

6. Metaclasses

  1. What is a metaclass in Python?
    • a) A class that creates other classes.
    • b) A class that manages the attributes of an object.
    • c) A class used to create objects.
    • d) A built-in class used for exception handling.
  2. What method does a metaclass use to create a new class in Python?
    • a) __init__()
    • b) __call__()
    • c) __new__()
    • d) __metaclass__()
  3. How do you define a metaclass in Python?
    • a) By subclassing type and overriding __new__ or __init__.
    • b) By subclassing object.
    • c) By using class keyword.
    • d) By importing metaclass module.
  4. What is the purpose of metaclasses in Python?
    • a) To define how classes behave and how they are instantiated.
    • b) To define class methods.
    • c) To create objects from classes.
    • d) To modify the behavior of objects.

7. Memory Management in Python

  1. How does Python handle memory management?
    • a) By using reference counting and garbage collection.
    • b) By using manual memory allocation.
    • c) By using stack and heap memory allocation.
    • d) By using a memory pool.
  2. What is the purpose of Python’s garbage collection?
    • a) To manually allocate memory for objects.
    • b) To free up memory by reclaiming objects that are no longer in use.
    • c) To manage class inheritance.
    • d) To optimize the performance of Python code.
  3. What is the main function of reference counting in Python’s memory management?
    • a) To allocate memory for new objects.
    • b) To track the number of references to an object in memory.
    • c) To remove unused objects from memory.
    • d) To optimize object creation.
  4. Which of the following triggers garbage collection in Python?
    • a) When the reference count of an object drops to zero.
    • b) When the gc.collect() method is called.
    • c) Both a and b.
    • d) When the del keyword is used.
  5. How can you check the memory usage of an object in Python?
    • a) Using sys.memory()
    • b) Using gc.memory()
    • c) Using sys.getsizeof()
    • d) Using get.memory()
  6. What happens when an object’s reference count becomes zero in Python?
    • a) The object is immediately removed from memory.
    • b) The object is added to the garbage collection queue for cleanup.
    • c) The object is kept in memory until it’s manually deleted.
    • d) The object is reused for other references.

Answers (Tabular Form)

QnoAnswer
1a) [x for x in range(5)]
2b) [1, 4, 9, 16, 25]
3a) {x for x in range(5)}
4a) {key: value for key, value in zip(range(5), range(5))}
5a) A function that modifies the behavior of another function.
6d) To call a decorator function on a function.
7b) Decorators are used to modify or enhance functions or methods without changing their actual code.
8b) staticmethod()
9a) A type of function that returns a sequence of values.
10c) It yields values one at a time using the yield keyword.
11b) It returns the next value from the generator.
12a) (x**2 for x in range(5))
13b) re
14a) It returns the first match of the pattern in the string.
15b) \d
16a) It replaces a matched pattern with a new string.
17b) To automatically handle resource cleanup like file handling or network connections.
18a) with
19a) Initializes the context and sets up the resource.
20a) Exits the context manager and handles exceptions.
21a) A class that creates other classes.
22c) __new__()
23a) By subclassing type and overriding __new__ or __init__.
24a) To define how classes behave and how they are instantiated.
25a) By using reference counting and garbage collection.
26b) To free up memory by reclaiming objects that are no longer in use.
27b) To track the number of references to an object in memory.
28c) Both a and b.
29c) Using sys.getsizeof()
30b) The object is added to the garbage collection queue for cleanup.

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top