Python - Built in Function - 14.4
Python - Built in Function - 14.4
1. def sayHello():
2. print('Hello World!')
3. sayHello()
4. sayHello()
a)
Hello World!
Hello World!
b)
'Hello World!'
'Hello World!'
c)
Hello
Hello
1. def sayHello():
2. print('Hello World!') # block belonging to the function
3. # End of function #
4.
5. sayHello() # call the function
6. sayHello() # call the function again
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.
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 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
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
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.
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
In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 7
and c gets the default value of 10.
In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of
the argument. Then, the parameter c gets the value of 24 due to naming i.e. keyword
arguments. The variable b gets the default value of 5.
In the third usage func(c=50, a=100), we use keyword arguments for all specified values.
Notice that we are specifying the value for parameter c before that for a even though a is
defined before c in the function definition.
a) 2
b) 3
c) The numbers are equal
d) None of the mentioned
Answer: b
Explanation: The maximum function returns the maximum of the parameters, in this case the
numbers supplied to the function. It uses a simple if..else statement to find the greater value
and then returns that value.