Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Assessment-2

Total questions: 20

Worksheet time: 20mins

Name
Class
Date
1.

Which of the following is a valid way to call a function in Python?

a)

call add()

b)

add()

c)

function add()

d)

def add()

2.

Which function is used to apply a function to all the elements in an iterable?

a)

filter()

b)

map()

c)

reduce()

d)

apply()

3.

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))

a)

[1, 4, 9, 16, 25]

b)

[2, 4, 6, 8, 10]

c)

[1, 2, 3, 4, 5]

d)

[1, 8, 27, 64, 125]

4.

What is the output of the following code?

numbers = [1, 2, 3, 4, 5]

result = list(map(lambda x: x * 2, numbers))

print(result)

a)

[1, 2, 3, 4, 5]

b)

[2, 4, 6, 8, 10]

c)

[2, 3, 4, 5, 6]

d)

[1, 4, 9, 16, 25]

5.

Which of the following is a valid set in Python?

a)

{1, 2, 3, 3, 4}

b)

{1, 2, [3, 4]}

c)

{1, 2, (3, 4)}

d)

{1, 2, {3, 4}}

6.

What is the output of the following code?

words = ["apple", "banana", "orange", "kiwi"]

result = list(filter(lambda x: len(x) > 5, words))

print(result)

a)

["apple" ,"banana"]

b)

["orange", "kiwi"]

c)

["apple" ,"orange"]

d)

["banana","orange"]

7.

What is the output of the following code?

numbers = [ ]

result = map(lambda x: x, numbers)

print(list(result))

a)

[ ]

b)

None

c)

[0]

d)

[None]

8.

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)

a)

[1, 3, 5]

b)

[2, 4]

c)

[1, 2, 3, 4, 5]

d)

[2, 4, 6, 8, 1]

9.

What is the output of the following code?

my_set = {1, 2, 3, 4, 5}

print(len(my_set))

a)

1

b)

5

c)

4

d)

0

10.

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.

a)

1,3,4,2

b)

2,4,1,3

c)

4,3,2,1

d)

1,2,3,4

11.

What is an exception in Python?

a)

An error that occurs during program execution

b)

A built-in function for error handling

c)

A warning message

d)

A type of loop

12.

What is the output of the following code?

try:

x = 1 / 0

except ZeroDivisionError:

print("Division by zero!")

a)

Division by zero!

b)

ZeroDivisionError

c)

1

d)

0

13.

Which keyword is used to handle exceptions in Python?

a)

try

b)

catch

c)

except

d)

raise

14.

What is the output of the following code?

try:

x = int("abc")

except ValueError:

print("Invalid conversion")

finally:

print("Done")

A) B)

C)

D)

a)

"Invalid conversion"

b)

Done

c)

Invalid conversion

d)

Error message

15.

Which of the following is NOT a standard exception class in Python?

a)

ValueError

b)

TypeError

c)

SyntaxError

d)

RangeError

16.

What is the purpose of the try block in Python?

a)

To define custom exception classes

b)

To handle exceptions that may occur during code execution

c)

To execute code only if no exceptions occur

d)

To terminate the program

17.

What is the correct way to import the random module in Python?

a)

import random

b)

import Random

c)

include random

d)

from random import *

18.

What will be the output of the following code?

try:

raise ValueError("Custom error")

except ValueError as e:

print(e)

a)

Custom error

b)

ValueError

c)

Nothing (no output)

d)

Syntax error

19.

What does the following code snippet do?

import random

x = random.randint(1, 10)

print(x)

a)

Error

b)

Prints a random integer between 1 and 10

c)

Prints the number 10

d)

Prints the number 1

20.

What does random.choice([1, 2, 3, 4]) return?

a)

A random integer

b)

A random floating-point number

c)

A random element from the list

d)

Error