This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets – 4”.
1. Which of the following functions will return the symmetric difference between two sets, x and y?
a) x | y
b) x ^ y
c) x & y
d) x – y
View Answer
Explanation: The function x ^ y returns the symmetric difference between the two sets x and y. This is basically an XOR operation being performed on the two sets.
2. What will be the output of the following Python code snippet?
z=set('abc$de') 'a' in z
a) True
b) False
c) No output
d) Error
View Answer
Explanation: The code shown above is used to check whether a particular item is a part of a given set or not. Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in the absence of the quotes.
3. What will be the output of the following Python code snippet?
z=set('abc') z.add('san') z.update(set(['p', 'q'])) z
a) {‘abc’, ‘p’, ‘q’, ‘san’}
b) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}
c) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’}
d) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
View Answer
Explanation: The code shown first adds the element ‘san’ to the set z. The set z is then updated and two more elements, namely, ‘p’ and ‘q’ are added to it. Hence the output is: {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
4. What will be the output of the following Python code snippet?
s=set([1, 2, 3]) s.union([4, 5]) s|([4, 5])
a)
{1, 2, 3, 4, 5} {1, 2, 3, 4, 5}
b)
Error {1, 2, 3, 4, 5}
c)
{1, 2, 3, 4, 5} Error
d)
Error ErrorView Answer
Explanation: The first function in the code shown above returns the set {1, 2, 3, 4, 5}. This is because the method of the function union allows any iterable. However the second function results in an error because of unsupported data type, that is list and set.
5. What will be the output of the following Python code snippet?
for x in set('pqr'): print(x*2)
a)
pp qq rr
b)
pqr pqr
c) ppqqrr
d) pqrpqr
View Answer
Explanation: The code shown above prints each element of the set twice separately. Hence the output of this code is:
pp
rr
6. What will be the output of the following Python code snippet?
{a**2 for a in range(4)}
a) {1, 4, 9, 16}
b) {0, 1, 4, 9, 16}
c) Error
d) {0, 1, 4, 9}
View Answer
Explanation: The code shown above returns a set containing the square of values in the range 0-3, that is 0, 1, 2 and 3. Hence the output of this line of code is: {0, 1, 4, 9}.
7. What will be the output of the following Python function?
{x for x in 'abc'} {x*3 for x in 'abc'}
a)
{abc} aaa bbb ccc
b)
abc abc abc abc
c)
{‘a’, ‘b’, ‘c’} {‘aaa’, ‘bbb’, ‘ccc’}
d)
{‘a’, ‘b’, ‘c’} abc abc abcView Answer
Explanation: The first function prints each element of the set separately, hence the output is: {‘a’, ‘b’, ‘c’}. The second function prints each element of the set thrice, contained in a new set. Hence the output of the second function is: {‘aaa’, ‘bbb’, ‘ccc’}. (Note that the order may not be the same)
8. The output of the following code is: class<’set’>.
type({})
a) True
b) False
View Answer
Explanation: The output of the line of code shown above is: class<’dict’>. This is because {} represents an empty dictionary, whereas set() initializes an empty set. Hence the statement is false.
9. What will be the output of the following Python code snippet?
a=[1, 4, 3, 5, 2] b=[3, 1, 5, 2, 4] a==b set(a)==set(b)
a)
True False
b)
False False
c)
False True
d)
True TrueView Answer
Explanation: In the code shown above, when we check the equality of the two lists, a and b, we get the output false. This is because of the difference in the order of elements of the two lists. However, when these lists are converted to sets and checked for equality, the output is true. This is known as order-neutral equality. Two sets are said to be equal if and only if they contain exactly the same elements, regardless of order.
10. What will be the output of the following Python code snippet?
l=[1, 2, 4, 5, 2, 'xy', 4] set(l) l
a)
{1, 2, 4, 5, 2, ‘xy’, 4} [1, 2, 4, 5, 2, ‘xy’, 4]
b)
{1, 2, 4, 5, ‘xy’} [1, 2, 4, 5, 2, ‘xy’, 4]
c)
{1, 5, ‘xy’} [1, 5, ‘xy’]
d)
{1, 2, 4, 5, ‘xy’} [1, 2, 4, 5, ‘xy’]View Answer
Explanation: In the code shown above, the function set(l) converts the given list into a set. When this happens, all the duplicates are automatically removed. Hence the output is: {1, 2, 4, 5, ‘xy’}. On the other hand, the list l remains unchanged. Therefore the output is: [1, 2, 4, 5, 2, ‘xy’, 4].
Note that the order of the elements may not be the same.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Check Information Technology Books
- Check Python Books
- Apply for Python Internship
- Apply for Programming Internship