Python MCQs on SET: SET 4

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

s=set()

type(s)

a) <’set’>

b) <class ‘set’>

c) set

d) class set

Check Answer

Answer: b

32. The following Python code results in an error.

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

a) True

b) False

Check Answer

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

33. Set makes use of __________

Dictionary makes use of ____________

a) keys, keys

b) key values, keys

c) keys, key values

d) key values, key values

Check Answer

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

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

Check Answer

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

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

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

s

a) {2, 5, 7}

b) {2, 5, 6, 7}

c) {2, 5, 6, 6, 7}

d) Error

Check Answer

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

36. Input order is preserved in sets.

a) True

b) False

Check Answer

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

37. 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]

Check Answer

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

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

Check Answer

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

39. Which of the following functions cannot be used on heterogeneous sets?

a) pop

b) remove

c) update

d) sum

Check Answer

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

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

Check Answer

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

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