Python Sets Quiz


12
Question 1
Question 1: What is the output of the following program?
sets = {1, 2, 3, 4, 4} 
print(sets) 

A
{1, 2, 3}
B
{1, 2, 3, 4}
C
{1, 2, 3, 4, 4}
D
Error
Python Sets Quiz    
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) 

A
{1, 2, 3, 4, 5}
B
{3, 4, 5, 1, 2, 3}
C
{1, 2, 3, 3, 4, 5}
D
Error
Python Sets Quiz    
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) 

A
{1, 2, 3, 4}
B
{1, 2, 3}
C
Invalid Syntax
D
Error
Python Sets Quiz    
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) 

A
{1, 2, 3, 4}
B
{1, 2, 3}
C
Invalid Syntax
D
None
Python Sets Quiz    
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)) 

A
3
B
6
C
Unexpected
D
Error
Python Sets Quiz    
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) 
A
{0, 1, 2, 3, 4, 5}
B
{0, 1, 2, 3, 4, 5, 6, 8}
C
{ 6, 8}
D
None
Python Sets Quiz    
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) 
A
{0, 1, 2, 3, 4, 5, 6, 8}
B
{0, 1, 3, 5, 6, 8}
C
{2, 4}
D
{0, 8, 6}
Python Sets Quiz    
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)
A
{0, 1, 2, 3, 4, 5, 6, 8}
B
{0, 8, 6}
C
{2, 4}
D
{0, 1, 3, 5, 6, 8}
Python Sets Quiz    
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) 

A
{0, 1, 2, 3, 4, 5, 6, 8}
B
{2, 4}
C
{0, 8, 6}
D
{0, 1, 3, 5, 6, 8}
Python Sets Quiz    
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) 
A
{1, 2, 4, 4, 3, 3, 3, 6, 5}
B
{1, 2, 3, 4, 5, 6}
C
[1, 2, 3, 4, 5, 6]
D
[1, 2, 4, 4, 3, 3, 3, 6, 5]
Python Sets Quiz    
Discuss it


Question 10 Explanation: 
All the elements in sets must be unique.
There are 11 questions to complete.
12
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.