Professional Documents
Culture Documents
i Pu Cs Chapter 7 Q&A
i Pu Cs Chapter 7 Q&A
FUNCTIONS
VSA SA LA E TOTAL
1 FIB
MCQ
a) Function programming
b) Modular programming
c) Structured programming
d) Object-oriented programming
b) To reduce readability
d) To increase complexity
a) def function_name:
b) function function_name():
c) def function_name():
d) function function_name:
a) Ends the function execution and optionally passes back an expression to the
caller
Answer: a) Ends the function execution and optionally passes back an expression
6. What is the term for a value passed to a function during the function call?
a) Parameter
b) Argument
c) Return value
d) Variable
Answer: b) Argument
8. In the function definition `def add(a, b):`, what are `a` and `b`?
a) Arguments
b) Return values
c) Parameters
d) Variables
PRIMUS PU COLLEGE, BANGALORE
Answer: c) Parameters
a) func
b) define
c) function
d) def
Answer: d) def
Python Code:
def greet(name):
4
return "Hello, " + name
print(greet("Alice"))
a) Hello,
b) Hello, greet
c) Hello, Alice
d) greet(Alice)
a) call my_function()
b) my_function()
c) def my_function()
d) execute my_function()
Answer: b) my_function()
Python Code:
print(result)
a) 8
b) 3
c) 5
PRIMUS PU COLLEGE, BANGALORE
d) add
Answer: a) 8
a) Decreases readability
c) Increases reusability
Python Code:
6
def multiply(a, b):
return a * b
print(multiply(2, 4))
a) 2
b) 4
c) 6
Answer: d) 8
a) It can be reused
d) It helps in modularity
16. What is the main advantage of breaking a program into smaller functions?
17. What is the keyword used to return a value from a function in Python?
a) send
b) yield
PRIMUS PU COLLEGE, BANGALORE
c) return
d) give
Answer: c) return
Python Code:
return a - b
print(subtract(10, 3))
a) 7
8
b) 3
c) 10
d) -7
Answer: a) 7
b) A loop structure
d) A data type
a) def function_name[]:
b) def function_name():
c) function_name():
d) function_name[]:
return a / b
print(divide(10, 2))
a) 5.0
b) 5
PRIMUS PU COLLEGE, BANGALORE
c) 2
d) 0.5
Answer: a) 5.0
a) Declaring a function
b) Executing a function
c) function1
d) 1function
Answer: d) 1function
Python Code:
print("Hello, World!")
say_hello()
a) Hello, World!
b) say_hello
c) print
d) Hello
c) To create a loop
d) To import a module
26. Which of the following functions is used to find the length of a list in Python?
a) length()
PRIMUS PU COLLEGE, BANGALORE
b) len()
c) size()
d) count()
Answer: b) len()
Python Code:
def square(x):
return x * x
print(square(3))
12
a) 3
b) 6
c) 9
d) 12
Answer: c) 9
a) A loop counter
13
30. What will be the output of the following code?
Python Code:
a) Hello
PRIMUS PU COLLEGE, BANGALORE
b) World
c) Hello, World!
d) concat
c) function_name(arg1; arg2)
14
32. What will be the output of the following code?
Python Code:
print(power(2, 3))
a) 6
c) 9
d) 12
Answer: b) 8
15
34. What will be the output of the following code?
Python Code:
def add_five(x):
return x + 5
print(add_five(10))
a) 10
PRIMUS PU COLLEGE, BANGALORE
b) 15
c) 5
d) 20
Answer: b) 15
a) function
b) define
c) def
d) func
16
Answer: c) def
36. What will be the output of the following code?
Python Code:
def greet():
return "Hello!"
print(greet())
b) "Hello!"
c) Hello!
d) None
Answer: c) Hello!
a) def function_name(param=value):
b) def function_name(param==value):
c) function_name(param=value):
d) function_name(param==value):
17
Answer: a) def function_name(param=value):
38. What is the output of the following code?
Python Code:
def increment(n):
n += 1
return n
PRIMUS PU COLLEGE, BANGALORE
print(increment(7))
a) 6
b) 7
c) 8
d) 9
Answer: c) 8
Python Code:
def double(x):
return x * 2
print(result)
a) 2
b) 4
c) 6
d) 8
Answer: d) 8
d) Using parentheses ()
Python Code:
return a - b
print(subtract(15, 5))
a) 10
b) 20
c) 15
d) 5
Answer: a) 10
43. What is the term used for a function defined inside another function?
a) Nested function
20
b) Inner function
c) Sub function
d) Lambda function
Python Code:
result = sum_numbers(3, 4)
print(result)
a) 3
b) 4
c) 7
d) 10
Answer: c) 7
return x
print(identity("Python"))
a) Python
b) identity
PRIMUS PU COLLEGE, BANGALORE
c) "Python"
d) print
Answer: a) Python
46. How do you ensure a function has a variable number of arguments in Python?
return s * n
print(repeat_string("Hi", 3))
a) Hi
b) HiHiHi
d) repeat_string
Answer: b) HiHiHi
a) def my_func():
b) def my-func():
c) def my_func1():
d) def _myfunc():
Python Code:
PRIMUS PU COLLEGE, BANGALORE
return a + b
print(add(3))
a) 3
b) 5
c) 8
d) 15
Answer: c) 8
24
FILL IN THE BLANKS (Sample)
2 MARKS QUESTIONS
2 Marks Questions
num = 5
def myfunc1():
global num
PRIMUS PU COLLEGE, BANGALORE
Output:
Accessing num = 5
num reassigned = 10
Accessing num outside myfunc1 10
def myFunc1():
y = num + 5
print("Accessing num -> (global) in myFunc1, value =",
26
num)
print("Accessing y-> (local variable of myFunc1)
accessible, value=", y)
myFunc1()
print("Accessing num outside myFunc1", num)
print("Accessing y outside myFunc1", y)
Output:
def function_name(parameters):
# function body
29
3 MARKS QUESTIONS
code does not need to be written at multiple places, which also makes
debugging easier. For example, a function to calculate the area of a
circle can be reused whenever needed.
o Reusability: Functions can be reused in different parts of the program
or in different programs. For example, a function that calculates the tax
on a product can be used in various billing systems.
o Parallel Development: Work can be divided among team members and
completed in parallel. Each member can work on different functions.
2. Write a short program to demonstrate the use of a global variable.
x = 10 # global variable
def increase():
global x
x += 5
print("Inside function, x =", x)
increase()
30 print("Outside function, x =", x)
Output:
Inside function, x = 15
Outside function, x = 15
def example():
y = 5 # local variable
y += 3
print("Inside function, y =", y)
example()
Output:
Inside function, y = 8
def get_min_max(numbers):
return min(numbers), max(numbers)
nums = [1, 2, 3, 4, 5]
minimum, maximum = get_min_max(nums)
print("Min:", minimum)
print("Max:", maximum)
Output:
Min: 1 31
Max: 5
5. Describe the process of creating a user-defined function with parameters.
Creating a user-defined function with parameters involves:
o Using the def keyword followed by the function name and parameters
in parentheses.
o Writing the function body indented under the function header.
o Optionally, returning a value using the return statement.
def greet(name):
print("Hello,", name)
PRIMUS PU COLLEGE, BANGALORE
greet("Alice")
Output:
Hello, Alice
6. Write a function that calculates the sum of the first n natural numbers.
def sum_natural_numbers(n):
return n * (n + 1) // 2
Output:
Output:
Square of 4 is 16
display_info("Alice", 25)
display_info(age=25, name="Alice")
9. Output:
Name: Alice
Age: 25
greet("Alice")
greet("Bob", "Good morning")
Output:
11. Write a short program to demonstrate the use of the global keyword.
PRIMUS PU COLLEGE, BANGALORE
count = 0
def increment():
global count
count += 1
increment()
print("Count after increment:", count)
Output:
12. Describe the flow of execution in a Python program with multiple function
calls. The flow of execution in a Python program with multiple function calls
involves:
o Starting at the first statement of the program.
o Executing function definitions but not their bodies.
def first():
print("First function")
def second():
print("Second function")
first()
second()
First function
Second function
13. Explain the concept of a module in Python and how it helps in program
development. A module in Python is a file containing Python code, which can
include functions, classes, and variables. Modules help in:
o Organizing code into manageable sections.
o Reusing code across different programs by importing the module.
o Reducing redundancy and improving maintainability.
# example_module.py
def greet(name):
print("Hello,", name)
# main_program.py
import example_module 35
example_module.greet("Alice")
Output:
Hello, Alice
14. Write a short program to demonstrate the use of the Python Standard
Library.
import math
def calculate_circle_area(radius):
return math.pi * radius ** 2
PRIMUS PU COLLEGE, BANGALORE
Output:
15. How can you reuse functions from another program or module? Explain
with an example. Functions from another program or module can be reused by
importing the module.
# math_functions.py
def add(a, b):
return a + b
# main_program.py
36 import math_functions
print("Addition:", math_functions.add(5, 3))
print("Subtraction:", math_functions.subtract(5, 3))
Output:
Addition: 8
Subtraction: 2
16. Explain the importance of the def keyword in Python. The def keyword is
used to define a new function. It tells the Python interpreter that the following
block of code is a function.
17. Describe how function headers and function calls work in Python.
o Function Header: Begins with def, followed by the function name,
parentheses, and a colon. It may include parameters.
def greet(name):
greet("Alice")
Output:
Enter a number: 4
Square of 4 is 16
PRIMUS PU COLLEGE, BANGALORE
19. Explain the significance of the colon (:) at the end of a function header in
Python. The colon (:) at the end of a function header indicates the beginning
of the function body. It tells the Python interpreter that the following indented
block of code belongs to the function.
def greet(name):
print("Hello,", name)
5 MARKS QUESTIONS
1. Write a Python program that demonstrates the use of both global and local
variables within multiple functions.
x = 50 # global variable
def func1():
global x
x += 10
y = 20 # local variable
38
print("func1 - global x:", x)
print("func1 - local y:", y)
def func2():
z = 30 # local variable
print("func2 - global x:", x)
print("func2 - local z:", z)
func1()
func2()
print("Global x after functions:", x)
Output:
result1 = add(5, 3)
result2 = subtract(10, 4)
def calculate_area(radius):
pi = 3.14159
PRIMUS PU COLLEGE, BANGALORE
return pi * radius ** 2
o Testing and Debugging: Smaller functional units are easier to test and
debug compared to a large block of code.
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
def feature1():
pass # Developer A
def feature2():
pass # Developer B
40
3. Write a Python function to calculate the factorial of a number.
def calcFact(num):
fact = 1
for i in range(num,0,-1):
fact = fact * i
print("Factorial of",num,"is",fact)
Output:
Factorial of 5 is 120
def greet(name):
print("Hello,", name)
o Calling a Function:
▪ Use the function name followed by parentheses.
▪ Pass arguments inside the parentheses if the function requires 41
parameters.
greet("Alice")
o Executing a Function:
▪ When a function is called, Python executes the code within the
function body.
▪ If the function returns a value, it can be captured in a variable or
used directly.
result = add(3, 4)
print("Sum:", result)
def my_function():
local_var = 10
print(local_var)
my_function()
# print(local_var) # This would raise an error
global_var = 20
def another_function():
print(global_var)
another_function()
print(global_var)
def main():
x = 10
y = 5
main()
Output:
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2.0
PRIMUS PU COLLEGE, BANGALORE
7. Discuss the role and benefits of the Python Standard Library in program
development. Provide examples of commonly used modules and functions.
import math
print(math.sqrt(16))
import random
print(random.randint(1, 10))
greet("Alice", 25)
greet(age=25, name="Alice")
greet("Alice")
greet("Bob", 25)
45
9. Write a detailed program that demonstrates the use of modules in Python.
Show how to import modules, use functions from them, and handle
namespace conflicts.
# math_operations.py
def add(a, b):
return a + b
# main_program.py
import math_operations as mo
from math import sqrt # Importing specific function
def main():
x = 10
y = 5
main()
Output:
Addition: 15
Subtraction: 5
Square root of 16: 4.0
46
10. Discuss the importance of modularity and reusability in programming. Use
Python functions and modules to illustrate your points.
o Modularity: Dividing a program into separate modules that perform
distinct tasks. Each module can be developed, tested, and debugged
independently, enhancing maintainability and scalability.
# module1.py
def function1():
pass
# module2.py
def function2():
# main.py
import module1
import module2
module1.function1()
module2.function2()
# math_functions.py
def add(a, b):
return a + b
# main_program.py
import math_functions as mf
result = mf.add(3, 4) 47
print("Sum:", result)
Modularity and reusability make programs easier to understand, reduce
development time, and minimize the risk of errors.
def greet(name):
print("Hello,", name)
o Calling Functions:
▪ Use the function name followed by parentheses.
▪ Pass arguments inside the parentheses if the function requires
parameters.
greet("Alice")
o Best Practices:
▪ Use meaningful function names that describe what the function
does.
▪ Keep functions short and focused on a single task.
▪ Document functions with comments and docstrings.
48
▪ Handle edge cases and errors within the function.
o Common Pitfalls:
▪ Forgetting to return a value from the function.
result = add(2, 3)
print(result) # Correct usage
def increment():
global count
count += 1
print(append_to_list(1)) # [1]
print(append_to_list(2)) # [1, 2] (unexpected)
# Correct way
def append_to_list(value, my_list=None):
if my_list is None:
my_list = []
my_list.append(value) 49
return my_list
print(append_to_list(1)) # [1]
print(append_to_list(2)) # [2]
Example:
def factorial(n):
"""Calculate the factorial of a number."""
if n == 0:
return 1
else:
return n * factorial(n-1)
PRIMUS PU COLLEGE, BANGALORE
Output:
Factorial of 5: 120
a)
50 Error: The function call create(5) is missing a second argument for freq.
b)
from math import sqrt, ceil
def calc():
print(cos(0))
calc() # function call
Error: The function cos from the math module is not imported.
c)
mynum = 9
d)
e)
51
def greet():
return "Good morning"
greet() = message # function call
which is 89.
3. Use of random() vs randint() to generate random numbers between 1 and
5
Example:
s, p = sum_and_product(2, 3)
print("Sum:", s)
print("Product:", p)
Example:
x = 10 # global variable
def func():
y = 5 # local variable
print("Local y:", y) 53
func()
print("Global x:", x)
No, a function does not always return a value. Functions that do not explicitly
return a value return None by default.
Example:
def greet():
print("Hello")
PRIMUS PU COLLEGE, BANGALORE
****************
54