Computer science
Assignment
📚 MCQs: Working with Functions and Python Libraries
Q1.
Which of the following is the correct way to define a function in Python?
A) function myFunction():
B) def myFunction:
C) def myFunction():
D) function: myFunction()
✅ Answer: C) def myFunction():
Q2.
What is the output of this code?
def add(x, y=5):
return x + y
print(add(2))
A) 2
B) 5
C) 7
D) Error
✅ Answer: C) 7
Q3.
Which statement is used to exit a function and return a value?
A) break
B) stop
C) return
D) exit
✅ Answer: C) return
Q4.
What will be the output?
def greet(name):
print("Hello,", name)
result = greet("Alice")
print(result)
A) Hello, Alice then Hello, Alice
B) Hello, Alice then None
C) Hello, Alice then greet
D) None then Hello, Alice
✅ Answer: B) Hello, Alice then None
Q5.
Which of the following is NOT a valid Python library?
A) numpy
B) math
C) random
D) numbers
✅ Answer: D) numbers (Note: There is a numbers module, but it’s rarely used as a general-purpose
library.)
Q6.
Which library is commonly used for numerical computations?
A) pandas
B) os
C) numpy
D) sys
✅ Answer: C) numpy
Q7.
What will import math do?
A) Import a file named [Link] from your directory.
B) Import Python’s built-in math module.
C) Define a new math function.
D) Throw an error.
✅ Answer: B) Import Python’s built-in math module.
Q8.
Which function finds the square root in the math module?
A) sqrt()
B) square_root()
C) root()
D) mathroot()
✅ Answer: A) sqrt()
Q9.
How do you import only the sqrt function from math?
A) import sqrt from math
B) from math import sqrt
C) include sqrt from math
D) use sqrt from math
✅ Answer: B) from math import sqrt
Q10.
Which of the following statements is true about functions in Python?
A) A function must return a value.
B) Functions cannot call other functions.
C) Functions cannot have default arguments.
D) Functions can return multiple values.
✅ Answer: D) Functions can return multiple values.
📚 More MCQs: Working with Functions and Python Libraries
Q11.
What is the output of this code?
def func(a, b):
return a * b
print(func(2, 3))
print(func('2', 3))
A) 5 and 5
B) 6 and 222
C) 6 and 222
D) 6 and 2222
✅ Answer: C) 6 and 222
Q12.
Which of these statements about variable scope is TRUE?
A) Variables defined inside a function are global by default.
B) Variables defined inside a function are local by default.
C) Global variables can’t be accessed inside functions.
D) The global keyword is used to make a local variable global inside a function.
✅ Answer: B) Variables defined inside a function are local by default.
Q13.
What will be the output?
def test():
pass
print(test())
A) 0
B) None
C) pass
D) Error
✅ Answer: B) None
Q14.
Which library would you use for working with random numbers?
A) sys
B) os
C) random
D) math
✅ Answer: C) random
Q15.
Which statement is used to import a library with an alias?
A) import math as m
B) import as math m
C) from math import as m
D) include math as m
✅ Answer: A) import math as m
Q16.
What is the output of this code?
def calc(a, b):
return a + b, a - b
x, y = calc(5, 2)
print(x, y)
A) 7 3
B) 3 7
C) (7, 3)
D) Error
✅ Answer: A) 7 3
Q17.
What will this code print?
def outer():
def inner():
return "Hello"
return inner()
print(outer())
A) Hello
B) inner
C) outer
D) None
✅ Answer: A) Hello
Q18.
Which library is used for working with dates and times?
A) datetime
B) date
C) timeit
D) calendar
✅ Answer: A) datetime
Q19.
Which of these is the correct syntax to import all functions from a module?
A) import math.*
B) import * from math
C) from math import *
D) include all math
✅ Answer: C) from math import *
Q20.
What will be the output?
import math
print([Link](3.7))
A) 3.7
B) 4
C) 3
D) Error
✅ Answer: C) 3
📚 More MCQs: Working with Functions and Python Libraries
Q21.
Which of the following is a built-in function in Python?
A) sqrt()
B) mean()
C) sum()
D) average()
✅ Answer: C) sum()
Q22.
How do you find the length of a list my_list?
A) length(my_list)
B) count(my_list)
C) size(my_list)
D) len(my_list)
✅ Answer: D) len(my_list)
Q23.
Which function in the random module returns a random float between 0 and 1?
A) rand()
B) random()
C) randint()
D) randfloat()
✅ Answer: B) random()
Q24.
Which statement is TRUE about Python modules?
A) A module is always a single function.
B) A module can contain functions, variables, and classes.
C) You cannot import your own Python files as modules.
D) Modules must be installed via pip only.
✅ Answer: B) A module can contain functions, variables, and classes.
Q25.
What does the dir() function do?
A) Deletes a module.
B) Lists the attributes and methods of an object.
C) Changes the working directory.
D) Imports a directory as module.
✅ Answer: B) Lists the attributes and methods of an object.
Q26.
What is the output?
def f(x):
if x > 0:
return True
else:
return
print(f(1))
print(f(-1))
A) True and False
B) True and None
C) True and 0
D) True and Error
✅ Answer: B) True and None
Q27.
Which of the following is the correct syntax for a lambda function?
A) lambda: x, y : x + y
B) lambda x, y: x + y
C) def lambda(x, y): return x + y
D) function lambda(x, y): return x + y
✅ Answer: B) lambda x, y: x + y
Q28.
Which library is commonly used for data analysis and dataframes?
A) numpy
B) math
C) random
D) pandas
✅ Answer: D) pandas
Q29.
How do you install a new library using pip?
A) install pip library_name
B) pip install library_name
C) pip library_name install
D) import pip library_name
✅ Answer: B) pip install library_name
Q30.
What is the output?
def example(a, b, c=2):
return a + b + c
print(example(1, 1))
A) 2
B) 3
C) 4
D) Error
✅ Answer: C) 4
✅ All done!