Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

1. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable

View Answer

Answer: b
Explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.




2 - Question

2. Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’}
b) dict([[1,”A”],[2,”B”]])
c) {1,”A”,2”B”}
d) { }

View Answer

Answer: c
Explanation: Option c is a set, not a dictionary.




3 - Question

3. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
for i,j in a.items():
    print(i,j,end=" ")

a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”

View Answer

Answer: a
Explanation: In the above code, variables i and j iterate over the keys and values of the dictionary respectively.




4 - Question

4. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}
print(a.get(1,4))

a) 1
b) A
c) 4
d) Invalid syntax for get method

View Answer

Answer: b
Explanation: The get() method returns the value of the key if the key is present in the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.




5 - Question

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

a={1:"A",2:"B",3:"C"}
print(a.get(5,4))

a) Error, invalid syntax
b) A
c) 5
d) 4

View Answer

Answer: d
Explanation: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.




6 - Question

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

a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) C
c) {1: 3, 2: 3, 3: 3}
d) No method called setdefault() exists for dictionary

View Answer

Answer: b
Explanation: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.




7 - Question

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

a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
b) None
c) Error
d) [1,3,6,10]

View Answer

Answer: a
Explanation: setdefault() will set dict[key]=default if key is not already in the dictionary.




8 - Question

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

a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}

View Answer

Answer: c
Explanation: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.




9 - Question

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

a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

a) Error, copy() method doesn’t exist for dictionaries
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
d) “None” is printed

View Answer
Answer: b
Explanation: Changes made in the copy of the dictionary isn’t reflected in the original one.

 




10 - Question

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

a={1:"A",2:"B",3:"C"}
a.clear()
print(a)

a) None
b) { None:None, None:None, None:None}
c) {1:None, 2:None, 3:None}
d) { }

View Answer

Answer: d
Explanation: The clear() method clears all the key-value pairs in the dictionary.




11 - Question

11. Which of the following isn’t true about dictionary keys?
a) More than one key isn’t allowed
b) Keys must be immutable
c) Keys must be integers
d) When duplicate keys encountered, the last assignment wins

View Answer

Answer: c
Explanation: Keys of a dictionary may be any data type that is immutable.




12 - Question

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

a={1:5,2:3,3:4}
a.pop(3)
print(a)

a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}

View Answer

Answer: b
Explanation: pop() method removes the key-value pair for the key mentioned in the pop() method.




13 - Question

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

a={1:5,2:3,3:4}
print(a.pop(4,9))

a) 9
b) 3
c) Too many arguments for pop() method
d) 4

View Answer

Answer: a
Explanation: pop() method returns the value when the key is passed as an argument and otherwise returns the default value(second argument) if the key isn’t present in the dictionary.




14 - Question

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

a={1:"A",2:"B",3:"C"}
for i in a:
    print(i,end=" ")

a) 1 2 3
b) ‘A’ ‘B’ ‘C’
c) 1 ‘A’ 2 ‘B’ 3 ‘C’
d) Error, it should be: for i in a.items():

View Answer

Answer: a
Explanation: The variable i iterates over the keys of the dictionary and hence the keys are printed.




15 - Question

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

>>> a={1:"A",2:"B",3:"C"}
>>> a.items()

a) Syntax error
b) dict_items([(‘A’), (‘B’), (‘C’)])
c) dict_items([(1,2,3)])
d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

View Answer

Answer: d
Explanation: The method items() returns list of tuples with each tuple having a key-value pair.

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