wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

sADAHBN j12q3b4e1hjbasDI!J@#!(Python))

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

What is the output of ('a', 'b', 'c') + ('d',)?

a)

('a', 'b', 'c', 'd')

b)

('a', 'b', 'c', 'd',)

c)

('abcd')

d)

Error

2.

Which of these will create an empty set?

a)

{}

b)

set()

c)

[]

d)

()

3.

What file mode opens a file for writing and creates it if it doesn't exist?

a)

"r"

b)

"a"

c)

"w"

d)

"x"

4.

Which dictionary method returns None if a key doesn't exist instead of raising an error?

a)

dict[key]

b)

dict.get(key)

c)

dict.find(key)

d)

dict.search(key)

5.

What does the finally block do in exception handling?

a)

Runs only if there's an exception

b)

Runs only if there's no exception

c)

Runs regardless of whether there's an exception

d)

Prevents exceptions from occurring

6.

Which of these is NOT a valid set operation?

a)

Union (|)

b)

Intersection (&)

c)

Concatenation (+)

d)

Difference (-)

7.

What does .rstrip() do when reading a file?

a)

Removes whitespace from the right side including newlines

b)

Removes whitespace from the left side

c)

Removes all whitespace

d)

Reads the file in reverse

8.

What is the output of len({1, 2, 2, 3, 3, 3})?

a)

6

b)

3

c)

1

d)

Error

9.

Which exception is raised when dividing by zero?

a)

ValueError

b)

ZeroDivisionError

c)

MathError

d)

DivideError

10.

How do you access the value for key "age" in dictionary person?

a)

person[age]

b)

person("age")

c)

person["age"]

d)

person.get["age"]

11.

What does the file mode "a" do?

a)

Opens for reading

b)

Opens for writing (overwrites)

c)

Opens for appending

d)

Opens for both reading and writing

12.

Which of these tuple methods counts occurrences of an element?

a)

.count()

b)

.find()

c)

.occurrences()

d)

.total()

13.

What will set1 ^ set2 return?

a)

Union

b)

Intersection

c)

Difference

d)

Symmetric difference

14.

Which statement correctly opens a file for reading and automatically closes it?

a)

file = open("data.txt")

b)

with open("data.txt") as file:

c)

open("data.txt").read()

d)

Both b and c

15.

What is the main advantage of tuples over lists?

a)

They can be modified

b)

They are mutable

c)

They are more memory efficient

d)

They support more methods

16.

Which of these will cause a KeyError?

a)

Yes

b)

No

c)

Only if using .get()

d)

Never

17.

What does file.readlines() return?

a)

A string of the entire file

b)

A list of strings (lines)

c)

A single line

d)

A dictionary of lines

18.

Which data structure guarantees unique elements?

a)

List

b)

Tuple

c)

Set

d)

Dictionary

19.

What exception is raised when trying to convert "abc" to an integer?

a)

TypeError

b)

ValueError

c)

ConversionError

d)

IntError

20.

How do you iterate through both keys and values in a dictionary?

a)

for key in dict:

b)

for value in dict.values():

c)

for key, value in dict.items():

d)

Both a and c

21.

What is the purpose of the continue statement in file processing?

a)

To stop reading the file

b)

To skip to the next line

c)

To close the file

d)

To write to the file

22.

Which of these is immutable?

a)

List

b)

Set

c)

Dictionary

d)

Tuple

23.

What does "Hello" in {"Hello", "World"} return?

a)

True

b)

False

c)

1

d)

"Hello"

24.

How do you handle multiple exceptions in one except block?

a)

except ValueError, TypeError:

b)

except [ValueError, TypeError]:

c)

except (ValueError, TypeError):

d)

except ValueError and TypeError:

25.

What is the output of ("apple", "banana")[1]?

a)

"apple"

b)

"banana"

c)

1

d)

Error

26.

Which method removes and returns an item from a dictionary?

a)

.remove()

b)

.pop()

c)

.delete()

d)

.extract()

27.

What does open("file.txt", "r") return if the file doesn't exist?

a)

Creates the file

b)

Returns an empty file object

c)

Raises FileNotFoundError

d)

Returns None

28.

Which of these operations removes common elements from the first set?

a)

Union

b)

Intersection

c)

Difference

d)

Symmetric difference

29.

What is the correct way to add a new key-value pair to a dictionary?

a)

dict.add("key", "value")

b)

dict["key"] = "value"

c)

dict.insert("key", "value")

d)

dict.append("key", "value")

30.

Which block executes if no exception occurs in a try-except structure?

a)

except

b)

else

c)

finally

d)

then