Comp Science (week 9)
Where is function defined in Python? Module Class Another function All of the mentioned
All of the mentioned
What are the two main types of functions in Python?
Built-in function & User defined function
A function definition must be evaluated by the interpreter before the function can be called.
True
A local variable is defined inside a function, while a global variable is defined outside any function.
True
Which correctly defines two parameters x and y for a function definition: def calc_val( ):
calc_val (x, y) :
Given a function definition:def calc_val(a, b, c):and given variables i, j, and k, which are valid arguments in the call calc_val( )?
calc_val( k, i+ j, 99 )
What will be the output of the following Python code? x = 50 def func( ): global x print('x is', x) x = 2 print('Changed global x to', x) func( ) print('x is now', x)
x is 50 Changed global x to 2 Value of x is 2
What will be the output of the following Python code? x = 50def func(x):print('x is', x)x = 2print('Changed local x to', x) func(x)print('x is now', x)
x is 50 Changed local x to 2 x is now 50
What will be the output of the following Python code? def change(x = 0, y = 1): x = x + y y = y + 1 print(x, y) change(x = 0 , y = 1) change(x = 1 , y = 0) change(y = 1 , x = 0) change(y = 0 , x = 1)
1 2 1 1 1 2 1 1
What will be the output of the following Python code? def change(a, b): a = a + 1 b = b + 1 print(a, b) change( a = 1, b = 0 ) change( a = 0, b = 1 )
2 1 1 2
What will be the output of the following Python code? def printMax(a, b):if a > b:print(a, 'is maximum')elif a == b:print(a, 'is equal to', b)else:print(b, 'is maximum') x = 3 y = 4 printMax( x+1, y)
4 is equal to 4
What will be the output of the following Python code? def foo( arg1, *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) )
5 14
What will be the output of the following Python code? def foo( *args ): result = 0 for i in args: result += i return result print( foo(1, 2, 3) ) print( foo(1, 2, 3, 4, 5) )
6 15
What will be the output of the following Python code? def power(x, y=2): result = 1 for i in range(y): result = result * x return result print( power(3) ) print( power(3, 3) ) print( power(4) ) print( power(4, 3) )
9 27 16 64
What will be the output of the following Python code? def say( message, times = 1): print(message * times) say('Hello') say('World', 5)
Hello WorldWorldWorldWorldWorld
What will be the output of the following Python code? def foo( i, x=[ ]): x.append(i) return x if __name__ == "__main__": for i in range( 3 ): print( foo( i ) )
[0] [0, 1] [0, 1, 2]
What will be the output of the following Python code? def func(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c)f unc(3, 7) func(25, c = 24) func(c = 50, a = 100)
a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
Which are the advantages of functions in python? Reducing duplication of code Decomposing complex problems into simpler pieces Improving clarity of the code All of above
all of the above
Which keyword is used for function?
def
The function must return a value.
false