Disctionaries
Disctionaries
A) []
B) {}
C) ()
D) None
Answer: B) {}
2. Which method returns a list of all the keys in a dictionary?
A) keys()
B) get_keys()
C) items()
D) values()
Answer: A) keys()
3. What will be the output of the following code?
pythonCopy code
d = { "a" : 1 , "b" : 2 } print (d.get( "c" , 3 ))
A) 1
B) 2
C) 3
D) KeyError
Answer: C) 3
4. Which of the following methods removes an item from the dictionary?
A) discard()
B) delete()
C) remove()
D) pop()
Answer: D) pop()
5. What will be the result of the following?
pythonCopy code
d = { "a" : 1 , "b" : 2 } d[ "c" ] = 3
A) {"a": 1, "b": 2, "c": 3}
B) {"a": 1, "b": 2}
C) KeyError
D) ValueError
Answer: A) {"a": 1, "b": 2, "c": 3}
6. Which method returns a list of all values in a dictionary?
A) keys()
B) get_values()
C) items()
D) values()
Answer: D) values()
7. What is the output of the following code?
pythonCopy code
d = { "a" : 1 , "b" : 2 } print (d.setdefault( "a" , 3 ))
A) 1
B) 2
C) 3
D) None
Answer: A) 1
8. Which of the following is not a valid dictionary declaration?
A) d = {1: "one", 2: "two"}
B) d = {"one": 1, "two": 2}
C) d = {[1,2]: "one two"}
D) d = {(1,2): "one two"}
Answer: C) d = {[1,2]: "one two"}
9. How do you delete a dictionary?
A) del
B) pop
C) remove
D) discard
Answer: A) del
10. What is the output of the following code?
pythonCopy code
d = { "a" : 1 , "b" : 2 } print ( len (d))
A) 1
B) 2
C) 3
D) 4
Answer: B) 2
Answer: A) copy()
Answer: C) update()
13. How do you check if a key exists in a dictionary?
A) has_key()
B) exists()
C) contains()
D) Using in
Answer: D) Using in
Answer: B) None
Answer: C) Dictionaries can contain multiple items for the same key.
17. Which of the following methods returns a list of tuples, where each tuple
is a key-value pair?
A) keys()
B) values()
C) pairs()
D) items()
Answer: D) items()
Answer: B) 2
Answer: C) List
25. Which of the following will create a dictionary with keys 1, 2, 3 and
values "one", "two", "three"?
A) dict([[1,"one"], [2,"two"], [3,"three"]])
B) dict([(1,"one"), (2,"two"), (3,"three")])
C) {[1,"one"], [2,"two"], [3,"three"]}
D) {(1,"one"), (2,"two"), (3,"three")}
27. Which method would be used to get a value of a key without throwing
an error if the key doesn't exist?
A) get()
B) fetch()
C) retrieve()
D) lookup()
Answer: A) get()
Answer: B) clear()
30. Which method can be used to insert a new key-value pair into the
dictionary only if the key doesn't already exist?
A) insert()
B) add()
C) setdefault()
D) update()
Answer: C) setdefault()