Python Dictionary MCQs with answers Page - 1

You will find multiple-choice questions (MCQs) related to #Python Dictionary here. Go through these questions to prepare effectively for your upcoming exams and interviews.

To view the correct answer for any question, simply click the "Show Answer" button.

Have a question to share? Click on "Add Question" to contribute!

A

Admin • 802.91K Points
Coach

Q. Which method is used to return a view object of dictionary values?

  • (A) get()
  • (B) values()
  • (C) items()
  • (D) keys()

A

Admin • 802.91K Points
Coach

Q. What happens when two keys in a dictionary are the same?

  • (A) An error occurs
  • (B) The first key is retained
  • (C) The last key overwrites the previous one
  • (D) Both keys are stored

A

Admin • 802.91K Points
Coach

Q. What will `dict.fromkeys(['a', 'b'], 0)` return?

  • (A) {'a': 0, 'b': 0}
  • (B) {'a': None, 'b': None}
  • (C) {0: 'a', 0: 'b'}
  • (D) ['a', 'b']: 0

A

Admin • 802.91K Points
Coach

Q. Which keyword is used to check if a key exists in a dictionary?

  • (A) exists
  • (B) has
  • (C) in
  • (D) contains

A

Admin • 802.91K Points
Coach

Q. What does `dict.setdefault('key', default)` do?

  • (A) Sets a new key only if it exists
  • (B) Returns the value if key exists; otherwise sets it to default
  • (C) Raises an error if key doesn’t exist
  • (D) Deletes the key

A

Admin • 802.91K Points
Coach

Q. Can a dictionary have a key of any data type?

  • (A) Yes, any data type
  • (B) Only strings
  • (C) Only integers and strings
  • (D) Only immutable types like strings, numbers, tuples

A

Admin • 802.91K Points
Coach

Q. Which method returns the value for the given key, or None if not found?

  • (A) get()
  • (B) find()
  • (C) lookup()
  • (D) select()

A

Admin • 802.91K Points
Coach

Q. What does `popitem()` do in a dictionary?

  • (A) Removes all items
  • (B) Removes an arbitrary key-value pair
  • (C) Removes the first item
  • (D) Removes the last inserted key-value pair

A

Admin • 802.91K Points
Coach

Q. What is the time complexity of accessing a value from a dictionary?

  • (A) O(n)
  • (B) O(log n)
  • (C) O(1)
  • (D) O(n log n)

A

Admin • 802.91K Points
Coach

Q. Which is a correct way to merge two dictionaries `d1` and `d2` in Python 3.9+?

  • (A) d1 = d1 + d2
  • (B) d1.merge(d2)
  • (C) d1 |= d2
  • (D) merge(d1, d2)
What's Tag

As you may know, questions are organized under broad categories. Each category can include various types of questions. For example, the "History" category might contain questions about the Revolt of 1857, Shivaji Maharaj, Ancient History, Buddhism, and more.

To further refine this organization, we've introduced tags, which act as sub-categories to group questions more specifically.

Verified users can add tags to any question. If you have any suggestions regarding this system, we'd love to hear from you. Contact Us

Learn More