Sheet Function
Sheet Function
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.
Answer: c
Explanation: None.
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.
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
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.
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.
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
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.
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
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
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