What does the following code snippet print?
def outer_func(x): def inner_func(y): return x + y return inner_func my_func = outer_func(5) print(my_func(3))
15
5
8
Error
Consider the function definition: def my_func(a, b=5, *args, **kwargs): .... What does *args achieve?
def my_func(a, b=5, *args, **kwargs): ...
*args
It collects any number of positional arguments into a tuple.
It defines a variable number of default arguments.
It enforces that the function must be called with at least two arguments.
It collects any number of keyword arguments into a dictionary.
Which module would you use to interact with the operating system, like creating directories?
datetime
sys
os
math
What is the primary advantage of using a list comprehension over a traditional for loop when creating a new list in Python?
List comprehensions offer significant performance improvements, especially for large lists.
List comprehensions allow you to modify existing lists, while for loops can only create new ones.
List comprehensions can be used with any iterable, while for loops are limited to lists.
List comprehensions are more readable and concise for simple list creations.
What happens if no except block matches the raised exception?
except
The finally block is executed, and then the program continues.
finally
The program enters an infinite loop.
The program terminates with an unhandled exception error.
The program continues executing from the next line after the try...except block.
try...except
What is the purpose of the 'r' prefix in a regular expression pattern?
It treats the pattern as a raw string, ignoring escape sequences.
It performs a greedy match.
It enables multiline matching.
It makes the pattern case-insensitive.
Which code snippet correctly creates a dictionary where keys are numbers from 1 to 5 and values are their squares using a dictionary comprehension?
[num: num**2 for num in range(1, 6)]
{num**2: num for num in range(1, 6)}
{num: num**2 for num in range(1, 6)}
(num: num**2 for num in range(1, 6))
Which concept in OOP emphasizes providing a simplified view of an object, hiding its complexity?
Inheritance
Polymorphism
Abstraction
Encapsulation
Which of the following is a characteristic of encapsulation in Object-Oriented Programming?
Bundling data and methods that operate on that data into a single unit (a class)
Allowing direct modification of an object's internal state from anywhere in the program
Exposing all data and methods publicly
Preventing the use of inheritance
What is the primary purpose of using classes in Python?
To create reusable blueprints for objects with shared attributes and behaviors.
To store data in a structured format like dictionaries.
To control program flow and logic.
To define functions for specific tasks.