Worksheetspython just lec 4
Total questions: 30
Worksheet time: 15mins
Name
Class
Date
1.
What does a function parameter represent?
a)
A value passed when calling
b)
A name in the function definition
c)
A module
d)
A file handle
e)
A loop variable
2.
What are the values passed to a function call called?
a)
parameters
b)
arguments
c)
tokens
d)
imports
e)
locals
3.
What is the output of: def f(): pass; print(f()) ?
a)
0
b)
False
c)
None
d)
''
e)
Error
4.
Which call uses a keyword argument?
a)
pow(2,3)
b)
pow(2,b=3)
c)
pow(a=2,3)
d)
pow(2:3)
e)
pow[2,3]
5.
What is the purpose of a docstring?
a)
Speed up execution
b)
Document a function/module
c)
Replace comments
d)
Store binary data
e)
Change scope
6.
What is the scope of a variable defined inside a function?
a)
Global
b)
Local to that function
c)
Module-wide always
d)
Class-wide
e)
Depends on type
7.
Which keyword is used to declare that a variable is global inside a function?
a)
global
b)
nonlocal
c)
public
d)
extern
e)
static
8.
Which keyword refers to a variable in the nearest enclosing (non-global) scope?
a)
global
b)
nonlocal
c)
outer
d)
enclose
e)
scope
9.
What does 'import math' allow you to do?
a)
Use all built-ins
b)
Access math.sqrt as math.sqrt
c)
Replace built-ins
d)
Run math automatically
e)
Disable floats
10.
Which import form brings a name directly into the namespace?
a)
import math
b)
from math import sqrt
c)
import from math sqrt
d)
include math.sqrt
e)
use math as sqrt
11.
What does 'as' do in imports (e.g., import numpy as np)?
a)
Creates a copy of the module
b)
Creates an alias name
c)
Runs the module twice
d)
Imports only functions
e)
Prevents import
12.
What is the value of __name__ when a file is run directly?
a)
'__main__'
b)
'main'
c)
'__file__'
d)
None
e)
Depends on OS
13.
The common pattern 'if __name__ == "__main__":' is used to:
a)
Prevent syntax errors
b)
Run code only when executed directly
c)
Speed up imports
d)
Declare globals
e)
Create packages
14.
Which statement best describes DRY?
a)
Use many global variables
b)
Don't Repeat Yourself
c)
Don't Run Yesterday
d)
Use recursion always
e)
Avoid functions
15.
Which statement best describes SRP?
a)
One file per project
b)
Each function/module has one responsibility
c)
Always use classes
d)
Never use modules
e)
No side effects
16.
Which is a safer default-argument pattern for a mutable list?
a)
def f(x=[]): ...
b)
def f(x=None): if x is None: x=[]
c)
def f(x=list()): ...
d)
def f(*x=[]): ...
e)
def f(x=()): ...
17.
What does *args capture in a function definition?
a)
Only keyword arguments
b)
Extra positional arguments as a tuple
c)
Only integers
d)
A list of modules
e)
The return value
18.
[Functions & Modules] What does **kwargs capture?
a)
Extra keyword arguments as a dict
b)
Extra positional args
c)
Only strings
d)
File paths
e)
Exceptions
19.
Which built-in can be used to get help/documentation for a function?
a)
doc()
b)
help()
c)
manual()
d)
info()
e)
about()
20.
In a multi-file project, what is a Python module?
a)
A folder only
b)
A .py file that can be imported
c)
A compiled .exe
d)
A database table
e)
A notebook cell
21.
Which file name makes a directory a package in traditional Python packaging?
a)
main.py
b)
__init__.py
c)
package.py
d)
module.py
e)
__package__.py
22.
What is a common benefit of modularizing code into functions?
a)
Harder testing
b)
Easier reuse and testing
c)
More global state
d)
Less readability
e)
No need for loops
23.
What does return do?
a)
Prints output
b)
Exits the function and optionally provides a value
c)
Stops the whole program always
d)
Skips one loop iteration
e)
Catches exceptions
24.
Which function signature allows optional threshold with default 10?
a)
def f(threshold):
b)
def f(threshold==10):
c)
def f(threshold=10):
d)
def f(10=threshold):
e)
def f(*threshold=10):
25.
What is the output of: def f(x): x+=1; return x; a=1; f(a); print(a)?
a)
2
b)
1
c)
0
d)
Error
e)
None
26.
What does 'return results or None' mean (conceptually)?
a)
Always returns None
b)
Returns results if truthy, otherwise None
c)
Returns boolean only
d)
Raises error if empty
e)
Returns 0 on empty
27.
Which best describes a pure function?
a)
Reads/writes files
b)
Depends on global state
c)
No side effects; output depends only on inputs
d)
Always prints
e)
Uses random by default
28.
Which tool helps avoid circular imports in small projects?
a)
Put everything in one function
b)
Move shared code into a separate module
c)
Use more globals
d)
Disable imports
e)
Use break
29.
Which is true about Python default arguments?
a)
Evaluated at each call
b)
Evaluated once at function definition time
c)
Only for ints
d)
Only for strings
e)
Never evaluated
30.
Which expression creates an anonymous function that squares x?
a)
lambda x: x*x
b)
def (x): x*x
c)
func x -> x*x
d)
square(x) => x*x
e)
anon(x){x*x}
100 %
