Hey, there experienced Python programmers, are you ready to test your skills with our Python Functions Quiz Part-2?
This quiz features 21 medium to high-complexity questions that will put your knowledge to the test. But before you dive in, let’s talk about the importance of functions in Python.
Functions are a powerful tool that helps organize your code into logical blocks, making it easier to read, modularize, and reusable. They are also a great way to create interfaces for other programmers.
Did you know that in Python, functions can return multiple values? It’s true! Return the results as a tuple.
And if you ever need to list down the functions of a module, you can use the <getmembers()> method of the <inspect> module.
So, if you’re up for a challenge, take our Python Functions Quiz Part-2 now. Good luck and have fun!
Take on our Advance Level Python Functions Quiz Part-2!
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
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('The value of x is', x)
- A. x is 50
Changed global x to 2
The value of x is 50 - B. x is 50
Changed global x to 2
The value of x is 2 - C. x is 50
Changed global x to 50
The value of x is 50 - D. None of the mentioned
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)
Answer. A, D, and E
Note: B and C lead 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
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
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
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
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
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
Q-10. What is the output of the following code snippet?
def add(*args): '''The 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. The function returns the addition
of all values
6 15 - B. 6 15
- C. 123 12345
- D. 6 120
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
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+++
Q-13. What is the order of usage of *args, **kwargs, and formal args in the function header?
- A. some_func(formal_args, *args, **kwargs)
- B. some_func(**kwargs, *args, formal_args)
- C. some_func(*args, **kwargs, formal_args)
- D. some_func(*args, formal_args, **kwargs)
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
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
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
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
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 the function call:", myList)
- A. Inside func(): [1, 2, 3, 0]
After the function call: [1, 2, 3, 4, 5] - B. Inside func(): [0, 1, 2, 3]
After the function call: [0, 1, 2, 3] - C. Inside func(): [1, 2, 3, 0]
After the function call: [1, 2, 3, 0] - D. Inside func(): [1, 2, 0, 3]
After function call: [1, 2, 0, 3]
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]
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]
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
Level Up Your Python Function Skills: Recap of Our Exciting Quiz!
Congratulations on completing the Python Functions Quiz Part-2! We hope you had a blast and learned some new things.
Now that you’ve honed your skills, why not try out our other exciting Python programming quizzes? Keep exploring and leveling up your skills!
Suggested Readings
- 30 Questions on Python List, Tuple, and Dictionary
- Quiz on Python Functions Part I
- Python Quiz for Beginners Part II
- File Handling in Python Part I
- File Handling in Python Part II
- Basic Linux Questions and Answers
Keep up the learning momentum and stay curious!
Cheers,
TechBeamers