Search Header Logo

Dictionary

Authored by Sonam Tenzin

Computers

8th Grade

Used 3+ times

Dictionary
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

10 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

What is a dictionary in Python?

A data structure that stores items in a sorted manner.

A collection of elements arranged in a linear sequence.

A collection of key-value pairs.

A way to store only numeric values.

Answer explanation

Media Image

A dictionary is a collection of keys and values. Dictionaries can not have duplicated items, meaning the keys are unique. A dictionary is defined using curly brackets {}. The key & value pair is defined using a colon (:) - key:value.

The syntax to define a dictionary:

dictionary_name = {key1:value1, key2:value2,...}

For example:

shopping_list = {"eggs":12 , "milk":2, "apples":5, "tomatoes": 6}

2.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you create an empty dictionary in Python?

my_dict = {}

my_dict = []

my_dict = ()

my_dict = None

3.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you access the value associated with a specific key in a dictionary?

By using the index of the value.

By using the value as an index.

By using the key as an index.

By using the value's position in the dictionary.

Answer explanation

Media Image

You access the value associated with a specific key in a dictionary by using square brackets and placing the key inside the brackets. This allows you to retrieve the value (or data) that corresponds to that particular key.

4.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

Can a dictionary in Python contain duplicate keys?

Yes, but only if the values are different.

No, dictionaries cannot have duplicate keys.

Yes, but it will cause an error when accessing values.

Yes, but only if the keys are numbers.

Answer explanation

Media Image

No, a dictionary in Python cannot contain duplicate keys. Each key in a dictionary must be unique. If you attempt to add a key that already exists in the dictionary, it will overwrite the existing key-value pair with the new value.

Remember that keys in a dictionary must be unique, and adding a key that already exists will simply update the associated value.

5.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you add a new key-value pair to an existing dictionary?

dictionary.add(key, value)

dictionary.insert(key, value)

dictionary[key] = value

dictionary.append(key, value)

Answer explanation

Media Image

You can add a new key-value pair to an existing dictionary by assigning a value to a new key using square brackets. If the key already exists, the assignment will update the value for that key; if it doesn't exist, a new key-value pair will be added to the dictionary.

6.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

What happens if you try to access a key that doesn't exist in a dictionary?

Python raises an error.

Python returns the value None.

Python returns an empty string.

Python returns a special "key not found" value.

Answer explanation

Media Image

You will encounter a KeyError exception. This is because dictionaries in Python use keys to look up and retrieve values, and if the specified key is not found in the dictionary, Python raises a KeyError to indicate that the key does not exist.

7.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you remove a key-value pair from a dictionary?

dictionary.remove(key)

del dictionary[key]

dictionary.pop(key)

dictionary.delete(key)

Answer explanation

Media Image

The pop() method removes the item with the specified key name:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)

The del keyword can also delete the dictionary completely.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict["model"]
print(thisdict)

The clear() method empties the dictionary:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.clear()
print(thisdict)

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?