arrow
arrow
arrow
A dictionary is declared as D = {10: "A", 25: "B", 32: "C", 54: "D"} Which of the following is incorrect?
Question

A dictionary is declared as
D = {10: "A", 25: "B", 32: "C", 54: "D"}
Which of the following is incorrect?

A.

D[20] = "E"

B.

D[30] += 20

C.

D[32] += '*'

D.

D["X"] = 100

Correct option is B


Let's analyze each statement in relation to the dictionary D:
D = {10: "A", 25: "B", 32: "C", 54: "D"}
1. (a) D[20] = "E": This is correct. You can add new key-value pairs to the dictionary. The key 20 does not exist in the dictionary, so it will be added with the value "E".
· Updated Dictionary: {10: "A", 25: "B", 32: "C", 54: "D", 20: "E"}
2. (b) D[30] += 20: This is incorrect. The value associated with the key 30 doesn't exist in the dictionary, so attempting to increment it with += 20 will raise a KeyError. You can only perform operations like += on existing keys, where the value is a number (or string) that supports this operation.
3. (c) D[32] += '*': This is correct. The value of D[32] is "C", which is a string. Using += '*' will append the character '*' to the existing value, resulting in "C*".
· Updated Dictionary: {10: "A", 25: "B", 32: "C*", 54: "D"}
4. (d) D["X"] = 100: This is correct. You can add new keys to the dictionary. In this case, the key "X" (a string) will be added with the value 100.
· Updated Dictionary: {10: "A", 25: "B", 32: "C", 54: "D", "X": 100}
Important Key Points:
1. You can add new key-value pairs to a dictionary.
2. Operations like += can only be performed on keys that already exist and whose values support the operation.
3. Python dictionaries can have mixed types of keys (integers, strings, etc.).
Knowledge Booster:
· Option (a): This is correct because adding new key-value pairs to a dictionary is valid in Python.
· Option (c): This is correct because string concatenation using += is valid in Python.
· Option (d): This is correct because adding a new key-value pair with a string key is valid.

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘EMRS PGT’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
353k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow