Test Series - python

Test Number 57/108

Q: 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
Solution: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
Q: 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. { }
Solution: Option c is a set, not a dictionary.
Q: 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”
Solution: In the above code, variables i and j iterate over the keys and values of the dictionary respectively.
Q: 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
Solution: 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.
Q: 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
Solution: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.
Q: 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
Solution: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.
Q: 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]
Solution: setdefault() will set dict[key]=default if key is not already in the dictionary.
Q: 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’}
Solution: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.
Q: 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
Solution: Changes made in the copy of the dictionary isn’t reflected in the original one.
Q: 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. { }
Solution: The clear() method clears all the key-value pairs in the dictionary.

You Have Score    /10