WorksheetsIT 403 - Quiz 2
Total questions: 25
Worksheet time: 17mins
When importing a module using from module_name import *, it automatically imports all functions, variables, and classes without any namespace conflicts.
True
False
Tuples are immutable, which means you cannot modify individual elements, but you can reassign the entire tuple variable to a new tuple.
True
False
The finally block in exception handling will execute even if a return statement is encountered in the try block.
True
False
When a function parameter has a default value, it must be placed after all parameters without default values in the function definition.
True
False
The __init.py__ file in a Python package is required to make Python treat the directory as a package, and it must contain import statements for all modules in that package.
True
False
Which of the following best explains why tuple unpacking is considered a powerful feature in Python?
It allows tuples to be modified after creation
It enables elegant assignment of multiple variables in a single operation
It automatically converts tuples to lists for easier manipulation
It provides better memory management than individual variable assignments
In the context of variable scope, what happens when a function tries to modify a global variable without using the global keyword?
Python raises a NameError immediately
The function creates a local variable with the same name, leaving the global variable unchanged
The global variable is automatically modified
Python raises a SyntaxError during compilation
Which exception handling approach demonstrates the best practice for managing multiple potential errors?
Using a single except Exception: to catch all possible errors
Using multiple specific except clauses for different error types
Using try-except without specifying exception types
Using only finally blocks without except clauses
What is the primary advantage of using *args and **kwargs in function definitions?
They improve function execution speed
They allow functions to accept a variable number of arguments
They automatically validate input parameters
They enable functions to return multiple values
When using dictionary methods, what is the key difference between .get() and direct key access using square brackets?
.get() is faster for key lookups
.get() returns None for missing keys while square brackets raise KeyError
Square brackets can only access string keys
.get() automatically creates missing keys
What is the primary purpose of the if __name__ == "__main__": construct in Python modules?
To define the main function of the module
To prevent the module from being imported by other scripts
To execute code only when the module is run directly, not when imported
To specify the module's version information
Which import strategy provides the best balance between code readability and namespace management?
from module import * for convenience
import module with full module names for clarity
Using aliases with import module as alias for commonly used modules
Selective imports with from module import specific_function
In Python package structure, what role does the __init.py__ file serve beyond making a directory a package?
It only marks the directory as a package and serves no other purpose
It can control what gets imported when someone imports the package
It automatically imports all modules in the package
It defines the package's main execution entry point
When designing a package hierarchy, what is considered the best practice for organizing related functionality?
Put all functions in a single large module to avoid complexity
Create one module per function for maximum modularity
Group related functions into logically organized modules within packages
Use only built-in modules to avoid dependency issues
What is the main advantage of using a requirements.txt file in Python projects?
It automatically installs Python on new systems
It ensures reproducible environments by specifying exact package versions
It speeds up package installation significantly
It reduces the size of Python packages
Analyze the following code and determine the output:
65
60
165
160
What will be the output of this code fragment?
(1, 3, 5) <class 'tuple'>
[1, 3, 5] <class 'list'>
(1, 2, 3, 4, 5, 6) <class 'tuple'>
[1, 2, 3, 4, 5, 6] <class 'list'>
Trace through this exception handling code:
Calculation attempted
Cannot divide by zero
Cannot divide by zero
Calculation attempted
None
Calculation attempted
Invalid input type
What is the output of this dictionary manipulation code?
4, -2
3, Not found
4, Not found
3, -2
Analyze this scope-related code:
Global: 8, Local: 0
Global: 3, Local: 0
Global: 0, Local: 0
Global: 0, Local: 3
def, return
function, print
def, print
func, return
try, except
if, else
try, catch
check, error
0, -1, len
1, 4, size
0, 3, length
1, -1, len
*, +, final_grade
**, *, total
/, +, final_grade
=, +, final_grade
age, student, major
years, student, course
age, ("Alice", 20, "BSIT"), major
old, student, major
