Master Python Dictionaries with These 30 Multiple Choice Questions

Master Python Dictionaries with These 30 Multiple Choice Questions

30 multiple-choice questions about Python dictionaries:

  1. What is the data structure used to represent dictionaries in Python?

    • A) Arrays

    • B) Linked Lists

    • C) Hash Tables

    • D) Stacks

  2. How do you initialize an empty dictionary in Python?

    • A) dict()

    • B) {}

    • C) []

    • D) ()

  3. In a dictionary, keys must be:

    • A) Immutable

    • B) Mutable

    • C) Ordered

    • D) Sorted

  4. Which method is used to access the value associated with a key in a dictionary?

    • A) get()

    • B) retrieve()

    • C) access()

    • D) value()

  5. What happens if you try to access a key that doesn't exist in a dictionary?

    • A) It raises a KeyError

    • B) It returns None

    • C) It returns an empty dictionary

    • D) It returns an empty string

  6. How do you add a new key-value pair to an existing dictionary?

    • A) Using the insert() method

    • B) Using the add() method

    • C) Using the append() method

    • D) Using square brackets []

  7. Can a dictionary have multiple keys with the same value?

    • A) Yes

    • B) No

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

    • A) delete()

    • B) remove()

    • C) pop()

    • D) erase()

  9. Which method is used to iterate over the keys in a dictionary?

    • A) keys()

    • B) iterate()

    • C) get_keys()

    • D) get()

  10. How do you merge two dictionaries in Python?

    • A) Using the merge() method

    • B) Using the combine() method

    • C) Using the update() method

    • D) Using the concatenate() method

  11. Which method is used to sort a dictionary by its keys?

    • A) sort()

    • B) sorted()

    • C) sort_keys()

    • D) order_keys()

  12. How do you check if a key exists in a dictionary?

    • A) Using the exists() method

    • B) Using the contains() method

    • C) Using the in keyword

    • D) Using the has_key() method

  13. How do you create a dictionary from two lists?

    • A) Using loops

    • B) Using the dict() constructor

    • C) Using the zip() function

    • D) Using list comprehension

  14. What is the purpose of the collections.defaultdict class?

    • A) To create dictionaries with default values

    • B) To create dictionaries with ordered keys

    • C) To create dictionaries with sorted values

    • D) To create dictionaries with unique keys

  15. How do you create a shallow copy of a dictionary?

    • A) Using the copy() method

    • B) Using the clone() method

    • C) Using the shallow_copy() method

    • D) Using the duplicate() method

  16. Which method is used to clear all elements from a dictionary?

    • A) clear()

    • B) remove_all()

    • C) delete_all()

    • D) empty()

  17. How do you check if a dictionary is empty?

    • A) Using the empty() method

    • B) Using the is_empty() method

    • C) Using the len() function

    • D) Using the size() method

  18. Can dictionaries be nested in Python?

    • A) Yes

    • B) No

  19. How do you sort a dictionary by its values?

    • A) Using the sort_values() method

    • B) Using the sorted() function with a custom key

    • C) Using the order_values() method

    • D) Using the sort() method

  20. Which method is used to create a dictionary with default values?

    • A) defaultdict()

    • B) initialize()

    • C) set_default()

    • D) create_default()

  21. What is the difference between dict.keys(), dict.values(), and dict.items()?

    • A) They return the same result

    • B) keys() returns keys, values() returns values, and items() returns key-value pairs

    • C) keys() returns values, values() returns keys, and items() returns keys

    • D) They are not valid methods in Python dictionaries

  22. How do you create a deep copy of a dictionary?

    • A) Using the deepcopy() function from the copy module

    • B) Using the clone() method

    • C) Using the duplicate() method

    • D) Using the deep_copy() method

  23. What is the difference between dict.get(key) and dict[key]?

    • A) There is no difference

    • B) get() returns the value if the key exists, otherwise None, while [key] raises a KeyError if the key doesn't exist

    • C) get() raises a KeyError if the key doesn't exist, while [key] returns None

    • D) get() returns True if the key exists, otherwise False, while [key] returns the value

  24. How do you iterate over the values in a dictionary?

    • A) Using a for loop with values() method

    • B) Using a for loop with items() method

    • C) Using a while loop with values() method

    • D) Using a while loop with items() method

  25. How do you iterate over key-value pairs in a dictionary?

    • A) Using a for loop with keys() method

    • B) Using a for loop with values() method

    • C) Using a for loop with items() method

    • D) Using a while loop with keys() method

  26. Which method is used to create a dictionary with keys from a sequence and default value for all keys?

    • A) defaultdict()

    • B) setdefault()

    • C) fromkeys()

    • D) initialize()

  27. How do you create a dictionary comprehension in Python?

    • A) [key: value for key, value in iterable]

    • B) {key: value for key, value in iterable}

    • C) (key: value for key, value in iterable)

    • D) `<key, value> for key, value in

iterable`

  1. Which method is used to check the presence of a key in a dictionary without raising an error?

    • A) contains_key()

    • B) has_key()

    • C) key_exists()

    • D) get()

  2. Which method is used to update a dictionary with elements from another dictionary or from an iterable of key-value pairs?

    • A) merge()

    • B) update()

    • C) extend()

    • D) append()

  3. How do you sort a dictionary by its keys in descending order?

    • A) sorted(d, reverse=True)

    • B) sort(d, reverse=True)

    • C) d.sort(reverse=True)

    • D) sort_keys(d, reverse=True)

Answers:

  1. C) Hash Tables

  2. B) {}

  3. A) Immutable

  4. A) get()

  5. A) It raises a KeyError

  6. D) Using square brackets []

  7. A) Yes

  8. C) pop()

  9. A) keys()

  10. C) Using the update() method

  11. B) sorted()

  12. C) Using the in keyword

  13. C) Using the zip() function

  14. A) To create dictionaries with default values

  15. A) Using the copy() method

  16. A) clear()

  17. C) Using the len() function

  18. A) Yes

  19. B) Using the sorted() function with a custom key

  20. A) defaultdict()

  21. B) keys() returns keys, values() returns values, and items() returns key-value pairs

  22. A) Using the deepcopy() function from the copy module

  23. B) get() returns the value if the key exists, otherwise None, while [key] raises a KeyError if the key doesn't exist

  24. A) Using a for loop with values() method

  25. C) Using a for loop with items() method

  26. C) fromkeys()

  27. B) {key: value for key, value in iterable}

  28. D) get()

  29. B) update()

  30. A) sorted(d, reverse=True)

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!