Python MCQs on SET: SET 5

41. 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

Check Answer

Answer: b
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.

42. 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

Check Answer

Answer: a
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.

43. 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’}

Check Answer

Answer: d
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’}

44. 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
Error

Check Answer

Answer: c
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.

45. 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

Check Answer

Answer: a
The code shown above prints each element of the set twice separately. Hence the output of this code is:
pp
qq
rr

46. 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}

Check Answer

Answer: d
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}.

47. 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
abc

Check Answer

Answer: c
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)

48. The output of the following code is: class<’set’>.

type({})

a) True

b) False

Check Answer

Answer: b
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.

49. 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
True

Check Answer

Answer: c
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.

50. 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’]

Check Answer

Answer: b
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.

Page  1  2  3  4  5  6

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment