wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Sets and Dictionaries Worksheet

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

Which of the following is a characteristic of sets in Python?

a)

Ordered collection

b)

Allows duplicates

c)

Mutable and supports mathematical set operations

d)

Supports indexing and slicing

2.

Why do sets in Python not support indexing?

a)

They are mutable

b)

They maintain order

c)

They are unordered and use hash-based storage

d)

They allow duplicates

3.

How can you create a set from the string 'spam'?

a)

set = 'spam'

b)

set('spam')

c)

{'spam'}

d)

list('spam')

4.

What happens when you try to add a duplicate element to a set?

a)

It raises an error

b)

The set remains unchanged

c)

The duplicate replaces the existing one

d)

The set becomes a list

5.

Which elements can be added to a set?

a)

Lists

b)

Dictionaries

c)

Tuples

d)

Sets

6.

What does the union operation on sets do?

a)

Finds common elements

b)

Combines all unique elements

c)

Finds elements in one but not the other

d)

Checks if one is subset of another

7.

What is the difference between remove() and discard() in sets?

a)

remove() does not raise error if element not present, discard() does

b)

discard() does not raise error if element not present, remove() does

c)

Both raise errors

d)

No difference

8.

What does pop() method do in a set?

a)

Removes the first element

b)

Removes and returns an arbitrary element

c)

Removes the last element

d)

Clears the set

9.

How do you check if two sets have no elements in common?

a)

issubset()

b)

issuperset()

c)

isdisjoint()

d)

intersection()

10.

What is the output of {x ** 2 for x in [1, 2, 3, 4]}?

a)

{1, 4, 9, 16}

b)

[1, 4, 9, 16]

c)

(1, 4, 9, 16)

d)

Error

11.

Which of the following is true about dictionary keys in Python?

a)

Can be mutable like lists

b)

Must be unique and immutable

c)

Can be duplicates

d)

Maintain no order

12.

How do you access a value in a dictionary?

a)

dict.value(key)

b)

dict[key]

c)

dict.get(value)

d)

dict.items(key)

13.

What does dict.items() return?

a)

List of keys

b)

List of values

c)

List of key-value tuples

d)

The dictionary itself

14.

How can you 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})

15.

What happens if you assign a value to an existing key in a dictionary?

a)

Raises error

b)

Adds a duplicate key

c)

Overwrites the previous value

d)

Ignores the assignment

16.

Which method removes a specific key and returns its value?

a)

del dict[key]

b)

dict.pop(key)

c)

dict.remove(key)

d)

dict.discard(key)

17.

What does dict.fromkeys(keys, value) do?

a)

Creates a dictionary with keys and default value

b)

Copies a dictionary

c)

Updates values

d)

Removes keys

18.

How do you merge two dictionaries using update()?

a)

dict1 | dict2

b)

dict1.update(dict2)

c)

dict1 + dict2

d)

dict1.merge(dict2)

19.

What is a frozenset in Python?

a)

Mutable set

b)

Immutable set that is hashable

c)

Ordered set

d)

Set that allows duplicates

20.

In the problem of finding failures in subjects, which set operation is used to find total unique failures?

a)

Intersection

b)

Difference

c)

Union

d)

Symmetric difference