Python Sets Quiz
Question 1 |
Question 1: What is the output of the following program?
sets = {1, 2, 3, 4, 4}
print(sets)
{1, 2, 3} | |
{1, 2, 3, 4} | |
{1, 2, 3, 4, 4} | |
Error |
Discuss it
Question 1 Explanation:
Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence output will be {1, 2, 3, 4}.
Question 2 |
Question 2: What is the output of the following program?
sets = {3, 4, 5}
sets.update([1, 2, 3])
print(sets)
{1, 2, 3, 4, 5} | |
{3, 4, 5, 1, 2, 3} | |
{1, 2, 3, 3, 4, 5} | |
Error |
Discuss it
Question 2 Explanation:
The method update adds elements to a set.
Question 3 |
Question 3: What is the output of the following program?
set1 = {1, 2, 3}
set2 = set1.copy()
set2.add(4)
print(set1)
{1, 2, 3, 4} | |
{1, 2, 3} | |
Invalid Syntax | |
Error |
Discuss it
Question 3 Explanation:
In the above piece of code, set2 is barely a copy and not an alias of set1. Hence any change made in set2 isn’t reflected in set1.
Question 4 |
Question 4: What is the output of the following program?
set1 = {1, 2, 3}
set2 = set1.add(4)
print(set2)
{1, 2, 3, 4} | |
{1, 2, 3} | |
Invalid Syntax | |
None |
Discuss it
Question 4 Explanation:
The add method doesn’t return anything. Hence there will be no output.
Question 5 |
Question 5: What is the output of the following program?
set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(len(set1 + set2))
3 | |
6 | |
Unexpected | |
Error |
Discuss it
Question 5 Explanation:
The unsupported operand type(s) for +: ‘set’ and ‘set’.
Question 6 |
Question 6: What is the output of the following program?
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
print(A | B)
{0, 1, 2, 3, 4, 5} | |
{0, 1, 2, 3, 4, 5, 6, 8} | |
{ 6, 8} | |
None |
Discuss it
Question 6 Explanation:
The "|" operator is used for union.
Question 7 |
Question 7: What is the output of the following program?
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
print(A & B)
{0, 1, 2, 3, 4, 5, 6, 8} | |
{0, 1, 3, 5, 6, 8} | |
{2, 4} | |
{0, 8, 6} |
Discuss it
Question 7 Explanation:
The "&" operator is used for intersection.
Question 8 |
Question 8: What is the output of the following program?
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
print( A - B)
{0, 1, 2, 3, 4, 5, 6, 8} | |
{0, 8, 6} | |
{2, 4} | |
{0, 1, 3, 5, 6, 8} |
Discuss it
Question 8 Explanation:
The "-" operator is used to get the difference between two iterable.
Question 9 |
Question 9: What is the output of the following program?
A = {0, 2, 4, 6, 8};
B = {1, 2, 3, 4, 5};
print( A ^ B)
{0, 1, 2, 3, 4, 5, 6, 8} | |
{2, 4} | |
{0, 8, 6} | |
{0, 1, 3, 5, 6, 8} |
Discuss it
Question 9 Explanation:
The "^" operator is used to get the Symmetric difference.
Question 10 |
Question 10: What is the output of the following program?
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5]) print(set1)
{1, 2, 4, 4, 3, 3, 3, 6, 5} | |
{1, 2, 3, 4, 5, 6} | |
[1, 2, 3, 4, 5, 6] | |
[1, 2, 4, 4, 3, 3, 3, 6, 5] |
Discuss it
Question 10 Explanation:
All the elements in sets must be unique.
There are 11 questions to complete.

