[go: up one dir, main page]

0% found this document useful (0 votes)
10 views6 pages

Dictionary Quizs

This document contains a quiz about dictionaries in Python with 29 multiple choice questions. The questions cover dictionary basics like creating, accessing, updating and deleting dictionaries as well as common dictionary methods like keys(), values(), items(), pop(), update() etc.

Uploaded by

same here
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Dictionary Quizs

This document contains a quiz about dictionaries in Python with 29 multiple choice questions. The questions cover dictionary basics like creating, accessing, updating and deleting dictionaries as well as common dictionary methods like keys(), values(), items(), pop(), update() etc.

Uploaded by

same here
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dictionaries Quiz

Question 1: What is the result of calling the `len()` function on an empty dictionary?

0 - Correct!

None

Error

Question 2: Which symbol is used to access values from a dictionary in Python?

>

[ ] - Correct!

Question 3: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['b'])

2 - Correct!

b - Incorrect! Correct answer: 2

KeyError: b - Incorrect! Correct answer: 2

Question 4: Which method is used to add a new key-value pair to a dictionary?

add() - Incorrect! Correct answer: update()

insert()

append()

update() - Correct!

Question 5: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict['d'] = 4
print(my_dict)

{'a': 1, 'b': 2, 'c': 3}

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

{'a': 1, 'b': 2, 'c': 3, 4}

Error

Question 6: Which of the following is not a valid key type for a dictionary in Python?
String - Incorrect! Correct answer: List

Integer - Incorrect! Correct answer: List

List - Correct!

Tuple

Question 7: What does the `pop()` method do in Python dictionaries?

Removes and returns the last element - Incorrect! Correct answer: Removes and returns the
specified key-value pair

Removes and returns the first element

Removes and returns the specified key-value pair - Correct!

Returns the index of the specified element

Question 8: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.pop('b'))

2 - Correct!

'b'

KeyError: 'b'

Question 9: Which method is used to remove a key-value pair from a dictionary without raising
an error if the key is not found?

remove() - Incorrect! Correct answer: pop()

delete()

discard()

pop() - Correct!

Question 10: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('d'))

KeyError

None - Correct!

0 - Incorrect! Correct answer: None

Error

Question 11: What method returns a list of all keys in a dictionary?

keys() - Correct!

get_keys()
dict_keys()

list_keys()

Question 12: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} print('b' in
my_dict)

True - Correct!

False

Error

None

Question 13: Which of the following is used to iterate over the items of a dictionary?

for key, value in my_dict.items():

for key in my_dict.keys():

for value in my_dict.values():

All of the above - Correct!

Question 14: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear() print(my_dict)

{} - Correct!

{'a': 1, 'b': 2, 'c': 3}

None

Error

Question 15: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))

3 - Correct!

Question 16: Which method is used to return a shallow copy of a dictionary?

copy() - Correct!

clone() - Incorrect! Correct answer: copy()

duplicate()

shallow() - Incorrect! Correct answer: copy()

Question 17: What is the correct way to create an empty dictionary in Python?
{} - Correct!

[] - Incorrect! Correct answer: {}

()

None

Question 18: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.update({'d': 4}) print(my_dict)

{'a': 1, 'b': 2, 'c': 3}

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

{'a': 1, 'b': 2, 'c': 3, 4}

Error

Question 19: What method is used to retrieve the value for a given key from a dictionary?

retrieve()

get() - Correct!

fetch()

read()

Question 20: What data structure is a dictionary in Python?

Sequential - Incorrect! Correct answer: Associative

Associative - Correct!

Ordered

Indexed

Question 21: Which of the following is not a valid key type for a dictionary in Python?

Integer

String

List - Correct!

Tuple

Question 22: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['d'])

KeyError: 'd'

None

'd'
Question 23: Which method is used to remove a key-value pair from a dictionary?

pop() - Correct!

delete()

remove()

discard()

Question 24: What will be the output of the following code? my_dict = {'apple': 2, 'banana': 3,
'orange': 4} del my_dict['banana'] print(my_dict)

{'apple': 2, 'banana': 3, 'orange': 4}

{'apple': 2, 'orange': 4}

{'banana': 3, 'orange': 4}

Error: del keyword cannot be used with dictionaries

Question 25: Which of the following methods returns a list of all the keys in a dictionary?

keys() - Correct!

get_keys()

dict_keys()

list_keys()

Question 26: What does the `update()` method do in Python dictionaries?

Adds a new key-value pair - Incorrect! Correct answer: Updates existing key-value pairs with
new values

Removes a key-value pair

Updates existing key-value pairs with new values - Correct!

Clears the dictionary

Question 27: What is the time complexity for accessing an element in a dictionary by its key?

O(1) - Correct!

O(log n)

O(n)

O(n log n)

Question 28: Which of the following functions creates a dictionary from two lists, one
containing keys and the other containing values?

create_dict()

dict_from_lists()
zip_dict()

dict() - Correct!

Question 29: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))

3 - Correct!

You might also like