Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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




2 - Question

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.




3 - Question

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.




4 - Question

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

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

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

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

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

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

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

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.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners