Python Function Q&A - 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Q-1.

  What is the output of the following code snippet?

def func( mylist ):

"This changes a passed list into this function"

mylist = [1,2,3,4]; # This would assign new reference in


mylist

print ("Values inside the function: ", mylist)

return

mylist = [10,20,30];

func( mylist );

print ("Values outside the function: ", mylist)

A. Values inside the function:  [1, 2, 3, 4]


Values outside the function:  [10, 20, 30]
B. Values inside the function:  [10, 20, 30]
Values outside the function:  [10, 20, 30]
C. Values inside the function:  [1, 2, 3, 4]
Values outside the function: [1, 2, 3, 4]
D. None of the above
Click here to view the answer.
Answer. A
 

Q-2. What is the output of the following code snippet?

x = 50

def func():

global x
print('x is', x)

x = 2

print('Changed global x to', x)

func()

print('Value of x is', x)

A. x is 50
Changed global x to 2
Value of x is 50
B. x is 50
Changed global x to 2
Value of x is 2
C. x is 50
Changed global x to 50
Value of x is 50
D. None of the mentioned
Click here to view the answer.
Answer. B
 

Q-3.Which of the following function calls can be used to invoke the below
function definition?

def test(a, b, c, d)

A. test(1, 2, 3, 4)
B. test(a = 1, 2, 3, 4)
C. test(a = 1, b = 2, c = 3, 4)
D. test(a = 1, b = 2, c = 3, d = 4)
E. test(1, 2, 3, d = 4)
Click here to view the answer.
Answer. A,D and E
B and C leads to SyntaxError: positional argument follows keyword argument
 

Q-4. What is the output of the following code snippet?

def test(a, b=5, c=10):

print('a is', a, 'and b is', b, 'and c is', c)


test(3, 7)

test(25, c = 24)

test(c = 50, a = 100)

A. a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
B. a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
C. 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
D. None of the above
Click here to view the answer.
Answer. C
 

Q-5.  What is the value of num after the function call?

def myfunc(text, num):

while num > 0:

num = num - 1

num=4

myfunc('Hello', num)

A. 4
B. 3
C. 0
D. 1
Click here to view the answer.
Answer. A
 
Q-6. What is the output of the following code snippet?

def func(x = 1, y = 2):

return x + y, x - y

x, y = func(y = 2, x = 1)

print(x, y)

A. 1 3
B. 3 1
C. The program has a runtime error because the function returns the multiple values
D. 3 -1
E. -1 3
Click here to view the answer.
Answer. D
 

Q-7. What is the output of the following code snippet?

def func():

text = 'Welcome'

name = (lambda x:text + ' ' + x)

return name

msg = func()

print(msg('All'))

A. Welcome All
B. All Welcome
C. All
D. Welcome
Click here to view the answer.
Answer. A
Q-8. What is the output of the following code snippet?
min = (lambda x, y: x if x < y else y)
print(min(101*99, 102*98))

A. 9997
B. 9999
C. 9996
D. 9998

Click here to view the answer.


Answer. C

Q-9. What is the output of the following code snippet?


def func(x, y=2):
num = 1
for i in range(y):
num = num * x
return num
print (func(4))
print (func(4, 4))

A. 8
16
B. 16
256
C. 32
1024
D. 128
1256

Click here to view the answer.


Answer. B

Q-10. What is the output of the following code snippet?


def add(*args):
'''Function returns the addition
of all values'''
r = 0
for i in args:
r += i
return r
print (add.__doc__)
print (add(1, 2, 3))
print (add(1, 2, 3, 4, 5))

A. Function returns the addition


of all values
6
15
B. 6
15
C. 123
12345
D. 6
120

Click here to view the answer.


Answer. A

Q-11. What is the output of the following code snippet?


def addFunc(item):
item += [1]

mylist = [1, 2, 3, 4]
addFunc(mylist)
print (len(mylist))

A.  2
B.  4
C.  5
D.  An exception is thrown

Click here to view the answer.


Answer. C

Q-12. What is the output of the following code snippet?


def heading(str):
print ("+++%s+++" % str)
heading.id = 1
heading.text = "Python functions"
heading("%d %s" % (heading.id, heading.text))
A.  +++%s+++
B.  Python functions
C.  +++Python functions+++
D.  +++1 Python functions+++

Click here to view the answer.


Answer. D

Q-13. What is the order of usage of  *args, **kwargs, and formal args in
function header?

A.  some_func(fargs, *args, **kwargs)


B.  some_func(**kwargs, *args, fargs)
C.  some_func(*args, **kwargs, fargs)
D.  some_func(*args, fargs, **kwargs)

Click here to view the answer.


Answer. A

Q-14. What is the output of the following code snippet?


def test_var_args(param1, *args):
print (type(args))

test_var_args('delhi', 'noida', 'gurgaon', 'ghaziabad')

A.  str
B.  int
C.  tuple
D.  list
E.  dict

Click here to view the answer.


Answer. C

Q-15. What is the output of the following code snippet?


def test_var_args(param1, **kwargs):
print (type(kwargs))
test_var_args('capitals', India='New Delhi',
Australia='Canberra', China='Beijing')

A.  str
B.  int
C.  tuple
D.  list
E.  dict

Click here to view the answer.


Answer. E

Q-16. What is the output of the following code snippet?


def test_var_args(farg, *args):
print ("formal arg:", farg)
for arg in args:
print ("another arg:", arg)
test_var_args(1, "two", 3)

A.  formal arg: 1


another arg: two
another arg: 3
B.  formal arg: 1
another arg: two
C.  An exception is thrown
D.  None of the above

Click here to view the answer.


Answer. A

Q-17. What is the output of the following code snippet?


def test_var_kwargs(farg, **kwargs):
print ("formal arg:", farg)
for key in kwargs:
print ("another keyword arg: %s: %s" % (key, kwargs[key]))
test_var_kwargs(farg=1, myarg2="two", myarg3=3)

A.  formal arg: 1


another keyword arg: myarg2: two
another keyword arg: myarg3: 3
B.  formal arg: 1
another keyword arg: myarg2: two
C.  An exception is thrown
D.  None of the above

Click here to view the answer.


Answer. A

Q-18. What is the output of the following code snippet?


myList = [1, 2, 3, 4, 5]
def func(x):
x.pop()
x.pop()
x.insert(-1, 0)
print ("Inside func():", x)

func(myList)
print ("After function call:", myList)

A.  Inside func(): [1, 2, 3, 0]


After function call: [1, 2, 3, 4, 5]
B.  Inside func(): [0, 1, 2, 3]
After function call: [0, 1, 2, 3]
C.  Inside func(): [1, 2, 3, 0]
After function call: [1, 2, 3, 0]
D.  Inside func(): [1, 2, 0, 3]
After function call: [1, 2, 0, 3]

Click here to view the answer.


Answer. D

Q-19. What is the output of the following code snippet?


nums = range(2, 50)
for i in range(2, 8):
nums = list(filter(lambda x: x == i or x % i, nums))
print (nums)

A. [2, 3, 4, 5, 6, 7, 8]
B. [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
C. [2, 3, 5, 6, 7, 10, 15, 19, 20, 27, 35, 41, 49]
D. [2, 3, 5, 9, 13, 17, 26, 31, 35, 37, 41, 43, 47, 49]

Click here to view the answer.


Answer. B

Q-20. What is the output of the following code snippet?


text = 'Welcome to the world of Python'
words = text.split()
length = list(map(lambda word: len(word), words))
print (length)

A. [7, 2, 3, 5, 2, 6]
B. [30]
C. [7, 3, 4, 5, 6, 3]
D. [30, 23, 20, 15, 13, 7]

Click here to view the answer.


Answer. A

Q-21. What is the output of the following code snippet?


from functools import reduce
f = lambda a,b: a if (a > b) else b
num = reduce(f, [47,11,42,102,13])
print (num)

A. 47
B. 42
C. 102
D. 11

Click here to view the answer.


Answer. C

You might also like