This Quiz contains 20 multiple-choice questions focused on functions, including topics such as user-defined functions, parameters, and variable scope, tailored for CBSE Class 12 Computer Science students.
Students Point
Students PointยทIndia
What is the output of the following function call: def func(x, y=5): return x + y when called as func(3)?
8
5
3
None
Explanation
In the function func(x, y=5), y has a default value of 5. When we call func(3), x is assigned the value 3, and y takes the default value of 5. The function returns 3 + 5, which equals 8.
Which of the following correctly describes a local variable?
It can be accessed from anywhere in the program.
It retains its value even after the function execution.
It is defined globally and can be accessed throughout the program.
It is specific to the function in which it is declared.
Explanation
A local variable is defined within a function and can only be accessed inside that function. It does not exist outside of the function's scope.
What will be the output of the following code? def add(a, b=2): return a + b; print(add(3, 4))
2
7
4
3
Explanation
The function add(a, b=2) is called with a as 3 and b as 4. Since we provided a value for b, the default value of 2 is ignored. The function returns 3 + 4, which equals 7.
Which of the following statements about positional parameters is correct?
They can be passed in any order.
They are optional and have default values.
They must be provided in the order defined in the function.
They are the same as keyword parameters.
Explanation
Positional parameters are those parameters that need to be provided in the order defined in the function. They allow for the correct mapping of arguments to parameters based on their position.
What will be the output of the following function: def func(a, b): return a * b; print(func(5))?
Error: missing 1 required positional argument 'b'
5
10
None
Explanation
The function func(a, b) requires two arguments, but only one argument (5) is provided in the call. This will raise a TypeError because the second argument b is missing.
What does the following code snippet demonstrate? def greet(name): return f'Hello, {name}!'; print(greet('Alice'))
Error in function definition.
It returns a greeting string.
It prints the name directly.
It uses a global variable.
Explanation
This code snippet defines a simple user-defined function greet that takes a parameter name and returns a greeting string. When called with 'Alice', it outputs 'Hello, Alice!'.
What will happen if you try to access a local variable outside its function scope?
You will get a NameError.
The program will run without issues.
The value will be returned as None.
The variable will be treated as global.
Explanation
Accessing a local variable outside its function scope will result in a NameError because the variable does not exist in that context.
In the context of functions, what are default parameters?
Parameters that cannot be changed.
Parameters that require user input.
Parameters that are always optional.
Parameters that have a default value.
Explanation
Default parameters are parameters that assume a default value if a value is not provided during the function call. This allows the function to be called with fewer arguments.
What is the flow of execution when a function is called?
The program stops executing.
The function executes but does not return control.
Control transfers to the function and returns after execution.
The function runs indefinitely.
Explanation
When a function is called, the program control transfers to the function's code block. It executes the code inside the function and returns control back to the point where the function was called after completing execution.
What is the output of the following code snippet? def multiply(x, y=3): return x * y; print(multiply(4, 2))
12
8
7
6
Explanation
The function multiply(x, y=3) is called with x as 4 and y as 2. Since a value is provided for y, the function returns 4 * 2, which equals 8.
What is the purpose of the return statement in a function?
To exit the function and return a value.
To print output to the console.
To define local variables.
To start the function execution.
Explanation
The return statement is used to exit a function and send back a value to the caller. It effectively ends the function execution and can provide output.
If a function modifies a global variable, what happens to that variable outside the function?
It remains unchanged.
It is reset to its initial value.
It cannot be modified.
It is updated with the new value.
Explanation
If a function modifies a global variable, that change is reflected outside the function as well since the variable is declared in the global scope.
What is the significance of the keyword 'global' in Python?
It allows access to local variables.
It prevents variable modification.
It allows modification of global variables.
It is used to define a function.
Explanation
The 'global' keyword is used to declare a variable inside a function as global, allowing it to modify the global variable rather than creating a local one.
Which of the following is an example of a user-defined function?
print()
len()
input()
def my_function(): pass
Explanation
A user-defined function is any function that is defined by the user using the 'def' keyword, like def my_function(): pass. It is not built-in.
How can you pass multiple arguments to a function in Python?
Only one argument can be passed.
Separate them with commas.
Use a list to group them.
Pass them as a single string.
Explanation
You can pass multiple arguments to a function by separating them with commas in the function call, matching the parameters defined in the function.
What occurs if a function does not have a return statement?
It returns None by default.
It raises an error.
It returns the last variable assigned.
It causes an infinite loop.
Explanation
If a function does not have a return statement, it returns None by default when executed. This indicates that no value has been explicitly returned.
Which of the following statements about function parameters is true?
Parameters are the same as return values.
Parameters cannot be optional.
Parameters are placeholders for input values.
Parameters must always be provided.
Explanation
Function parameters act as placeholders for the values that will be passed into the function. They allow the function to accept input during its execution.
What will be the output of
`
def check(x):
return x == 10;
print(check(10))
`?
False
True
None
Error
Explanation
The function check(x) checks if x is equal to 10. When called with 10, it evaluates to True, thus returning True.
How does Python handle variable scope?
By using the LEGB rule.
By using only local scope.
By assuming all variables are global.
By storing all variables in a single namespace.
Explanation
Python uses a LEGB rule (Local, Enclosing, Global, Built-in) to determine the scope of variables. It checks in this order to find the variable's value.
What is the result of def func(): return 1; result = func(); print(result)?
None
0
Error
1
Explanation
The function func() returns the value 1. When result is assigned the return value of func(), it holds 1. Thus, printing result will output 1.
Sign up free to build quizzes, chatbots, flashcards, and many other interactive activities โ then assign them to your class and see how every student is doing.
Sign up now