WorksheetsPython Assessment-2
Total questions: 20
Worksheet time: 20mins
Which of the following is a valid way to call a function in Python?
call add()
add()
function add()
def add()
Which function is used to apply a function to all the elements in an iterable?
filter()
map()
reduce()
apply()
What is the output of the following code snippet?
def square(x):
return x * x
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)
print(list(squared_numbers))
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
[1, 2, 3, 4, 5]
[1, 8, 27, 64, 125]
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, numbers))
print(result)
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[2, 3, 4, 5, 6]
[1, 4, 9, 16, 25]
Which of the following is a valid set in Python?
{1, 2, 3, 3, 4}
{1, 2, [3, 4]}
{1, 2, (3, 4)}
{1, 2, {3, 4}}
What is the output of the following code?
words = ["apple", "banana", "orange", "kiwi"]
result = list(filter(lambda x: len(x) > 5, words))
print(result)
["apple" ,"banana"]
["orange", "kiwi"]
["apple" ,"orange"]
["banana","orange"]
What is the output of the following code?
numbers = [ ]
result = map(lambda x: x, numbers)
print(list(result))
[ ]
None
[0]
[None]
What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)
[1, 3, 5]
[2, 4]
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 1]
What is the output of the following code?
my_set = {1, 2, 3, 4, 5}
print(len(my_set))
1
5
4
0
Match the Following
Set () 1)Applies function to each iterable item.
Recursion () 2)An function Selects items based on a condition.
filter () 3)An function that calls itself directly or indirectly.
map () 4)An unordered collection of unique elements.
1,3,4,2
2,4,1,3
4,3,2,1
1,2,3,4
What is an exception in Python?
An error that occurs during program execution
A built-in function for error handling
A warning message
A type of loop
What is the output of the following code?
try:
x = 1 / 0
except ZeroDivisionError:
print("Division by zero!")
Division by zero!
ZeroDivisionError
1
0
Which keyword is used to handle exceptions in Python?
try
catch
except
raise
What is the output of the following code?
try:
x = int("abc")
except ValueError:
print("Invalid conversion")
finally:
print("Done")
A) B)
C)
D)
"Invalid conversion"
Done
Invalid conversion
Error message
Which of the following is NOT a standard exception class in Python?
ValueError
TypeError
SyntaxError
RangeError
What is the purpose of the try block in Python?
To define custom exception classes
To handle exceptions that may occur during code execution
To execute code only if no exceptions occur
To terminate the program
What is the correct way to import the random module in Python?
import random
import Random
include random
from random import *
What will be the output of the following code?
try:
raise ValueError("Custom error")
except ValueError as e:
print(e)
Custom error
ValueError
Nothing (no output)
Syntax error
What does the following code snippet do?
import random
x = random.randint(1, 10)
print(x)
Error
Prints a random integer between 1 and 10
Prints the number 10
Prints the number 1
What does random.choice([1, 2, 3, 4]) return?
A random integer
A random floating-point number
A random element from the list
Error
