Monday, 23 December 2024

Multiple-Choice Quiz on Python Dictionaries

  1. What is the primary structure used to store data in a Python dictionary?
    A) List
    B) Set
    C) Key-Value pairs
    D) Tuple

  2. Which of the following is true about the keys in a Python dictionary?
    A) They can be mutable data types
    B) They must be unique
    C) They can be of any data type, including lists
    D) They are automatically sorted

  3. How do you access the value associated with a key in a Python dictionary?
    A) Using the index number
    B) Using the key itself in square brackets
    C) Using the key in parentheses
    D) Using the key with a dot notation

  4. What will be the output of the following code: d = {'a': 1, 'b': 2}; print(d['b'])?
    A) 1
    B) ‘b’
    C) 2
    D) KeyError

  5. Which method would you use to add a new key-value pair to an existing dictionary?
    A) append()
    B) add()
    C) update()
    D) insert()

  6. If you want to remove a key-value pair from a dictionary, which method would you typically use?
    A) delete()
    B) pop()
    C) remove()
    D) discard()

  7. What will happen if you try to access a key that does not exist in a dictionary?
    A) It returns None
    B) It creates a new key with a default value
    C) It raises a KeyError
    D) It returns an empty string

  8. Which of the following methods can be used to retrieve all keys in a dictionary?
    A) keys()
    B) get_keys()
    C) list_keys()
    D) all_keys()

  9. How can you check if a specific key exists in a Python dictionary?
    A) Using the in operator
    B) Using the exists() method
    C) Using the check() method
    D) Using the includes() method

  10. In Python dictionaries, what is the time complexity for accessing a value by key?
    A) O(n)
    B) O(log n)
    C) O(1)
    D) O(n^2)

Answers:

  1. C
  2. B
  3. B
  4. C
  5. C
  6. B
  7. C
  8. A
  9. A
  10. C

 

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home