What is a key characteristic of a closure in Python?
It allows a function to be defined inside a class.
It prevents a function from accessing global variables.
It forces a function to return a value of a specific data type.
It allows a nested function to 'remember' and access variables from its enclosing function's scope even after the outer function has finished executing.
How do you create an instance of a class named 'Dog' in Python?
Dog.new()```
create Dog()```
Dog = new()```
my_dog = Dog()```
What will the following code snippet print?
def my_decorator(func): def wrapper(): print("Before function call") func() print("After function call") return wrapper @my_decorator def say_hello(): print("Hello!") say_hello()
Before function call Hello! After function call
Hello!
Error
Before function call After function call
Which statement correctly imports the 'math' module in Python?
include math
require math
using math
import math
How can you get the current date and time using the 'datetime' module?
datetime.today()
datetime.now
datetime.currentTime()
datetime.datetime.now()
Which of the following is a valid way to define a function with both default and keyword arguments in Python?
def my_function(a, b=2, **kwargs, c=3): # Function body
def my_function(a=1, *args, b, **kwargs): # Function body
def my_function(a=1, b, c=3): # Function body
def my_function(a, b=2, **kwargs): # Function body
How can you create a dictionary where the keys are numbers from 1 to 5 and the values are their squares using a dictionary comprehension?
{i: i**2 for i in range(1, 6)}
(i: i**2 for i in range(1, 6))
{i**2: i for i in range(1, 5)}
[i: i**2 for i in range(1, 6)]
Which data structure would be most appropriate for representing a graph in Python?
All of the above options can be used to represent a graph.
A list of lists, where each inner list represents a node and its connections.
A dictionary where keys are nodes, and values are lists of their neighbors.
A set of tuples, where each tuple represents an edge between two nodes.
Which method is used to write a single line of text to a file in Python?
writeline()
put()
writeln()
write()
What does the 'sys.argv' list in Python store?
Contents of the current working directory
Command line arguments passed to the script
Environment variables
System error messages