Sets In Python

This section focuses on the "Set" in Python. These Multiple Choice Questions (mcq) should be practiced to improve the Python skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1. Which of the following Python code will create a set?
(i) set1=set((0,9,0))
(ii) set1=set([0,2,9])
(iii) set1={}

A. iii
B. i and ii
C. ii, iii
D. All of the above

View Answer


2. What will be the output of following Python code?

set1={0,0,9}
print(set1)

A. {0,0,9}
B. {0,9}
C. {9}
D. It will throw an error as there are two 0 while creating the set.

View Answer


3. What will be the output of following Python code?

set1={2,5,3}
set2={3,1}
set3={}
set3=set1&set2
print(set3)

A. {3}
B. {}
C. {2,5,3,1}
D. {2,5,1}

View Answer


4. What will be the output of following Python code?

list1=[1,3,4,2]
x=list1.pop(2)
print(set([x]))

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

View Answer


5. What will set1|set2 do?

If set1={"a","b",3}
set2={3,7}

A. Elements of set2 will get appended to set1
B. Elements of set1 will get appended to set2
C. A new set will be created with the elements of both set1 and set2
D. A new set will be created with the unique elements of set1 and set2.

View Answer


6. What will the below Python code do?

set1={"a",3,"b",3}
set1.remove(3)

A. It removes element with index position 3 from set1
B. It removes element 3 from set1
C. It removes only the first occurance of 3 from set1
D. No such function exists for set

View Answer


7. What will the below Python code do?

set1={2,3}
set2={3,2}
set3={2,1}
if(set1==set2):
print("yes")
else:
print("no")
if(set1==set3):
print("yes")
else:
print("no")

A. Yes, No
B. No, No
C. Yes, Yes
D. "==" is not supported for set in Python

View Answer


8. Suppose there are two sets, set1 and set2,where set1 is the superset of set2. It is required to get only the unique elements of both the sets. Which of the following will serve the purpose?

set1={2,3}
set2={3,2}
set3={2,1}
if(set1==set2):
print("yes")
else:
print("no")
if(set1==set3):
print("yes")
else:
print("no")

A. set1|set2
B. set1&set2
C. set1-set2
D. None of the above

View Answer


9. What will the be the result of below Python code?

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

A. {1,2,3,4}
B. {1,2,3}
C. {1,2,3,4,4}
D. It will throw an error as same element is added twice

View Answer


10. Which of the following options will give an error if set1={2,3,4,5}?

A. print(set1[0])
B. set1[0]=9
C. set1=set1+{7}
D. All of the above

View Answer






Discussion


* You must be logged in to add comment.