Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

1. Which of these about a frozenset is not true?
a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type

View Answer

Answer: a
Explanation: A frozenset is an immutable data type.




2 - Question

2. What is the syntax of the following Python code?

>>> a=frozenset(set([5,6,7]))
>>> a

a) {5,6,7}
b) frozenset({5,6,7})
c) Error, not possible to convert set into frozenset
d) Syntax error

View Answer

Answer: b
Explanation: The above piece of code is the correct syntax for creating a frozenset.




3 - Question

3. Is the following Python code valid?

>>> a=frozenset([5,6,7])
>>> a
>>> a.add(5)

a) Yes, now a is {5,5,6,7}
b) No, frozen set is immutable
c) No, invalid syntax for add method
d) Yes, now a is {5,6,7}

View Answer

Answer: b
Explanation: Since a frozen set is immutable, add method doesn’t exist for frozen method.




4 - Question

4. Set members must not be hashable.
a) True
b) False

View Answer

Answer: b
Explanation: Set members must always be hashable.




5 - Question

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

>>> a={3,4,5}
>>> a.update([1,2,3])
>>> a

a) Error, no method called update for set data type
b) {1, 2, 3, 4, 5}
c) Error, list can’t be added to set
d) Error, duplicate item present in list

View Answer

Answer: b
Explanation: The method update adds elements to a set.




6 - Question

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

>>> a={1,2,3}
>>> a.intersection_update({2,3,4,5})
>>> a

a) {2,3}
b) Error, duplicate item present in list
c) Error, no method called intersection_update for set data type
d) {1,4,5}

View Answer

Answer: a
Explanation: The method intersection_update returns a set which is an intersection of both the sets.




7 - Question

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

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

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

View Answer

Answer: c
Explanation: Any change made in b is reflected in a because b is an alias of a.




8 - Question

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

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

View Answer

Answer: a
Explanation: In the above piece of code, b is barely a copy and not an alias of a. Hence any change made in b isn’t reflected in a.




9 - Question

9. What will be the output of the following Python 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

View Answer

Answer: d
Explanation: The method add returns nothing, hence nothing is printed.




10 - Question

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

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

View Answer

Answer: a
Explanation: – operator gives the set of elements in set a but not in set b.




11 - Question

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

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

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

View Answer

Answer: b
Explanation: The second parameter is the start value for the sum of elements in set a. Thus, sum(a,5) = 5+(5+6+7)=23.




12 - Question

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

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

View Answer

Answer: c
Explanation: Set comprehensions are allowed.




13 - Question

13. What will be the output of the following Python 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

View Answer

Answer: b
Explanation: Duplicate elements in a+b is eliminated and the length of a+b is computed.




14 - Question

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

View Answer

Answer: a
Explanation: The method issubset() returns True if b is a proper subset of a.




15 - Question

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

View Answer

Answer: a
Explanation: The method issubset() returns True if b is a proper subset of a.

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