Python Function Questions and Answers

Teachers and Examiners (CBSESkillEduction) collaborated to create the Python Function Questions and Answers. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

Python Function Questions and Answers

1. Observe the following programs carefully, and identify the error:
a) def create (text, freq):
for i in range (1, freq):
print text
create(5) #function call
Answer –
There are two errors in the program –
i) The arguments in not declare properly.
ii) In the print statement text sould be written in double quotes print “text”

b) from math import sqrt,ceil
def calc():
print cos(0)
calc() #function call
Answer –
There are two errors in the above program –
i) In the header import function is written defferent and the function used in the prgram is different
ii) The declaration of print statement is wrong example, print(cos(0))

c) mynum = 9
def add9():
mynum = mynum + 9
print mynum
add9() #function call
Answer –
There are two errors in the above program –
i) mynum variable is declare outside of the function, if you want to use then you have to used global variable.
ii) Print statment is written wrong. It should be print(mynum)

d) def findValue( vall = 1.1, val2, val3):
final = (val2 + val3)/ vall
findvalue() #function call
Answer –
There are three errors in the above program –
i) Inside the function there are three parameters but in the calling function no parameters are declare
ii) There are no arguments declare in the calling function
iii) ‘vall’ is already initialized as ‘vall = 1.0’, it is called ‘Default parameters’

e) def greet():
return(“Good morning”)
greet() = message #function call
Answer –
There are one errers in the above prgram –
i) Insilizetion is not possible in fucntion for example message = greet()

Python Function Questions and Answers

2. How is math.ceil(89.7) different from math.floor(89.7)?
Answer – math.ceil(89.7) will give output in whole number “90”, but math.floor(89.7) will given output “89”

3. Out of random() and randint(), which function should we use to generate random numbers between
1 and 5. Justify.
Answer – randint(), and random() functions are used to return from a given range of integers. The function “randint (a, b)” will produce a random integer with the specified parameters. The function “random ()” produces a random floating point number between 0 to1.

4. How is built-in function pow() function different from function math.pow() ? Explain with an example.
Answer – Function pow() will return number while math.pow() function will return value in decimal.
Example –
i) print(pow(3, 3)) will return 27
ii) print(math.pow(3, 3)) will return 27.0

5. Using an example show how a function in Python can return multiple values.
Answer –
def myfunction ( ) :
return 1, 2, 3
a, b, c = myfunction ( )
print ( a )
print ( b )
print ( c )

Python Function Questions and Answers

6. Differentiate between following with the help of an example:
a) Argument and Parameter
b) Global and Local variable
Answer –
a) Arguments are the values that are declared inside a function at the time the function is called. In comparison, a parameter is a set of variables that are specified at the time the function is called.
Example –
def myfunction(a,b):
return a + b
sum = myfunction(2, 4)
print(“Sum is = “, sum)

b) A variable that is defined outside of any function or block is referred to as a global variable. Any change made to the global variable will impact all the functions in the program.

A local variable is one that is declared inside any function or block. Only the function or block where it is defined, can access it.
Example –
str = “Good Morning” # Global Variable
def myfunction():
str1 = “Good Night” #Local Variable
print(str1) #local variable accessed
print(str) #Global variable accessed

7. Does a function always return a value? Explain with an example.
Answer – It’s not necessary for a function to return a value. The return statement is used to return a value in a user-defined function.

Suggested Lab. Exercises

1. Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.
Answer –
def myfunction(num):
if num % 7 == 0:
print(“Divisible by 7”)
print(“Not divisible by 7”)
n = int(input(“Enter number”))

Enter number 7
Divisible by 7

Enter number8
Not divisible by 7

2. Write a program that uses a user defined function that accepts name and gender (as M for Male, F for Female) and prefixes Mr/Ms on the basis of the gender.
Answer –
def myfunction(name, gender):
if gender == ‘F’:
print(“Name is :”, “Ms. ” + name)
elif gender == ‘M’:
print(“Name is :”, “Mr. ” + name)
print(“Select only M/F in Capital as gender”)
n = input(“Enter name “)
g = input(“Enter gender M/F in Capital “)
myfunction(n, g)

Enter name Anurag
Enter gender M/F in Capital M
Name is : Mr. Anurag

Python Function Questions and Answers

3. Write a program that has a user defined function to accept the coefficients of a quadratic equation in variables and calculates its determinant. For example : if the coefficients are stored in the variables a,b,c then calculate determinant as b2-4ac. Write the appropriate condition to check determinants on positive, zero and negative and output appropriate result.
Answer –
def myfunction(a, b, c):
determinant = b**2 – 4 * a * c
if (determinant > 0):
print(“The quadratic equation has two real roots.”)
elif (determinant == 0):
print(“The quadratic equation has one real root.”)
print(“The quadratic equation doesn’t have any real root.”)

a = int(input(“Enter the value of a: “))
b = int(input(“Enter the value of b: “))
c = int(input(“Enter the value of c: “))

Enter the value of a: 2
Enter the value of b: 5
Enter the value of c: 7
The quadratic equation doesn’t have any real root.

4. ABC School has allotted unique token IDs from (1 to 600) to all the parents for facilitating a lucky draw on the day of their Annual day function. The winner would receive a special prize. Write a program using Python that helps to automate the task.(Hint: use random module)
Answer –
import random
winner = random.randint(1, 600)
print(” Winner Token IDs : “, winner)

Winner Token IDs : 505

Python Function Questions and Answers

5. Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is compounded to calculate and displays compound interest.
(Hint: CI = ((P*(1+R/N))NT)
Answer –
def myfunction(principal, interest, year, n):
amount = principal * (1 + interest/100) ** n * year
compoundint = amount – principal
print(“Compound Interest is : “, compoundint)
principal = int(input(“Enter Principal amount : “))
interest = int(input(“Enter Rate of interest : “))
year = int(input(“Enter time period in years : “))
n = int(input(“Enter number of times in which interest is compounded”))
myfunction(principal, interest, year, n)

Enter Principal amount : 5000
Enter Rate of interest : 8
Enter time period in years : 5
Enter number of times in which interest is compounded5
Compound Interest is : 31733.2

Python Function Questions and Answers

6. Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapped and returned, i.e., number 2 is returned in place of number1 and number 1 is reformed in place of number 2, otherwise the same order is returned.
Answer –
def myfunction(a, b):
if(a < b):
return b, a
return a, b

n1 = int(input(“Enter first Number: “))
n2 = int(input ( “Enter second Number: “))

n1, n2 = myfunction(n1, n2)
print(“First Number: “, n1)
print(“Second Number: “, n2)

Enter first Number : 6
Enter second Number : 3
First Number : 6
Second Number : 3

omputer Science Class 11 Notes
Computer Science Class 11 MCQ
Computer Science Class 11 NCERT Solutions

Computer Science Class 11 Practical Questions and Answers