Python Questions and Answers – Sets – 3

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Sets – 3”.

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

s=set()
type(s)

a) <’set’>
b) <class ‘set’>
c) set
d) class set
View Answer

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

2. The following Python code results in an error.

s={2, 3, 4, [5, 6]}

a) True
b) False
View Answer

Answer: a
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

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

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}
View Answer

Answer: d
Explanation: 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?

advertisement
s={2, 5, 6, 6, 7}
s

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

Answer: b
Explanation: 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}

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

Answer: b
Explanation: 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]
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.