Sets In Python: MCQs on Sets in Python Programming

Sets In Python. Multiple Choice Questions on the topic of sets in Python Language.

 

Sets In Python

 

 

Is the following Python code valid ??

a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)

a) False
b) True
c) Syntax error for issuperset() method
d) Error, no method called issuperset() exists

 

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) Error, subsets aren’t allowed
d) Yes, {7,5} is printed

 

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

a={1,2,3}
b={1,2,3}
c=a.issubset(b)
print(c)

a) True
b) Error, no method called issubset() exists
c) Syntax error for issubset() method
d) False

 

What will be the output of the following Python ??

>>> a={3,4,5}
>>> b={5,6,7}
>>> a|b

a) Invalid operation
b) {3, 4, 5, 6, 7}
c) {5}
d) {3,4,6,7}

 

Sets In Python

 

What will be the output of the following code ??

>>> a={5,6,7,8}
>>> b={7,8,9,10}
>>> len(a+b)

a) 8
b) Error, unsupported operand ‘+’ for sets
c) 6
d) Nothing is displayed

 

What will be the output ??

>>> a={5,6,7,8}
>>> b={7,5,6,8}
>>> a==b

a) True
b) False

 

What will be the output of the following ??

>>> a={1,2,3}
>>> {x*2 for x in a|{4,5}}

a) {2,4,6}
b) Error, set comprehensions aren’t allowed
c) {8, 2, 10, 4, 6}
d) {8,10}

 

What will be the output of this code ??

>>> s={5,6}
>>> s*3

a) Error as unsupported operand type for set data type
b) {5,6,5,6,5,6}
c) {5,6}
d) Error as multiplication creates duplicate elements which isn’t allowed

 

Sets In Python

 

What will be the output of this ??

>>> a={5,6,7}
>>> sum(a,5)

a) 5
b) 23
c) 18
d) Invalid syntax for sum method, too many arguments

 

What will be the output of the code ??

>>> a={5,6,7,8}
>>> b={7,8,10,11}
>>> a^b

a) {5,6,7,8,10,11}
b) {7,8}
c) Error as unsupported operand type of set data type
d) {5,6,10,11}

 

What will be the output of the following Python code snippet ??

>>> a={1,2,3}
>>> b=frozenset([3,4,5])
>>> a-b

a) {1,2}
b) Error as difference between a set and frozenset can’t be found out
c) Error as unsupported operand type for set data type
d) frozenset({1,2})

 

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

 

Sets In Python

 

What will be the output of code ??

>>> a={1,2,3}
>>> b=a.add(4)
>>> b

a) 0
b) {1,2,3,4}
c) {1,2,3}
d) Nothing is printed

 

What is the output ??

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

 

What will be the output ??

>>> a={1,2,3}
>>> b=a.copy()
>>> b.add(4)
>>> a

a) {1,2,3}
b) Error, invalid syntax for add
c) {1,2,3,4}
d) Error, copying of sets isn’t allowed

Leave a Comment

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

error: Content is protected !!