wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Intermediate Python Worksheet

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

What is the purpose of the try-except block?

a)

Function definition

b)

Loop control

c)

Variable declaration

d)

Exception handling

2.

Which exception is raised when dividing by zero?

a)

ArithmeticError

b)

ValueError

c)

ZeroDivisionError

d)

TypeError

3.

What does the finally clause do?

a)

Always executes

b)

Executes only if exception occurs

c)

Never executes

d)

Executes only if no exception

4.

When does the else clause in try-except execute?

a)

Always

b)

Never

c)

When exception occurs

d)

When no exception occurs

5.

What keyword is used to raise an exception manually?

a)

exception

b)

raise

c)

throw

d)

error

6.

What does if name == "main": check?

a)

Class name

b)

Function name

c)

If module is run directly

d)

Variable name

7.

What is the purpose of the global keyword?

a)

Create global function

b)

Modify global variable

c)

Import global module

d)

Define global class

8.

What does the enumerate() function return?

a)

List of indices

b)

Dictionary

c)

List of values

d)

Index-value pairs

9.

What is list comprehension?

a)

List method

b)

List copying

c)

List sorting

d)

Elegant way to create lists

10.

What will [x*2 for x in range(5)] return?

a)

[1, 2, 3, 4, 5]

b)

[0, 1, 2, 3, 4]

c)

[2, 4, 6, 8, 10]

d)

[0, 2, 4, 6, 8]

11.

What is a virtual environment in Python?

a)

Virtual machine

b)

Cloud environment

c)

Testing environment

d)

Isolated Python environment

12.

Which command creates a virtual environment?

a)

python create env

b)

env create

c)

virtualenv myenv

d)

create virtualenv

13.

What does pip freeze do?

a)

Freezes Python version

b)

Clears cache

c)

Stops pip

d)

Lists installed packages

14.

What is a lambda function?

a)

Named function

b)

Built-in function

c)

Anonymous function

d)

Class method

15.

What will (lambda x: x**2)(5) return?

a)

10

b)

5

c)

Error

d)

25

16.

How many arguments can a lambda function have?

a)

None

b)

Any number

c)

Only two

d)

Only one

17.

What does the join() method do?

a)

Joins dictionaries

b)

Joins list elements into string

c)

Joins two strings

d)

Joins two lists

18.

What will "-".join(["a", "b", "c"]) return?

a)

"a-b-c"

b)

["a-b-c"]

c)

"abc"

d)

Error

19.

What does the format() method do?

a)

Formats strings with values

b)

Formats files

c)

Formats numbers

d)

Formats dates

20.

What will "{0} and {1}".format("A", "B") return?

a)

"A and B"

b)

"{A} and {B}"

c)

"0 and 1"

d)

Error

21.

What does the map() function do?

a)

Navigates data

b)

Creates a map

c)

Maps keys to values

d)

Applies function to all items

22.

What does the filter() function return?

a)

Sorted list

b)

Filtered dictionary

c)

All elements

d)

Elements where function returns True

23.

Where is the reduce() function imported from?

a)

collections

b)

Built-in

c)

functools

d)

itertools

24.

What will list(map(lambda x: x*2, [1, 2, 3])) return?

a)

[1, 2, 3]

b)

[1, 4, 9]

c)

[2, 4, 6]

d)

Error

25.

What does list slicing [::2] mean?

a)

Last two elements

b)

First two elements

c)

Every second element

d)

Every element

26.

What will "Python"[::-1] return?

a)

"Python"

b)

Error

c)

"Pytho"

d)

"nohtyP"

27.

Which of the following is True about tuples?

a)

Can be changed

b)

Mutable

c)

Elements can be added

d)

Immutable

28.

What will {1, 2, 3}.union({3, 4, 5}) return?

a)

{1, 2, 3}

b)

{1, 2, 3, 4, 5}

c)

Error

d)

{3}

29.

What will {1, 2, 3}.intersection({2, 3, 4}) return?

a)

{1, 4}

b)

Error

c)

{2, 3}

d)

{1, 2, 3, 4}

30.

What is the DRY principle in programming?

a)

Do Review Yearly

b)

Define Run Yield

c)

Don't Repeat Yourself

d)

Data Retrieval Yearly