Python Sets Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering set creation, uniqueness, adding/removing items, and mathematical operations like union and intersection.

35 Questions
~70 minutes
1

Question 1

Which of the following commands correctly creates an empty set in Python?

A
set()
B
{}
C
[]
D
empty_set()
2

Question 2

Analyze the code below. What will be the final content of the variable `unique_numbers`?

python
numbers = [1, 2, 2, 3, 3, 3]
unique_numbers = set(numbers)
A
{1, 2, 3}
B
{1, 2, 2, 3, 3, 3}
C
[1, 2, 3]
D
(1, 2, 3)
3

Question 3

What is the primary difference between the `remove()` and `discard()` methods when deleting an item from a set?

A
remove() raises a KeyError if the item is missing, while discard() does nothing.
B
discard() raises an error if the item is missing, while remove() does not.
C
remove() deletes the item permanently, while discard() moves it to a trash bin.
D
There is no difference; they are aliases for the same method.
4

Question 4

Consider the following two sets. Which expression correctly calculates their intersection (elements present in both)?

python
set_a = {1, 2, 3}
set_b = {3, 4, 5}
A
set_a & set_b
B
set_a | set_b
C
set_a - set_b
D
set_a + set_b
5

Question 5

What happens when you try to add a mutable object, such as a list, to a set?

python
s = {1, 2}
s.add([3, 4])
A
A TypeError is raised because lists are unhashable.
B
The list is added as a single element.
C
The elements of the list are added individually to the set.
D
The set is converted to a list.
6

Question 6

Which method would you use to add a single element to an existing set?

A
add()
B
append()
C
insert()
D
push()
7

Question 7

Analyze the code below. What is the output of the print statement?

python
s = {1, 2, 3}
print(4 in s)
A
False
B
True
C
None
D
Error
8

Question 8

What is the result of the union operation between `s1 = {1, 2}` and `s2 = {2, 3}`?

A
{1, 2, 3}
B
{2}
C
{1, 2, 2, 3}
D
{1, 3}
9

Question 9

Why are sets considered 'unordered' collections?

A
They do not record element position or insertion order.
B
They sort elements randomly every time you access them.
C
They can only store numbers.
D
They cannot be iterated over.
10

Question 10

Which of the following code snippets correctly creates a set with initial values?

A
my_set = {1, 2, 3}
B
my_set = [1, 2, 3]
C
my_set = (1, 2, 3)
D
my_set = <1, 2, 3>
11

Question 11

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

A
3
B
6
C
1
D
Error
12

Question 12

Which operator is used to calculate the symmetric difference (elements in either set, but not both)?

A
^
B
&
C
|
D
%
13

Question 13

Analyze the code below. What happens when `pop()` is called on the set?

python
fruits = {"apple", "banana", "cherry"}
item = fruits.pop()
A
An arbitrary element is removed and returned.
B
The last element added is removed.
C
The first element alphabetically is removed.
D
A TypeError is raised because sets don't support pop().
14

Question 14

What is the result of subtracting set B from set A: `A - B`, where `A = {1, 2, 3}` and `B = {3, 4}`?

A
{1, 2}
B
{4}
C
{1, 2, 4}
D
{3}
15

Question 15

How can you remove all elements from a set at once?

A
clear()
B
empty()
C
delete_all()
D
flush()
16

Question 16

Which of the following is true about the time complexity of checking membership (`x in s`) in a set?

A
It is O(1) on average.
B
It is O(n), depending on the set size.
C
It is O(log n).
D
It is slower than checking a list.
17

Question 17

What is the output of the following code involving the `update()` method?

python
s = {1, 2}
s.update([2, 3, 4])
A
{1, 2, 3, 4}
B
{1, 2, [2, 3, 4]}
C
{1, 2, 2, 3, 4}
D
Error
18

Question 18

Can a set contain another set as an element?

python
s = {1, {2, 3}}
A
No, because sets are mutable and thus unhashable.
B
Yes, sets can be nested arbitrarily.
C
Yes, but only if the inner set is empty.
D
No, unless you use a special library.
19

Question 19

What does the `isdisjoint()` method check?

A
Whether two sets have no elements in common.
B
Whether two sets are not equal.
C
Whether one set is a subset of another.
D
Whether the set contains broken links.
20

Question 20

Which Python function creates an immutable version of a set?

A
frozenset()
B
immutableset()
C
lockset()
D
tupleset()
21

Question 21

Analyze the code below. What is the value of `result`?

python
a = {1, 2, 3}
b = {1, 2}
result = b.issubset(a)
A
True
B
False
C
None
D
Error
22

Question 22

What happens if you iterate over a set using a `for` loop?

A
You get the elements in an arbitrary order.
B
You get the elements in sorted order.
C
You get the elements in the order they were inserted.
D
You cannot iterate over a set.
23

Question 23

Which of the following is NOT a valid way to remove an item from a set?

A
delete()
B
remove()
C
discard()
D
pop()
24

Question 24

What is the result of `set('hello')`?

A
{'h', 'e', 'l', 'o'}
B
{'h', 'e', 'l', 'l', 'o'}
C
{'hello'}
D
['h', 'e', 'l', 'l', 'o']
25

Question 25

How do you check if set A is a proper subset of set B (A is in B, but A != B)?

A
A < B
B
A <= B
C
A.ispropersubset(B)
D
A << B
26

Question 26

What is the output of the following set comprehension?

python
squares = {x*x for x in range(4)}
A
{0, 1, 4, 9}
B
[0, 1, 4, 9]
C
(0, 1, 4, 9)
D
{0, 1, 2, 3}
27

Question 27

Can you use the index operator `[]` to access elements in a set?

python
s = {1, 2, 3}
print(s[0])
A
No, it raises a TypeError.
B
Yes, it returns the first element.
C
Yes, if the set is sorted.
D
No, it returns None.
28

Question 28

What happens if you try to create a set with duplicate values explicitly?

python
s = {1, 1, 1}
A
The duplicates are automatically removed during creation.
B
A SyntaxError is raised.
C
A ValueError is raised.
D
The set keeps all duplicates.
29

Question 29

Which of the following data types is hashable and can be added to a set?

A
Tuple
B
List
C
Dictionary
D
Set
30

Question 30

What is the result of `s = {1, 2}; s.add(2)`?

A
{1, 2}
B
{1, 2, 2}
C
Error
D
{1}
31

Question 31

Which method returns a copy of the set?

A
copy()
B
clone()
C
duplicate()
D
replicate()
32

Question 32

What is the output of the following code?

python
a = {1, 2}
b = {2, 1}
print(a == b)
A
True
B
False
C
Error
D
None
33

Question 33

Which operation creates a set containing elements that are in A or B, but not in both?

A
Symmetric Difference
B
Intersection
C
Union
D
Subset
34

Question 34

What is the correct syntax to check if `x` is NOT in set `s`?

A
x not in s
B
x !in s
C
not(x in s)
D
Both A and C
35

Question 35

Analyze the code below. What is the final size of the set?

python
s = set()
s.add(1)
s.add(1.0)
s.add(True)
A
1
B
3
C
2
D
Error

QUIZZES IN Python