[go: up one dir, main page]

0% found this document useful (0 votes)
19 views6 pages

Disctionaries

The document contains 30 multiple choice questions about dictionaries in Python. It covers dictionary basics like defining, accessing and updating dictionaries as well as various dictionary methods like keys(), values(), items(), get(), pop(), copy() etc.

Uploaded by

sridhar k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Disctionaries

The document contains 30 multiple choice questions about dictionaries in Python. It covers dictionary basics like defining, accessing and updating dictionaries as well as various dictionary methods like keys(), values(), items(), get(), pop(), copy() etc.

Uploaded by

sridhar k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Which of the following defines an empty dictionary?

 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

11. Which of the following returns a shallow copy of the dictionary?


 A) copy()
 B) duplicate()
 C) shallow_copy()
 D) clone()

Answer: A) copy()

12. Which of the following is used to merge two dictionaries?


 A) concat()
 B) merge()
 C) update()
 D) join()

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

14. What is the result of the following operation?


pythonCopy code
d = { "a" : 1 , "b" : 2 } print (d.clear())
 A) {"a": 1, "b": 2}
 B) None
 C) {}
 D) Error

Answer: B) None

15. What will be the output of the following?


pythonCopy code
d = dict (name= "John" , age= 30 ) print (d)
 A) {'name': 'John', 'age': 30}
 B) {'name': 'John', 30: 'age'}
 C) Error
 D) ['name', 'John', 'age', 30]

Answer: A) {'name': 'John', 'age': 30}

16. Which one is not a correct statement?


 A) Dictionaries are ordered collections since Python 3.7.
 B) Dictionary keys are immutable.
 C) Dictionaries can contain multiple items for the same key.
 D) Dictionary values can be of any type.

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

18. What is the result of the following code?


pythonCopy code
d1 = { "a" : 1 } d2 = d1 d2[ "a" ] = 2 print (d1[ "a" ])
 A) 1
 B) 2
 C) KeyError
 D) None

Answer: B) 2

19. Which data type cannot be used as a dictionary key?


 A) Integer
 B) String
 C) List
 D) Tuple

Answer: C) List

20. What is the purpose of dict() constructor?


 A) Convert lists to dictionaries.
 B) Convert tuples to dictionaries.
 C) Create a new dictionary.
 D) All of the above.

Answer: D) All of the above.

21. Which statement is true regarding dictionary keys?


 A) They can be of mixed types.
 B) They must be unique within a dictionary.
 C) They are mutable types.
 D) They can be accessed using an index.

Answer: B) They must be unique within a dictionary.

22. Which of the following will raise an error?


pythonCopy code
d = { 1 : "a" , 2 : "b" } print (d[ 3 ])
 A) 1
 B) "a"
 C) KeyError
 D) None
Answer: C) KeyError

23. What will be the output of the following code?


pythonCopy code
d = { "name" : "John" , "age" : 30 } d[ "name" ] = "Doe" print (d)
 A) {"name": "John", "age": 30}
 B) {"name": "Doe", "age": 30}
 C) {"name": "John", "age": 31}
 D) {"name": "Doe", "age": 31}

Answer: B) {"name": "Doe", "age": 30}

24. How do you remove a key-value pair from a dictionary?


 A) remove()
 B) delete()
 C) discard()
 D) Using del

Answer: D) Using del

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

Answer: B) dict([(1,"one"), (2,"two"), (3,"three")])

26. Which of the following is false about dictionaries in Python?


 A) Dictionaries aren't ordered until Python 3.7.
 B) Dictionary keys are case sensitive.
 C) Dictionaries are mutable.
 D) Dictionaries can be nested.

Answer: A) Dictionaries aren't ordered until Python 3.7.

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

28. Which method is used to remove all elements from a dictionary?


 A) delete()
 B) clear()
 C) remove_all()
 D) discard()

Answer: B) clear()

29. What is the output of the following code?


pythonCopy code
d = { "name" : "John" , "age" : 30 } d.pop( "name" ) print (d)
 A) {"name": "John", "age": 30}
 B) {"name": "Doe", "age": 30}
 C) {"age": 30}
 D) Error

Answer: C) {"age": 30}

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

You might also like