250+ TOP MCQs on Python Sets and Answers

Python Multiple Choice Questions on “Sets – 3”.

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

a) <’set’>
b)
c) set
d) class set

Answer: b
Clarification: When we find the type of a set, the output returned is: .

2. The following Python code results in an error.

a) True
b) False

Answer: a
Clarification: The set data type makes use of a principle known as hashing. This means that each item in the set should be hashable. Hashable in this context means immutable. List is mutable and hence the line of code shown above will result in an error.

3. Set makes use of __________
Dictionary makes use of ____________
a) keys, keys
b) key values, keys
c) keys, key values
d) key values, key values

Answer: c
Clarification: Set makes use of keys.
Dictionary makes use of key values.

4. Which of the following lines of code will result in an error?
a) s={abs}
b) s={4, ‘abc’, (1,2)}
c) s={2, 2.2, 3, ‘xyz’}
d) s={san}

Answer: d
Clarification: The line: s={san} will result in an error because ‘san’ is not defined. The line s={abs} does not result in an error because abs is a built-in function. The other sets shown do not result in an error because all the items are hashable.

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

a) {2, 5, 7}
b) {2, 5, 6, 7}
c) {2, 5, 6, 6, 7}
d) Error

Answer: b
Clarification: Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Therefore the output is: {2, 5, 6, 7}

  250+ TOP MCQs on Python Precedence and Associativity and Answers

6. Input order is preserved in sets.
a) True
b) False

Answer: b
Clarification: The input order in sets is not maintained. This is demonstrated by the code shown below:

>>> s={2, 6, 8, 1, 5}
>>> s
{8, 1, 2, 5, 6}

7. Write a list comprehension for number and its cube for:

l=[1, 2, 3, 4, 5, 6, 7, 8, 9]

a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]

Answer: a
Clarification: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].

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

s={1, 2, 3}
s.update(4)
s

a) {1, 2, 3, 4}
b) {1, 2, 4, 3}
c) {4, 1, 2, 3}
d) Error

Answer: d
Clarification: The code shown above will result in an error because the argument given to the function update should necessarily be an iterable. Hence if we write this function as: s.update([4]), there will be no error.

9. Which of the following functions cannot be used on heterogeneous sets?
a) pop
b) remove
c) update
d) sum

Answer: d
Clarification: The functions sum, min and max cannot be used on mixed type (heterogeneous) sets. The functions pop, remove, update etc can be used on homogenous as well as heterogeneous sets. An example of heterogeneous sets is: {‘abc’, 4, (1, 2)}

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

s={4>3, 0, 3-3}
all(s)
any(s)

a)

True
False

b)

False
True

c)

True 
True

d)

False
False

View Answer

Answer: b
Clarification: The function all returns true only if all the conditions given are true. But in the example shown above, we have 0 as a value. Hence false is returned. Similarly, any returns true if any one condition is true. Since the condition 4>3 is true, true is returned.

  250+ TOP MCQs on Python Sets and Answers

 

 

Leave a Comment

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

Scroll to Top