Thursday, September 10, 2026

Python Set Quiz

 Python Set Quiz


1. What is the output? s = {1, 2, 2, 3, 3} print(len(s))

   A. 2

   B. 3

   C. 5

   D. 4


2. What is the output? s = {10, 20, 30} print(20 in s)

   A. 20

   B. False

   C. None

   D. True


3. What is the output? s = {1, 2, 3} s.add(4) print(len(s))

   A. 4

   B. 5

   C. 3

   D. 1


4. What is the output? A = {1, 2, 3} B = {3, 4, 5} print(A & B)

   A. {3}

   B. {4, 5}

   C. {1, 2}

   D. {1, 2, 3, 4, 5}


5. What is the output? A = {1, 2} B = {2, 3} print(A | B)

   A. {3}

   B. {2}

   C. {1, 2, 3}

   D. {1}


6. What is the output? A = {1, 2, 3, 4} B = {2, 4} print(A - B)

   A. {2, 4}

   B. {1, 3}

   C. {2}

   D. {1, 2, 3, 4}


7. What is the output? s = set([1, 1, 2, 2, 3]) print(s)

   A. {1, 2, 3}

   B. [1, 2, 3]

   C. {1, 2, 2, 3}

   D. {1, 1, 2, 2, 3}


8. What is the output? s = {5, 10, 15} s.remove(10) print(10 in s)

   A. None

   B. True

   C. False

   D. 10


9. What is the output? s = {1, 2, 3} s.add(2) print(len(s))

   A. 4

   B. 3

   C. 1

   D. 2


10. What is the output? s = {2, 4, 6} print(5 not in s)

   A. False

   B. None

   C. True

   D. 5

2 comments:

perlknowledge said...

What is the output?
s = set([1, 1, 2, 2, 3]) print(s)
Converting the sequence to a set removes duplicate values, leaving the three distinct elements.
What is the output?
s = {5, 10, 15} s.remove(10) print(10 in s)
remove(10) removes 10 from the set, so the subsequent membership test is False.
What is the output?
s = {1, 2, 3} s.add(2) print(len(s))
2 is already in the set, so adding it again does not create a new element.
What is the output?
s = {2, 4, 6} print(5 not in s)
5 is not an element of the set, so the not-in membership test evaluates to True.

perlknowledge said...

What is the output?

s = {1, 2, 2, 3, 3} print(len(s))

The set keeps only unique elements, so the distinct values are 1, 2, and 3.

What is the output?

s = {10, 20, 30} print(20 in s)

20 is one of the elements in the set, so the membership test evaluates to True.

What is the output?

s = {1, 2, 3} s.add(4) print(len(s))

The set starts with three elements and adding the new value 4 gives four distinct elements.

What is the output?

A = {1, 2, 3} B = {3, 4, 5} print(A & B)

The intersection contains elements that occur in both sets, and 3 is the only common element.

What is the output?

A = {1, 2} B = {2, 3} print(A | B)

The union contains every distinct element from both sets.

What is the output?

A = {1, 2, 3, 4} B = {2, 4} print(A - B)

Set difference keeps elements in A that are not present in B, leaving 1 and 3.

What is the output?

s = set(\[1, 1, 2, 2, 3\]) print(s)

Converting the sequence to a set removes duplicate values, leaving the three distinct elements.

What is the output?

s = {5, 10, 15} s.remove(10) print(10 in s)

remove(10) removes 10 from the set, so the subsequent membership test is False.

What is the output?

s = {1, 2, 3} s.add(2) print(len(s))

2 is already in the set, so adding it again does not create a new element.

What is the output?

s = {2, 4, 6} print(5 not in s)

5 is not an element of the set, so the not-in membership test evaluates to True.