WorksheetsPython Functions and Programming Concepts Worksheet
Total questions: 20
Worksheet time: 10mins
What is Top-Down Design?
A method that starts with small details and builds up to the main problem
A method where you start with the big problem first, then break it down into smaller parts until each is simple enough to solve
A bottom-up approach focusing only on code reuse
A technique for debugging functions only
According to the document, why are functions essential for large software projects?
They allow a single programmer to handle everything alone
They reduce code duplication, increase modularity, and enable team collaboration by breaking problems into sub-problems
They eliminate the need for variables
They automatically handle recursion without planning
What is a function in Python?
A block of code that runs only once at the start of the program
A block of organized, reusable code that performs a single, related action and provides modularity
A type of variable that stores data permanently
A keyword used only for printing output
Which of the following is an example of a built-in function in Python?
A user-defined greet() function
print()
A recursive factorial() function
A lambda function for sorting
How do you define a basic function in Python?
Using the 'function' keyword followed by the name
Using 'def' followed by the function name and parentheses, with indented code block
Using 'lambda' for all functions
By declaring variables inside a loop
What is the difference between actual parameters and formal parameters?
Actual parameters are placeholders in the function definition; formal are values passed during the call
Actual parameters are values passed to the function; formal parameters are placeholders in the definition
There is no difference; they are synonyms
Actual parameters must include data types; formal do not
In the example def greet(name, age): print(f"Hello {name}! You are {age} years old."), what are "name" and "age" when calling greet("Alice", 25)?
name="Alice" and age=25 are actual parameters
name and age are formal parameters
name and age are both variables
name and age are arguments only
What happens when arguments are passed to a function?
The values are copied to the corresponding parameters inside the function
The function name is changed to match the arguments
Arguments are always returned immediately
No copying occurs; references are shared globally
Which type of argument allows passing values in any order by specifying parameter names?
Positional arguments
Default arguments
Keyword arguments
Arbitrary arguments (*args)
In def func(name, job='developer'); print(name, 'is a', job), what is printed if called as func('Bob')?
Bob is a
Error: Missing job argument
Bob is a developer
developer is a Bob
What does *args allow in a function?
Fixed number of positional arguments only
Collecting unmatched positional arguments into a tuple for variable length
Only keyword arguments with defaults
Returning multiple values automatically
What is the output of print_args(1, 54, 60) for def print_args(*args): print(args)?
1 54 60
[1, 54, 60]
(1, 54, 60)
Error: Too many arguments
What does a return statement do in a function?
It prints the value to the console
It ends the function call and sends data back to the caller; without it, returns None
It defines new parameters
It creates a global variable
How can a function return multiple values?
By using a list only
By separating values with a comma, which packs them into a tuple
Using print statements
It cannot return multiple values
What is function composition?
Nesting functions inside each other permanently
Using the output of one function as input to another, like math.sin(3602math.pi)
Breaking code into modules only
Recursively calling the same function
In a nested function example like def outer(a, b): def inner(c, d): return c + d; return inner(a, b), what does outer(2, 4) return?
Error: Nested functions not allowed
6
(2, 4)
None
What is the base case in the recursive countdown(num) function?
When num > 0, print num and recurse
When num <= 0, print 'Stop'
Always recurse until num=1
No base case is needed
What is the scope of a variable declared inside a function?
Global: Accessible everywhere
Local: Limited to the function block
Enclosing: Only in nested functions
Universal: Across all files
In the lambda function filter(lambda x: x % 2 == 0, [1,2,3,4]), what does it do?
Prints all numbers
Filters even numbers: [2,4]
Sums the list
Sorts the list descending
What is a decorator in Python?
A way to define anonymous functions only
A function that takes another function as input and returns a modified version, e.g., for logging or timing
A type of recursion
A global variable wrapper
