1. Which of the following is NOT an advantage of using functions in a program?
(a) Increases readability of the program
(b) Reduces code length
(c) Increases reusability
(d) Increases the number of lines in the program
2. A function definition in Python begins with the keyword:
(a) function
(b) def
(c) define
(d) func
3. In the function header def mixedFraction(num, deno = 1):, the parameter deno is an example of:
(a) positional parameter
(b) default parameter
(c) local variable
(d) global variable
4. Which of the following statements is correct regarding default parameters in Python functions?
(a) Default parameters must be the first parameters in the function header.
(b) Default parameters must be the trailing parameters in the function header.
(c) Any parameter can have a default value irrespective of its position.
(d) Default parameters cannot be used with user-defined functions.
5. Assertion (A): A function must be defined before it is called in a Python program.
Reason (R): The Python interpreter executes statements from top to bottom, and a function call transfers control to the function definition.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
6. A variable defined inside a function is called a:
(a) global variable
(b) local variable
(c) built-in variable
(d) module variable
7. Which of the following functions is used to find the identity of an object in Python?
(a) id()
(b) type()
(c) len()
(d) range()
8. Assertion (A): Multiple values can be returned from a function using a tuple.
Reason (R): The return statement can return only one value at a time.
(a) Both A and R are true and R is the correct explanation of A.
(b) Both A and R are true but R is not the correct explanation of A.
(c) A is true but R is false.
(d) A is false but R is true.
9. The statement used to import only specific functions from a module is:
(a) import modulename
(b) from modulename import functionname
(c) import functionname from modulename
(d) include modulename
10. Which of the following is a built-in module in Python?
(a) math
(b) tent
(c) canvas
(d) calc
1. Define the term 'modular programming' as described in the chapter.
2. Differentiate between a parameter and an argument with reference to user-defined functions.
3. What is the difference between a void function and a function that returns a value?
4. Write the output of the following code fragment and state why the output is generated:
python
def helloPython():
print("I love Programming")
helloPython()
5. What is the role of the return statement in a user-defined function?
6. Differentiate between a built-in function and a user-defined function.
1. Identify the error in the following code and rewrite the corrected code:
python
def create(text, freq):
for i in range(1, freq):
print(text)
create(5)
2. Write the output of the following code and explain why the id values change or remain the same:
python
def incrValue(num):
print("id before increment =", id(num))
num = num + 5
print("id after increment =", id(num))
number = 8
incrValue(number)
3. What will be the output of the following code? Explain briefly.
python
def sumSquares(n):
sum = 0
for i in range(1, n+1):
sum = sum + i
print("The sum of first", n, "natural numbers is:", sum)
num = 5
sumSquares(num)
4. Identify the error in the following code and give the corrected version:
python
def calcInterest(principal = 1000, rate, time = 5):
interest = principal * rate * time / 100
print(interest)
calcInterest(5000, 0.05)
5. Write the output of the following code fragment:
python
def fullname(first, last):
fullname = first + " " + last
print("Hello", fullname)
first = "Gyan"
last = "Vardhan"
fullname(first, last)
1. Write a complete Python program using a user-defined function calcFact(num) that accepts a positive integer as argument and displays its factorial. The program should accept the number from the user and call the function.
2. Explain the flow of execution in a Python program that contains user-defined functions. Illustrate your answer with a dry-run using a trace table for the following code:
python
def addnum(a, b):
return a + b
x = 5
y = 7
result = addnum(x, y)
print(result)
3. Write a complete Python program that creates a user-defined module named basic_math containing four functions: addnum(x,y), subnum(x,y), multnum(x,y) and divnum(x,y). Also include a docstring describing the module. Demonstrate importing and using the functions.
Case 1: A company wants to calculate the payable amount for tents using modular programming. The program accepts height, radius and slant height, calculates canvas area using two separate functions for cylindrical and conical parts, computes total cost and finally adds 18% tax using another function.
(a) Identify the three user-defined functions used in the modular approach.
(b) What is the advantage of using separate functions for cylindrical and conical areas?
(c) Write the function header and body for the function that computes the payable amount after tax.
(d) If the same tax rate is to be used in another program, how can the function be reused?
Case 2: A student is creating a program to simulate a traffic light using two user-defined functions trafficLight() and light(colour). The program accepts a colour and displays appropriate messages.
(a) What value does the function light("RED") return?
(b) Which function calls the other function and how?
(c) Write the complete code for the function light(colour).
(d) Why is it important to define functions before calling them in this program?
return statement to send value(s) back to the caller. I love Programming — because the function is called after its definition and the print statement inside the function is executed. return statement transfers control back to the calling function and sends value(s) or None to the caller. input(), print()); user-defined functions are written by the programmer using def.create(5) passes only one argument but function expects two. Corrected code:python
def create(text, freq):
for i in range(1, freq):
print(text)
create("Hello", 5)num = num + 5 creates a new object. The sum of first 5 natural numbers is: 15 — because the loop adds numbers from 1 to 5. principal appears before non-default parameter rate. Corrected:python
def calcInterest(rate, principal=1000, time=5):
interest = principal * rate * time / 100
print(interest)Hello Gyan Vardhan — because the two strings are concatenated with a space inside the function.num = int(input("Enter the number: "))
calcFact(num)
```
2. Flow of execution: statements are executed top to bottom. When a function call is encountered, control jumps to the function definition, executes it, then returns to the point after the call. Trace table:
| Line | x | y | result |
|------|---|---|--------|
| 4 | 5 | | |
| 5 | 5 | 7 | |
| 6 | 5 | 7 | 12 |
| 7 | 5 | 7 | 12 |
basic_math.py with docstring and four functions as described in Program 7-16 of the chapter.(a) cyl(h,r), con(l,r), post_tax_price(cost)
(b) Increases reusability and readability; same conical function can be reused for other tent types.
(c) def post_tax_price(cost): tax = 0.18 * cost; net_price = cost + tax; return(net_price)
(d) By importing the module containing the function or copying the function definition.
(a) 0
(b) trafficLight() calls light(signal)
(c)
python
def light(colour):
if colour == "RED":
return 0
elif colour == "YELLOW":
return 1
else:
return 2
(d) Python executes code top to bottom; function must be defined before it is called, otherwise NameError occurs.
All questions are answerable from the NCERT chapter text. Reviewed by GFIS faculty.