Python Sets Quiz

Last Updated : 18 Sep, 2020
Question 1
Question 1: What is the output of the following program?
sets = {1, 2, 3, 4, 4} 
print(sets) 

Cross
{1, 2, 3}
Tick
{1, 2, 3, 4}
Cross
{1, 2, 3, 4, 4}
Cross
Error


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) 

Tick
{1, 2, 3, 4, 5}
Cross
{3, 4, 5, 1, 2, 3}
Cross
{1, 2, 3, 3, 4, 5}
Cross
Error


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) 

Cross
{1, 2, 3, 4}
Tick
{1, 2, 3}
Cross
Invalid Syntax
Cross
Error


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) 

Cross
{1, 2, 3, 4}
Cross
{1, 2, 3}
Cross
Invalid Syntax
Tick
None


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)) 

Cross
3
Cross
6
Cross
Unexpected
Tick
Error


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) 
Cross
{0, 1, 2, 3, 4, 5}
Tick
{0, 1, 2, 3, 4, 5, 6, 8}
Cross
{ 6, 8}
Cross
None


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) 
Cross
{0, 1, 2, 3, 4, 5, 6, 8}
Cross
{0, 1, 3, 5, 6, 8}
Tick
{2, 4}
Cross
{0, 8, 6}


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)
Cross
{0, 1, 2, 3, 4, 5, 6, 8}
Tick
{0, 8, 6}
Cross
{2, 4}
Cross
{0, 1, 3, 5, 6, 8}


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) 

Cross
{0, 1, 2, 3, 4, 5, 6, 8}
Cross
{2, 4}
Cross
{0, 8, 6}
Tick
{0, 1, 3, 5, 6, 8}


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) 
Cross
{1, 2, 4, 4, 3, 3, 3, 6, 5}
Tick
{1, 2, 3, 4, 5, 6}
Cross
[1, 2, 3, 4, 5, 6]
Cross
[1, 2, 4, 4, 3, 3, 3, 6, 5]


Question 10-Explanation: 
All the elements in sets must be unique.
There are 11 questions to complete.

Share your thoughts in the comments

Similar Reads