0% found this document useful (0 votes)
13 views10 pages

Python Function Basics and Examples

The document contains a series of Python questions focused on functions, including their usage, syntax, and output of specific code snippets. It covers topics such as defining functions, using lambda expressions, and understanding docstrings. The questions are multiple-choice and test knowledge on various aspects of Python functions.

Uploaded by

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

Python Function Basics and Examples

The document contains a series of Python questions focused on functions, including their usage, syntax, and output of specific code snippets. It covers topics such as defining functions, using lambda expressions, and understanding docstrings. The questions are multiple-choice and test knowledge on various aspects of Python functions.

Uploaded by

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

Python Questions – Function

1. Which of the following is the use of function in python?


a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned

2. Which keyword is used for function?


a) Fun
b) Define
c) def
d) Function

3. What will be the output of the following Python code?

def sayHello():
print('Hello World!')
sayHello()
sayHello()

a)

Hello World!
Hello World!

b)

'Hello World!'
'Hello World!'
c)

Hello
Hello
d) None of the mentioned

4. 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')
printMax(3, 4)
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned

5. What will be the output of the following Python code?

x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
a)

x is 50
Changed local x to 2
x is now 50
b)

x is 50
Changed local x to 2
x is now 2
c)

x is 50
Changed local x to 2
x is now 100
d) None of the mentioned

6. 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('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

7. What will be the output of the following Python code?

def say(message, times = 1):


print(message * times)
say('Hello')
say('World', 5)
a)

Hello
WorldWorldWorldWorldWorld
b)

Hello
World 5
c)

Hello
World,World,World,World,World
d)

Hello
HelloHelloHelloHelloHello

8. 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)

func(3, 7)
func(25, c = 24)
func(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 mentioned

9. What will be the output of the following Python code?

def maximum(x, y):


if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y

print(maximum(2, 3))
a) 2
b) 3
c) The numbers are equal
d) None of the mentioned
10. Which of the following is a feature of DocString?
a) Provide a convenient way of associating documentation with Python modules, functions,
classes, and methods
b) All functions should have a docstring
c) Docstrings can be accessed by the __doc__ attribute on objects
d) All of the mentioned

11. Which are the advantages of functions in python?


a) Reducing duplication of code
b) Decomposing complex problems into simpler pieces
c) Improving clarity of the code
d) All of the mentioned

12. What are the two main types of functions?


a) Custom function
b) Built-in function & User defined function
c) User function
d) System function

13. Where is function defined?


a) Module
b) Class
c) Another function
d) All of the mentioned

14. What is called when a function is defined inside a class?


a) Module
b) Class
c) Another function
d) Method

15. Which of the following is the use of id() function in python?


a) Id returns the identity of the object
b) Every object doesn’t have a unique id
c) All of the mentioned
d) None of the mentioned

16. Which of the following refers to mathematical function?


a) sqrt
b) rhombus
c) add
d) rhombus

17. What will be the output of the following Python code?

def cube(x):
return x * x * x
x = cube(3)
print (x)
a) 9
b) 3
c) 27
d) 30

18. What will be the output of the following Python code?

def C2F(c):
return c * 9/5 + 32
print(C2F(100))
print(C2F(0))
a)

212.0
32.0
b)

314.0
24.0
c)

567.0
98.0
d) None of the mentioned

19. What will be the output of the following Python code?

def power(x, y=2):


r=1
for i in range(y):
r=r*x
return r
print (power(3))
print (power(3, 3))
a)

212
32
b)

9
27
c)

567
98
d) None of the mentioned

20. What will be the output of the following Python code?

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

Function returns the sum of all values


6
15
b)

6
100
c)

123
12345
d) None of the mentioned
[Link] supports the creation of anonymous functions at runtime, using a construct called
__________
a) lambda
b) pi
c) anonymous
d) none of the mentioned

22. What will be the output of the following Python code?

y=6
z = lambda x: x * y
print (z(8))
a) 48
b) 14
c) 64
d) None of the mentioned

23. What will be the output of the following Python code?


lamb = lambda x: x ** 3
print(lamb(5))
a) 15
b) 555
c) 125
d) None of the mentioned

24. Does Lambda contains return statements?


a) True
b) False

25. Lambda is a statement.


a) True
b) False

26. Lambda contains block of statements.


a) True
b) False

27. What will be the output of the following Python code?

def f(x, y, z): return x + y + z


print(f(2, 30, 400))
a) 432
b) 24000
c) 430
d) No output

28. What will be the output of the following Python code?

def writer():
​ title = 'Sir'
​ name = (lambda x:title + ' ' + x)
​ return name

who = writer()
print(who('Arthur'))
a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned

29. What will be the output of the following Python code?

L = [lambda x: x ** 2,
lambda x: x ** 3,
lambda x: x ** 4]

for f in L:
​ print(f(3))
a)

27
81
343
b)

6
9
12
c)

9
27
81
d) None of the mentioned

30. What will be the output of the following Python code?

min = (lambda x, y: x if x < y else y)


print(min(101*99, 102*98))
a) 9997
b) 9999
c) 9996
d) None of the mentioned

Common questions

Powered by AI

A function decorator in Python can be implemented as follows: def decorator_function(original_function): def wrapper_function(): print('Wrapper executed this before {}'.format(original_function.__name__)) return original_function() return wrapper_function @decorator_function def display(): print('Display function ran') Decorators enhance or modify the behavior of functions or methods without changing their code. They are particularly useful for cross-cutting concerns like logging, access control, and authentication, abstracting repeated patterns away from business logic .

The function 'printMax(3, 4)' demonstrates decision structures through the use of if-elif-else statements. It compares two values and prints the larger one, or indicates equality if they are the same. This highlights Python's use of conditional logic to control the flow of execution based on conditions: '4 is maximum' indicates that the 'else' condition was met (since 3 < 4), illustrating the transition between if statements .

The docstring of a function in Python can be accessed using the '__doc__' attribute. This feature is significant because it provides a convenient way to associate documentation with Python modules, functions, classes, and methods, improving code readability and maintainability by enabling better in-line documentation. For instance, it helps developers quickly understand the expected usage of a function without digging into the code logic .

Lambda functions in Python are anonymous functions defined using the 'lambda' keyword. They are used for creating small, one-time, and single-line functions often used in higher-order functions like map, filter, or reduce. Unlike regular functions, lambda functions can have only one expression and do not contain return statements explicitly. They are especially useful in scenarios where a simple function is needed for a short period and hooking up an entire function definition would be overkill. An example is sorting based on a custom key: sorted(list_of_dicts, key=lambda x: x['key']).

Higher-order functions in Python are functions that either take other functions as arguments or return them as results. These are pivotal in functional programming, enabling tasks like operations on collections without explicitly using loops. A classic example is `map`, which applies a given function to all items in an input list. For instance, `list(map(lambda x: x**2, [2, 3, 4]))` results in `[4, 9, 16]`. Higher-order functions promote concise and expressive code that is both readable and maintainable .

Default arguments in Python functions are specified by assigning a default value to one or more parameters in the function definition. This allows the function to be called with fewer arguments than parameters if defaults are provided. For example, in the function 'def func(a, b=5, c=10):', 'b' and 'c' have default values. This approach enhances function flexibility and reduces the need for overloaded functions or repeated logic. It simplifies usage and API design, especially for operations where standard parameters satisfy most use cases .

The 'global' keyword in Python allows a function to modify a variable outside its local scope, effectively making it a global variable within that function. When a variable is declared as global, any changes to it within the function affect the variable globally across the application. For example, in the code: x = 50 def func(): global x print('x is', x) x = 2 print('Changed global x to', x) The variable 'x' is modified to 2 globally, so any references to 'x' outside 'func' will reflect this change .

Functions in Python are primarily used to reduce duplication of code, decompose complex problems into simpler pieces, and improve clarity of the code. These properties help in achieving better modularity, reusability, and maintainability of the code .

No, lambda functions in Python cannot have multiple expressions or statements. They are limited to a single expression, which makes them suitable for simple operations. This limitation is by design as lambda functions are intended for concise, small-scale code rather than multi-step operations, which should be handled by proper functions with named definitions and full syntactic support .

Using the 'def' keyword is preferable for defining functions, particularly when the function requires multiple statements, complex logic, or needs to be reused across different parts of the program. 'Def' allows for more readability, debugging capability, and the inclusion of multiple expressions and proper logical handling. In contrast, lambda functions are confined to a single expression and lack comprehensive support for detailed logic, making them impractical for anything beyond simple operations .

You might also like