Skip to content
QuiznExam

Dictionaries in Python

Test your knowledge of Python dictionaries, including creation, accessing values, updating items, dictionary methods, iteration, and nested dictionaries.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which data type is used to create a dictionary in Python?

    • A. dict
    • B. list
    • C. tuple
    • D. set
    Show answer

    A. dict

    Python dictionaries are instances of the built-in dict type.

  2. 2. Which brackets are used to define a dictionary literal in Python?

    • A. []
    • B. {}
    • C. ()
    • D. <>
    Show answer

    B. {}

    Curly braces are used to create dictionary literals, such as {"name": "John"}.

  3. 3. Which of the following correctly creates a dictionary?

    • A. {"name": "Alice", "age": 25}
    • B. ["name", "Alice", "age", 25]
    • C. ("name", "Alice", "age", 25)
    • D. {"name", "Alice", "age", 25}
    Show answer

    A. {"name": "Alice", "age": 25}

    A dictionary contains key-value pairs separated by colons and separated from each other by commas.

  4. 4. How do you access the value associated with the key "name" in a dictionary called person?

    • A. person(name)
    • B. person["name"]
    • C. person->name
    • D. person.name
    Show answer

    B. person["name"]

    Dictionary values can be accessed using square brackets with the corresponding key.

  5. 5. What happens when you access a dictionary using a key that does not exist with square brackets?

    • A. It returns None
    • B. It returns False
    • C. It raises a KeyError
    • D. It creates the key automatically
    Show answer

    C. It raises a KeyError

    Accessing a missing key with dictionary[key] raises a KeyError.

  6. 6. Which dictionary method can safely retrieve a value without raising a KeyError when the key is missing?

    • A. find()
    • B. get()
    • C. fetch()
    • D. read()
    Show answer

    B. get()

    The get() method returns the value for a key and can return a default value if the key does not exist.

  7. 7. Which statement adds or updates a key-value pair in a dictionary?

    • A. person["age"] = 30
    • B. person.add("age", 30)
    • C. person.insert("age", 30)
    • D. person.updateValue("age", 30)
    Show answer

    A. person["age"] = 30

    Assigning a value to a key adds the key if it is new or updates the existing value if the key already exists.

  8. 8. Which method returns all keys in a dictionary?

    • A. keys()
    • B. allKeys()
    • C. keyList()
    • D. getKeys()
    Show answer

    A. keys()

    The keys() method returns a view containing the dictionary's keys.

  9. 9. Which method returns all values in a dictionary?

    • A. values()
    • B. getValues()
    • C. allValues()
    • D. valueList()
    Show answer

    A. values()

    The values() method returns a view containing the dictionary's values.

  10. 10. Which dictionary method returns key-value pairs?

    • A. pairs()
    • B. items()
    • C. entries()
    • D. keyvalues()
    Show answer

    B. items()

    The items() method returns a view of the dictionary's key-value pairs.

  11. 11. Which method removes a specified key and returns its value?

    • A. remove()
    • B. delete()
    • C. pop()
    • D. discard()
    Show answer

    C. pop()

    The pop() method removes the specified key and returns its associated value.

  12. 12. Which method removes all items from a dictionary?

    • A. empty()
    • B. clear()
    • C. removeAll()
    • D. reset()
    Show answer

    B. clear()

    The clear() method removes all key-value pairs from the dictionary.

  13. 13. Which operator can be used to check whether a key exists in a dictionary?

    • A. in
    • B. contains
    • C. exists
    • D. has
    Show answer

    A. in

    The in operator checks whether a specified key exists in a dictionary.

  14. 14. What does the following code print? data = {"a": 1, "b": 2}; print(data["b"])

    • A. 1
    • B. 2
    • C. b
    • D. None
    Show answer

    B. 2

    The key "b" is associated with the value 2, so data["b"] returns 2.

  15. 15. Which statement about Python dictionaries is correct?

    • A. Dictionary keys must be unique
    • B. Dictionary values must be unique
    • C. Dictionaries can only contain strings
    • D. Dictionaries cannot contain nested dictionaries
    Show answer

    A. Dictionary keys must be unique

    Each key in a dictionary is unique. Assigning a new value to an existing key replaces its previous value.