0% found this document useful (0 votes)
5 views2 pages

Assignment On User Define Function in Python-Answer Key

The document is an assignment focused on user-defined functions in Python, presenting a series of questions and options related to function behavior, variable scope, and default arguments. It includes multiple-choice questions that test understanding of function definitions, global and local variables, and the effects of default parameters. The assignment aims to assess knowledge of Python programming concepts through practical examples.

Uploaded by

ishaanhalderdgp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Assignment On User Define Function in Python-Answer Key

The document is an assignment focused on user-defined functions in Python, presenting a series of questions and options related to function behavior, variable scope, and default arguments. It includes multiple-choice questions that test understanding of function definitions, global and local variables, and the effects of default parameters. The assignment aims to assess knowledge of Python programming concepts through practical examples.

Uploaded by

ishaanhalderdgp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT ON USER DEFINE FUNCTION IN PYTHON

** CHOOSE THE CORRECT OPTION


Q1. Q2. Q3.
x = 10 def func(a, L=[]): def func(*args, **kwargs):
def func(a, b=x): [Link](a) return args, kwargs
global x return L
x=a+b print(func(1)) print(func(1, 2, x=3, y=4))
return x print(func(2)) A. ((1,2), {'x':3,'y':4})
print(func(5)) A. [1], [2] B. [1], [1,2] B. ([1,2], {'x':3,'y':4})
print(x) C. [1,2], [1,2] D. Error C. Error
A. 15, 15 B. 15, 10 D. None
C. 20, 20 D. 15, 20
Q4. Q5. Q6.
x=5 def func(a, b=2, c=3): def func(a, b=5, *args, c=10, **kwargs):
def func(): return a + b + c return a, b, args, c, kwargs
x=x+1
return x print(func(1, c=10)) print(func(1, 2, 3, 4, c=20, d=30))
print(func()) A. 13 A. (1,2,(3,4),20,{'d':30})
A. 6 B. 12 B. Error
B. 5 C. 16 C. (1,5,(2,3,4),20,{'d':30})
C. UnboundLocalError D. Error D. None
D. NameError
Q7. Q8. Which of the following is true Q9. What is the effect of defining
def func(a, b=[]): about default arguments in a function with a parameter like
[Link](a) Python? def f(a=10, b=20):?
return b A) They must be specified in A) a and b are required positional
x = func(1) every function call. arguments.
y = func(2, []) B) They are evaluated at B) a and b are keyword-only
z = func(3) function definition time. arguments.
print(x, y, z) C) They can only be used with C) a and b have default values
A. [1,3] [2] [1,3] B. [1] [2] [3] positional arguments. and are optional.
C. [1,3] [2] [3] D. Error D) They are always integers. D) a and b become global
variables.
Q10. Given def g(a, b, c=3, d=4):, Q11. How can you modify a Q12. Which statement correctly
which of the following calls is global variable inside a function? describes the scope of variables in
Python?
valid? A) Use the keyword global A) Global variables are accessible only
A) g(1, 2) before the variable name. in the module they are defined.
B) g(1, b=2, c=5) B) Rebind the name without any B) Local variables declared in a function
C) g(a=1, 2, c=5) keyword. are accessible throughout the program.
D) g(1, 2, 3, 4, 5) C) It’s not possible to modify C) Variables defined in a function are
local to that function unless declared
globals inside a function. global.
D) Use a nonlocal keyword. D) There is no distinction between global
and local variables in Python.
Q13. If a function has both global Q14. What is the output? Q15.
and local variables with the same def modify_list(lst=[]): x = 10
name, what determines which one is [Link](1) def f():
used inside the function? x = 20
return lst
A) The one defined last in the file Assertion (A): The global variable x
result1 = modify_list()
B) The one assigned to within the remains unchanged after calling f( ).
function (local) unless declared
result2 = modify_list() Reason (R): Assignment inside a
global print(result1, result2) function creates a local variable
C) The global variable is always used A) [1] [1] B) [1, 1] [1, 1] unless declared global.
D) The interpreter randomly chooses C) [1] [1, 1] D) [ ] [1]

Both A and R true, R explains A


Q16. Q17. Q18.
x = 10 def f(x): def f(a, b=[], c={}):
def f(): x += 5 [Link](a)
print(x) return x c[a] = a
x = 5 a = 10 return b, c
f() Assertion (A): Value of a remains print(f(1))
Assertion (A): This code raises unchanged after f(a). print(f(2))
an error. Reason (R): Integers are
A. ([1], {1:1}) then ([2], {2:2})
Reason (R): Python treats x as immutable in Python.
local because it is assigned later B. ([1], {1:1}) then ([1,2], {1:1,2:2})
in the function. C. Error
Both A and R true, R explains A D. Depends on interpreter
Both A and R true, R explains A
Q19. Q20. Q21.
x = [1, 2] def f(a, b, c=3): Which order does Python follow for
def modify(lst): return a + b + c variable resolution inside a function?
[Link](3) print(f(1, c=5, b=2)) A. Local → Enclosing → Global → Built-
def reassign(lst): print(f(1, b=2, 4)) in
B. Global → Local → Built-in → Enclosing
lst = [4, 5]
C. Built-in → Global → Local → Enclosing
modify(x) D. Enclosing → Local → Global → Built-in
reassign(x) Q16. First function call output?
print(x) A. 8 B. 6 C. Error D. 10 Q22.
What happens with default arguments in
Q10. What is the final output? Q17. Second function call result? Python functions?
A. [1, 2, 3] B. [4, 5] A. They are evaluated at runtime for each call
A. 7 B. SyntaxError B. They are evaluated once at function
C. [1, 2] D. Error C. TypeError D. 9 definition time
Q11. Which statements are C. They are evaluated only if explicitly used
correct? D. They are always immutable
Q18. Which concept is tested
A. modify changes original list here? Q23.
B. reassign creates new local list A. Argument order rules Which is NOT allowed with functions in
C. Lists are mutable B. Keyword vs positional conflict Python?
D. Both functions modify global A. Passing as arguments
C. Default arguments B. Returning from other functions
list D. All of the above C. Assigning to variables
Q12. Why does reassign not D. Modifying function code at runtime
affect x? directly
A. It creates a new reference
Q24.
locally What distinguishes a method from a
B. Lists are immutable function?
C. Function arguments are copied A. Methods are faster
D. Python uses pass-by-value B. Methods belong to objects/classes
C. Functions cannot return values
D. Methods cannot take parameters
Q25. Q26.
Assertion (A): Using global allows
modification of a global variable
inside a function.
Reason (R): Without global,
assignments create a local variable.
Both A and R true, R explains A

You might also like