Python Multiple Choice Questions – Sets

Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets”.

Q1. Which of these about a set is not true?
A. Immutable data type
B. Does not allow duplicate values
C. Data type with unordered values
D. Mutable data type

Show Answer Answer:-A. Immutable data type
Explanation A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.


Q 2. Which of the following is not the correct syntax for creating a set?
A. set([[1,2],[3,4]])
B. set([1,2,2,3,4])
C. set((1,2,3,4))
D. {1,2,3,4}

Show Answer Answer:-A. set([[1,2],[3,4]])
Explanation The argument given for the set must be an iterable.


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

nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
A. 4
B. 7
C. 8
D. Error, invalid syntax for formation of set

Show Answer Answer:-A. 4
Explanation A set doesn’t have duplicate items.

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

a = [5,5,6,7,7,7]
b = set(a)
def test(lst):
if lst in b:
return 1
else:
return 0
for i in filter(test, a):
print(i,end=” “)
A. 5 5 6
B. 5 6 7
C. 5 6 7 7 7
D. 5 5 6 7 7 7

Show Answer Answer:-D. 5 5 6 7 7 7
Explanation The filter function will return all the values from list a which are true when passed to function test. Since all the members of the set are non-duplicate members of the list, all of the values will return true. Hence all the values in the list are printed.


Q 5. Which of the following statements is used to create an empty set?
A. { }
B. set()
C. [ ]
D. ( )

Show Answer Answer:-B. set()
Explanation { } creates a dictionary not a set. Only set() creates an empty set.


Q 6. What will be the output of the following Python code?

>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
A. {1,2}
B. True
C. False
D. Invalid operation

Show Answer Answer:-B. True
Explanation a


Q 7. If a={5,6,7,8}, which of the following statements is false?
A. print(len(a))
B. print(min(a))
C. a.remove(5)
D. a[2]=45

Show Answer Answer:-D. a[2]=45
Explanation The members of a set cannot be accessed by their index values since the elements of the set are unordered.


Q 8. If a={5,6,7}, what happens when a.add(5) is executed?
A. a={5,5,6,7}
B. a={5,6,7}
C. Error as there is no add function for set data type
D. Error as 5 already exists in the set

Show Answer Answer:-B. a={5,6,7}
Explanation There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.


Q 9. What will be the output of the following Python code?

>>> a={4,5,6}
>>> b={2,8,6}
>>> a+b
A. {4,5,6,2,8}
B. {4,5,6,2,8,6}
C. Error as unsupported operand type for sets
D. Error as the duplicate item 6 is present in both sets

Show Answer Answer:-C. Error as unsupported operand type for sets
Explanation Execute in python shell to verify.


Q 10. What will be the output of the following Python code?

>>> a={4,5,6}
>>> b={2,8,6}
>>> a-b
A. {4,5}
B. {6}
C. Error as unsupported operand type for set data type
D. Error as the duplicate item 6 is present in both sets

Show Answer Answer:-A. {4,5}
Explanation operator gives the set of elements in set a but not in set b.


Q 11. What will be the output of the following Python code?

>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b
A. {5,6,10,11}
B. {7,8}
C. {5,6,7,8,10,11}
D. Error as unsupported operand type of set data type

Show Answer Answer:-A. {5,6,10,11}
Explanation ^ operator returns a set of elements in set A or set B, but not in both (symmetric difference).


Q 12. What will be the output of the following Python code?

>>> s={5,6}
>>> s*3
A. {5,6}
B. {5,6,5,6,5,6}
C. Error as unsupported operand type for set data type
D. Error as multiplication creates duplicate elements which isn’t allowed

Show Answer Answer:-C. Error as unsupported operand type for set data type
Explanation The multiplication operator isn’t valid for the set data type.


Q 13. What will be the output of the following Python code?

>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b
A. True
B. False

Show Answer Answer:-A. True
Explanation It is possible to compare two sets and the order of elements in both the sets doesn’t matter if the values of the elements are the same.


Q 14. What will be the output of the following Python code?

>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b
A. {5}
B. {3,4,6,7}
C. {3, 4, 5, 6, 7}
D. Invalid operation

Show Answer Answer:-C. {3, 4, 5, 6, 7}
Explanation The operation in the above piece of code is union operation. This operation produces a set of elements in both set a and set b.


Q 15. Is the following Python code valid?

a={3,4,{7,5}}
print(a[2][0])
A. Yes, 7 is printed
B. Error, elements of a set can’t be printed
C. Yes, {7,5} is printed
D. Error, subsets aren’t allowed

Show Answer Answer:-D. Error, subsets aren’t allowed
Explanation In python, elements of a set must not be mutable and sets are mutable. Thus, subsets can’t exist.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page