0% found this document useful (0 votes)
9 views8 pages

Sheet Function

The document contains a series of questions and answers related to Python functions, covering topics such as function definition, global and local variables, default arguments, and lambda functions. Each question is followed by an explanation of the correct answer, providing insights into Python programming concepts. The document serves as a quiz format for learning and testing knowledge on Python functions.

Uploaded by

salmaabo341
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)
9 views8 pages

Sheet Function

The document contains a series of questions and answers related to Python functions, covering topics such as function definition, global and local variables, default arguments, and lambda functions. Each question is followed by an explanation of the correct answer, providing insights into Python programming concepts. The document serves as a quiz format for learning and testing knowledge on Python functions.

Uploaded by

salmaabo341
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
You are on page 1/ 8

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

Answer: a
Explanation: Functions are reusable pieces of programs. They allow you to give a name to a block of
statements, allowing you to run that block using the specified name anywhere in your program and any
number of times.

2. Which keyword is used for function?


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

Answer: c
Explanation: None.

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


1. def printMax(a, b):
2. if a > b:
3. print(a, 'is maximum')
4. elif a == b:
5. print(a, 'is equal to', b)
6. else:
7. print(b, 'is maximum')
8. printMax(3, 4)
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned

Answer: c
Explanation: Here, we define a function called printMax that uses two parameters called a and b. We
find out the greater number using a simple if..else statement and then print the bigger number.
4- What will be the output of the following Python code?
1. x = 50
2. def func(x):
3. print('x is', x)
4. x = 2
5. print('Changed local x to', x)
6. func(x)
7. print('x is now', x)
a) x is now 50
b) x is now 2
c) x is now 100
d) None of the mentioned

Answer: a
Explanation: The first time that we print the value of the name x with the first line in the function’s
body, Python uses the value of the parameter declared in the main block, above the function
definition.
Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value
of x in the function, the x defined in the main block remains unaffected.
With the last print function call, we display the value of x as defined in the main block, thereby
confirming that it is actually unaffected by the local assignment within the previously called function.

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


1. x = 50
2. def func():
3. global x
4. print('x is', x)
5. x = 2
6. print('Changed global x to', x)
7. func()
8. 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

Answer: b
Explanation: The global statement is used to declare that x is a global variable – hence, when we
assign a value to x inside the function, that change is reflected when we use the value of x in the
main block.

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


1. def say(message, times = 1):
2. print(message * times)
3. say('Hello')
4. say('World', 5)
a)
 Hello
 WorldWorldWorldWorldWorld

b)
 Hello
 World 5

c)
 Hello
 World,World,World,World,World

d)
 Hello
 HelloHelloHelloHelloHello

Answer: a
Explanation: For some functions, you may want to make some parameters optional and use default
values in case the user does not want to provide values for them. This is done with the help of default
argument values. You can specify default argument values for parameters by appending to the
parameter name in the function definition the assignment operator (=) followed by the default value.
The function named say is used to print a string as many times as specified. If we don’t supply a value,
then by default, the string is printed just once. We achieve this by specifying a default argument value
of 1 to the parameter times.
In the first usage of say, we supply only the string and it prints the string once. In the second usage of
say, we supply both the string and an argument 5 stating that we want to say the string message 5
times.

8- What will be the output of the following Python code?


def f1(x):
global x
x+=1
print(x)
f1(15)
print("hello")
a) error
b) hello
c) 16
d)
16

hello

Answer: a
Explanation: The code shown above will result in an error because ‘x’ is a global variable. Had it
been a local variable, the output would be: 16
hello

10- What will be the output of the following Python code?


x = 5
def f1():
global x
x = 4
def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)
print(total)
a) Error
b) 7
c) 8
d) 15

Answer: b
Explanation: In the code shown above, the variable ‘x’ has been declared as a global variable under
both the functions f1 and f2. The value returned is a+b+x = 1+2+4 = 7.
11- What will be the output of the following Python code?
x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)
a) 100
b) 90
c) 80
d) Error

Answer: a
Explanation: The output of the code shown above is 100. This is because the variable ‘x’ has been
declared as global within the functions f1 and f2.

13- How are variable length arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
View Answer
Answer: a
Explanation: Refer documentation.

How are keyword arguments specified in the function heading?


a) one-star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
View Answer
Answer: c
Explanation: Refer documentation.

15- What Is The Default Return Value For A Function That Does
Not Return Any Value Explicitly?
A. None
B. int
C. double
D. public
E. null
Click here to view the answer.
Answer. A

17- What Is The Output Of The Following Code Snippet?


def func(message, num = 1):
print(message * num)

func('Welcome')
func('Viewers', 3)
A. Welcome
Viewers
B. Welcome
ViewersViewersViewers
C. Welcome
Viewers,Viewers,Viewers
D. Welcome
Click here to view the answer.
Answer. B

18- What Is The Output Of The Following Code Snippet?


num = 1
def func():
global num
num = num + 3
print(num)

func()
print(num)
A. 1 4
B. 4 1
C. The program has a runtime error because the local variable ‘num’ referenced before
assignment.
D. 1 1
E. 4 4
Click here to view the answer.
Answer. E

19- What Is The Output Of The Following Code Snippet?


exp = lambda x: x ** 3
print(exp(2))
A. 6
B. 222
C. 8
D. None of the above
Click here to view the answer.
Answer. C

21. Does Lambda contains return statements?


a) True
b) False
View Answer
Answer: b
Explanation: lambda definition does not include a return statement. it always contains an expression
which is returned. Also note that we can put a lambda definition anywhere a function is expected.
We don’t have to assign it to a variable at all.

22-. Lambda is a statement.


a) True
b) False
View Answer
Answer: b
Explanation: lambda is an anonymous function in Python. Hence this statement is false.

23-. Lambda contains block of statements.


a) True
b) False
View Answer
Answer: b
Explanation: None.

24- What will be the output of the following Python code?


1. def f(x, y, z): return x + y + z
2. f(2, 30, 400)
a) 432
b) 24000
c) 430
d) No output
View Answer
Answer: a
Explanation: None

You might also like