Functions QA
Functions QA
Computer Science
Short Ques5ons & Answers
Topic: Func+ons in Python
1. Q: What are the three types of func4ons in Python? A: Built-in func4ons, func4ons
defined in modules, and user-defined func4ons.
2. Q: How do you create a user-defined func4on in Python? A: By using the def keyword
followed by the func4on name and a pair of parentheses containing the parameters.
3. Q: What are the two types of parameters in Python func4ons? A: Posi4onal
parameters and default parameters.
4. Q: How do you define a default parameter in a Python func4on? A: By assigning a
default value to a parameter using the equal sign (e.g., def my_func+on(x, y=5):).
5. Q: Can a Python func4on return mul4ple values? A: Yes, by returning them as a tuple
or another collec4on type (e.g., list or dic4onary).
6. Q: What is the flow of execu4on in a Python program? A: The flow of execu4on refers
to the order in which statements are executed during the program's run.
7. Q: What is the difference between global scope and local scope in Python? A: Global
scope refers to variables that are accessible throughout the en4re program, while
local scope refers to variables that are only accessible within a specific func4on or
block of code.
8. Q: How do you create a global variable in Python? A: By declaring a variable outside
of any func4on or block of code.
9. Q: How do you access a global variable from within a func4on? A: By using the global
keyword followed by the variable name.
10. Q: Can a local variable with the same name as a global variable be used within a
func4on? A: Yes, but it will shadow the global variable, making it inaccessible within
the func4on.
11. Q: What is a built-in func4on in Python? A: A func4on that is provided by the Python
interpreter and is always available without needing to import any modules.
12. Q: Give an example of a built-in func4on in Python. A: len(), which returns the
number of elements in a list or other iterable.
13. Q: What is a module in Python? A: A module is a file containing Python code, usually
consis4ng of func4ons, classes, and variables, that can be imported into other
Python programs.
14. Q: How do you import a func4on from a module in Python? A: By using the from
keyword followed by the module name, the import keyword, and the func4on name.
15. Q: What is a posi4onal parameter in a Python func4on? A: A posi4onal parameter is
a parameter that must be passed in a specific order when calling the func4on.
16. Q: What is a default parameter? A: A default parameter is a parameter that has a
default value specified when defining the func4on, allowing the func4on to be called
without providing a value for that parameter.
17. Q: What happens if you provide a value for a default parameter when calling a
func4on? A: The provided value will override the default value specified in the
func4on defini4on.
18. Q: What is the purpose of the return statement in a Python func4on? A: The return
statement is used to exit a func4on and return a value to the caller.
19. Q: Can you use a func4on without a return statement? A: Yes, but the func4on will
return None by default.
20. Q: What is the difference between arguments and parameters in Python? A:
Parameters are the variables defined in the func4on defini4on, while arguments are
the values passed to the func4on when it is called.
Programs:
1. Program to demonstrate a built-in func4on:
# Calculate the length of a list using the built-in len() func4on
my_list = [1, 2, 3, 4, 5]
length = len(my_list)
print("Length of the list:", length)
number = 16
sqrt_number = math.sqrt(number)
print("Square root of", number, "is", sqrt_number)
x=5
y=3
result = add_numbers(x, y)
print("Sum of", x, "and", y, "is", result)
number = 4
square = power(number)
print("Square of", number, "is", square)
width = 5
height = 3
area = area_of_rectangle(width, height)
print("Area of the rectangle is", area)
6. Program to demonstrate the use of global and local variables:
global_var = 10
def example_func4on():
local_var = 5
print("Global variable inside func4on:", global_var)
print("Local variable inside func4on:", local_var)
example_func4on()
print("Global variable outside func4on:", global_var)
def increment_count():
global count
count += 1
increment_count()
print("Count aeer increment:", count)
8. Program to demonstrate a func4on returning mul4ple values:
def min_max(numbers):
return min(numbers), max(numbers)
my_list = [1, 5, 3, 8, 2]
min_val, max_val = min_max(my_list)
print("Minimum and maximum values are:", min_val, max_val)
9. Program to demonstrate the use of a func4on as a parameter:
def square(x):
return x * x
def cube(x):
return x * x * x
number = 4
result_square = apply_func4on(square, number)
result_cube = apply_func4on(cube, number)
print("Square of", number, "is", result_square)
print("Cube of", number, "is", result_cube)
result = sum_numbers(1, 2, 3, 4, 5)
print("Sum of the numbers is:", result)
Prac+ce Ques+ons
1. Write a program that calculates the factorial of a given number using a user-defined
func4on.
2. Write a program that determines whether a given number is a prime number using a
user-defined func4on.
3. Write a program that finds the greatest common divisor (GCD) of two numbers using
a user-defined func4on.
4. Write a program that calculates the sum of a series of numbers (e.g., 1+2+3+...+n)
using a user-defined func4on.
5. Create a user-defined func4on that returns the length of a given string without using
the built-in len() func4on.
6. Write a program that accepts a list of numbers and returns a new list containing only
the even numbers using a user-defined func4on.
7. Create a user-defined func4on that accepts two strings and returns a concatenated
string without using the + operator.
8. Write a program that calculates the sum of the diagonal elements of a square matrix
using a user-defined func4on.
9. Write a program that accepts a list of numbers and returns the average of those
numbers using a user-defined func4on.
10. Create a user-defined func4on that accepts a list of strings and returns the longest
string in the list.
11. Write a program that sorts a list of numbers in ascending order using a user-defined
func4on.
12. Write a program that reverses a given string without using the built-in reverse()
func4on or string slicing.
13. Create a user-defined func4on that checks if a given word is a palindrome (reads the
same forward and backward).
14. Write a program that calculates the Fibonacci sequence up to a given number using a
user-defined func4on.
15. Create a user-defined func4on that accepts a list of numbers and returns the
maximum and minimum values in the list.
16. Write a program that converts a given temperature from Celsius to Fahrenheit and
vice versa using user-defined func4ons.
17. Write a program that finds the common elements between two given lists using a
user-defined func4on.
18. Create a user-defined func4on that calculates the area and circumference of a circle,
given its radius.
19. Write a program that counts the occurrences of a specific character in a given string
using a user-defined func4on.
20. Write a program that accepts a list of numbers and returns a list of unique elements
from the original list using a user-defined func4on.