Python Interview questions - dictionaries
(Python version 3.9 or later version)
Which of the following can be used as a dictionary key?
set
list
complex
dict
Which of the following about dictionary keys is NOT correct?
All keys in a dictionary must be unique
Some built-in methods can be used as keys
Strings of any length can be used as keys
None cannot be used as a key
Which of the following will NOT create an empty dictionary?
dict1 = { }
dict1 = dict ( )
dict1 = {'': ''}
dict1 = dict.__new__(dict, (), {})
What would be the result of executing the following code?
f={'Fr':33,'Br':55,'UK':44}
sorted(f)
['Br', 55, 'Fr', 33, 'UK', 44]
['Fr', 33, 'UK', 44, 'Br', 55]
[33, 44, 55]
['Br', 'Fr', 'UK']
Two dictionaries, d1 and d2, were created as shown below:
d1 = {'p':1, 'q':2}
d2 = {'q':3, 'r':4}
Which of the following code snippet(s) would merge d1 and d2 into a new dictionary, d3, that will have {'q': 2, 'r': 4, 'p': 1} as its data
d3 = d2 | d1
d3 = {**d2, **d1}
d3 = d2.copy(); d3.update(d1)
All of the above
Which line of code should replace # Replace this line in the following code snippet to get the result:
P-y-t-h-o-n!
w = 'Python'
d = {'sep':'-', 'end':'!'}
# Replace this line
print(w, d)
print(*w, *d)
print(*w, **d)
print(**w, **d)
What would be the output of executing the following code?
d, e, f = {},dict(),dict(())
d['a'] = 1.0; 
d['b'] = 2.0; 
d['c'] = 3.0
e = dict([('b',2/1), 
	 ('c',3/1),
	 ('a', 1/1)])
f = dict(zip(['c','a','b'], 
	      [3, 1, 2]))
print(d==e and e==f and d==f)
True
False
Type Error
Syntax Error
Which of the following dictionary methods can extract key:value pairs from a dictionary?
pairs()
items()
values()
data()
Which of the following is the difference between a dictionary (dict) and a list?
list is a sequence of objects while dict is an unordered key:value pair(s)
list is immutable while dict is mutable
list cannot have duplicate members while dict can
list cannot have functions as members while dict can
Which line of code should replace # Replace this line in the following code snippet to get 3 as the output to show the count of key:value pairs in dictionary t
t={'Fr':33,'Br':55,'UK':44}
# Replace this line
items(t)
pairs(t)
count(t)
len(t)
Python Interview Questions
  • Python - Dictionary
    1. Consider the following dictionary:{ "The Bodyguard":"1992", "Saturday Night Fever":"1977"} select the values?
    A) "1997"
    B) "1992"
    C) "The Bodyguard"

    Answer: "1997" & "1992"
    2.The variable release_year_dict is a Python Dictionary, what is the result of applying the following method: release_year_dict.values()
    A) retrieves the keys of the dictionary
    B) retrieves the values of the dictionary

    Answer: retrieves the keys of the dictionary
    3. What is wrong with the following dictionary: {'a':1, 'a':2}
    A) it has duplicate entries for the keys
    B) it has different entries for the values

    Answer: it has duplicate entries for the keys
    4. What python object do you cast to a dataframe?
    A) Tuple
    B) Set
    C) Dictionary
    D) Lists

    Answer: Dictionary
  • gk