Courseiva

200-901 Software Development and Design Practice Question

Which Python exception would be raised by the following code?

my_dict = {'a': 1} value = my_dict['b']

⚠ Common exam trap

Cisco often tests the distinction between KeyError and IndexError, trapping candidates who confuse dictionary key access with list index access, especially when the code uses square brackets in both contexts.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

KeyError

Accessing a dictionary key that does not exist raises a KeyError in Python. In the code, my_dict['b'] attempts to retrieve the value for key 'b', which is not present in the dictionary {'a': 1}, so Python raises KeyError.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    KeyError

    Why this is correct

    Accessing a missing key in a dictionary raises KeyError, because Python's dict lookup requires the key to exist. The code requests 'b' from a dict containing only 'a', so the lookup fails immediately and KeyError propagates.

  • ✗

    ValueError

    Why it's wrong here

    A missing dictionary key raises KeyError, not ValueError. ValueError arises when a built-in operation receives an argument of the right type but an invalid value, such as int('abc'), so it would fit a failed type conversion rather than a failed lookup.

  • ✗

    IndexError

    Why it's wrong here

    IndexError arises from sequence types such as lists or tuples when a positional subscript exceeds their bounds; dictionaries are mappings keyed by hash, so a missing key raises KeyError instead. It tempts because both are lookup failures on subscripting, and IndexError would be correct for `[1, 2][5]`.

  • ✗

    AttributeError

    Why it's wrong here

    AttributeError occurs when an attribute or method does not exist on an object, for example calling 'abc'.append(). Subscripting a dictionary with a missing key raises KeyError, so AttributeError would apply to a misspelled method name, not a lookup.

About these practice questions

One of 975 original 200-901 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 200-901 practice question is part of Courseiva's free Cisco certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 200-901 exam.