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: 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)}
What is the purpose of the 'init.py' file in a Python package?
Defines the package's main function
Contains unit tests for the package
Marks the directory as a Python package
Stores the package's documentation
What is the output of the following Python code?
with open('data.txt', 'w') as file: file.write('Hello, world!') print(file.read())
Hello, world!
The code will raise a ValueError.
The code will print nothing.
The code will raise an IOError.
How can you get the current date and time using the 'datetime' module?
datetime.now
datetime.today()
datetime.datetime.now()
datetime.currentTime()
How can you access the constant 'pi' from the 'math' module after importing it?
math(pi)
pi
math->pi
math.pi
Which principle of OOP is violated if you have to modify numerous parts of your code when making a small change to a class?
Encapsulation
Inheritance
Abstraction
Polymorphism
When might you choose to raise an exception manually using raise?
raise
To replace all occurrences of built-in exceptions in your code.
To silently ignore errors and prevent the program from crashing.
When you want to signal that an unexpected condition has occurred that your code cannot handle directly.
To automatically retry a failed operation without any intervention.
What happens if no except block matches the raised exception?
except
The program terminates with an unhandled exception error.
The finally block is executed, and then the program continues.
finally
The program enters an infinite loop.
The program continues executing from the next line after the try...except block.
try...except
What is a key characteristic of a closure in Python?
It forces a function to return a value of a specific data type.
It prevents a function from accessing global variables.
It allows a function to be defined inside a class.
It allows a nested function to 'remember' and access variables from its enclosing function's scope even after the outer function has finished executing.
What does the 're.findall()' function return?
A boolean value indicating whether the pattern is present in the string.
A list of all non-overlapping matches of the pattern in the string.
The first match of the pattern in the string.
None of the above.