Thursday 7 January 2021

Python Dictionary Interview questions - Multiple choice

 Dictionary:

========================================================================

1.Which of the following statements create a dictionary?

a) d = {}

b) d = {“john”:40, “peter”:45}

c) d = {40:”john”, 45:”peter”}

d) All of the mentioned


Answer: d

Explanation: Dictionaries are created by specifying keys and values.


2. What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}

"john" in d

a) True

b) False

c) None

d) Error


Answer: a

Explanation: In can be used to check if the key is in the dictionary.


3. What will be the output of the following Python code snippet?

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

d1 == d2

a) True

b) False

c) None

d) Error


Answer: b

Explanation: If d2 was initialized as d2 = d1 the answer would be true.


4. What will be the output of the following Python code snippet?

d1 = {"john":40, "peter":45}

d2 = {"john":466, "peter":45}

d1 > d2

a) True

b) False

c) Error

d) None


Answer: c

Explanation: Arithmetic > operator cannot be used with dictionaries.


5. What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}

d["john"]

a) 40

b) 45

c) “john”

d) “peter”


Answer: a

Explanation: Execute in the shell to verify.


6. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?

a) d.delete(“john”:40)

b) d.delete(“john”)

c) del d[“john”]

d) del d(“john”:40)


Answer: c

Explanation: Execute in the shell to verify.


7. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in the dictionary which command do we use?

a) d.size()

b) len(d)

c) size(d)

d) d.len()


Answer: b

Explanation: Execute in the shell to verify.


8. What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}

print(list(d.keys()))

a) [“john”, “peter”]

b) [“john”:40, “peter”:45]

c) (“john”, “peter”)

d) (“john”:40, “peter”:45)


Answer: a

Explanation: The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.


9. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“rohith”]?

a) Since “rohith” is not a value in the set, Python raises a KeyError exception

b) It is executed fine and no exception is raised, and it returns None

c) Since “rohith” is not a key in the set, Python raises a KeyError exception

d) Since “rohith” is not a key in the set, Python raises a syntax error


Answer: c

Explanation: Execute in the shell to verify.


10. Which of these about a dictionary is false?

a) The values of a dictionary can be accessed using keys

b) The keys of a dictionary can be accessed using values

c) Dictionaries aren’t ordered

d) Dictionaries are mutable


Answer: b

Explanation: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.


11. Which of the following is not a declaration of the dictionary?

a) {1: ‘A’, 2: ‘B’}

b) dict([[1,”A”],[2,”B”]])

c) {1, "A", 2, "B"}

d) { }


Answer: c

Explanation: Option c is a set, not a dictionary.


12. What will be the output of the following Python code snippet?

a={1:"A", 2:"B", 3:"C"}

print(a.get(1,4))

a) 1

b) A

c) 4

d) Invalid syntax for get method.


Answer: b

Explanation: The get() method returns the value of the key if the key is present in 

the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.


13. What will be the output of the following Python code snippet?

a={1:"A", 2:"B", 3:"C"}

print(a.get(5,4))

a) Error, invalid syntax

b) A

c) 5

d) 4


Answer: d

Explanation: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.


14. What will be the output of the following Python code snippet?

a={1:"A", 2:"B", 3:"C"}

print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}

b) C

c) {1: 3, 2: 3, 3: 3}

d) No method called setdefault() exists for dictionary


Answer: b

Explanation: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.


15. What will be the output of the following Python code snippet?

a={1:"A",2:"B",3:"C"}

a.setdefault(4,"D")

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}

b) None

c) Error

d) [1,3,6,10]


Answer: a

Explanation: setdefault() will set dict[key]=default if key is not already in the dictionary.


16. What will be the output of the following Python code?

a={1:"A", 2:"B", 3:"C"}

b={4:"D", 5:"E"}

a.update(b)

print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}

b) Method update() doesn’t exist for dictionaries

c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}

d) {4: ‘D’, 5: ‘E’}


Answer: c

Explanation: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.


17. What will be the output of the following Python code?

a={1:"A", 2:"B", 3:"C"}

b=a.copy()

b[2]="D"

print(a)

a) Error, copy() method doesn’t exist for dictionaries

b) {1: ‘A’, 2: ‘B’, 3: ‘C’}

c) {1: ‘A’, 2: ‘D’, 3: ‘C’}

d) “None” is printed


Answer: b

Explanation: .copy performs deep copy (creates separate memory location)Changes made in the 2nd dictionary aren’t reflected in the original one.


18. What will be the output of the following Python code?

a={1:"A", 2:"B", 3:"C"}

a.clear()

print(a)

a) None

b) { None:None, None:None, None:None}

c) {1:None, 2:None, 3:None}

d) { }


Answer: d

Explanation: The clear() method clears all the key-value pairs in the dictionary.


19. Which of the following isn’t true about dictionary keys?

a) When duplicate keys are encountered, the last assignment wins

b) Keys must be immutable

c) Keys must be integers

d) None


Answer: c

Explanation: Keys of a dictionary may be any data type that is immutable.


20. What will be the output of the following Python code?

a={1:5, 2:3, 3:4}

a.pop(2)

print(a)

a) {1: 5}

b) {1: 5, 2: 3}

c) Error, syntax error for pop() method

d) {1: 5, 3: 4}


Answer: d

Explanation: pop() method removes the key-value pair for the key mentioned in the pop() method.


21. What will be the output of the following Python code?

>>> a={1:"A", 2:"B", 3:"C"}

>>> a.items()

a) Syntax error

b) dict_items([(‘A’), (‘B’), (‘C’)])

c) dict_items([(1,2,3)])

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])


Answer: d

Explanation: The method items() returns the list of tuples with each tuple having a key-value pair.


22. Which of the statements about dictionary values is false?

a) More than one key can have the same value

b) The values of the dictionary can be accessed as dict[key]

c) Values of a dictionary must be unique

d) Values of a dictionary can be a mixture of letters and numbers


Answer: c

Explanation: More than one key can have the same value.


23. What will be the output of the following Python code snippet?

>>> a={1:"A", 2:"B", 3:"C"}

>>> del a

a) method del doesn’t exist for the dictionary

b) del deletes the values in the dictionary

c) del deletes the entire dictionary

d) del deletes the keys in the dictionary


Answer: c

Explanation: del deletes the entire dictionary and any further attempt to access it will throw an error.


24. If a is a dictionary with some key-value pairs, what does a.popitem() do?

a) Removes an arbitrary element

b) Removes all the key-value pairs

c) Removes the key-value pair for the key given as an argument

d) Invalid method for dictionary


Answer: a

Explanation: The method pop item() removes a random key-value pair.


25. What will be the output of the following Python code snippet?

numbers = letters = narendra= {}

numbers[1] = 56

numbers[3] = 7

letters[4] = 'B'

narendra['Numbers'] = numbers

narendra['Letters'] = letters

print(narendra)

a) Error, dictionary in a dictionary can’t exist

b) ‘Numbers’: {1: 56, 3: 7}

c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}

d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}


Answer: d

Explanation: Dictionary in a dictionary (nested dictionaries) can exist.


26. What will be the output of the following Python code snippet?

test = {1:'A', 2:'B', 3:'C'}

test = {}

print(len(test))

a) 0

b) None

c) 3

d) An exception is thrown


Answer: a

Explanation: In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.


27. What will be the output of the following Python code snippet?

test = {1:'A', 2:'B', 3:'C'}

del test[1]

test[1] = 'D'

del test[2]

print(len(test))

a) 0

b) 2

c) Error as the key-value pair of 1:’A’ is already deleted

d) 1


Answer: b

Explanation: After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.


28. What will be the output of the following Python code snippet?

a={}

a['a']=1

a['b']=[2,3,4]

print(a)

a) Exception is thrown

b) {‘b’: [2], ‘a’: 1}

c) {‘b’: [2], ‘a’: [3]}

d) {‘b’: [2, 3, 4], ‘a’: 1}


Answer: d

Explanation: Dictionary is unordered. Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.


29. What will be the output of the following Python code?

a = {}

a[2] = 1

a[1] = [2,3,4]   

print(a[1][1])

a) [2,3,4]

b) 3

c) 2

d) An exception is thrown


Answer: (b) 

Explanation: Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.


30. What will be the output of the following Python code?

a={'B':5, 'A':9, 'C':7}

sorted(a)

a) [‘A’,’B’,’C’]

b) [‘B’,’C’,’A’]

c) [5,7,9]

d) [9,5,7]


Answer: a

Explanation: Return a new sorted list of keys in the dictionary.


31. What will be the output of the following Python code?

>>> a={}

>>> a.fromkeys( [1, 2, 3], "check")

a) Syntax error

b) {1:”check”, 2:”check”, 3:”check”}

c) “check”

d) {1:None, 2:None, 3:None}


Answer: b

Explanation: The dictionary takes values of keys from the list and initializes them to the default value 

(value is given in the second parameter). Execute in Python shell to verify.


32. What will be the output of the following Python code?

b = {}

all(b)

a) { }

b) False

c) True

d) An exception is thrown


Answer: c

Explanation: Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.


33. If b is a dictionary, what does any(b) do?

a) Returns True if any key of the dictionary is true

b) Method any() doesn’t exist for dictionary

c) Returns True if all keys of the dictionary are true

d) None


Answer: a

Explanation: Method any() returns True if any key of the dictionary is true and False if the dictionary is empty.


34. What will be the output of the following Python code?

        a = {"a":1, "b":2, "c":3}

b = dict(zip(a.values(),a.keys()))

        print(b)

a) {‘a’: 1, ‘b’: 2, ‘c’: 3}

b) An exception is thrown

c) {‘a’: ‘b’: ‘c’: }

d) {1: ‘a’, 2: ‘b’, 3: ‘c’}


Answer: d

Explanation: The above piece of code inverts the key-value pairs in the dictionary.


35. What will be the output of the following Python code?

a = dict()

a[1]

a) An exception is thrown since the dictionary is empty

b) ‘ ‘

c) 1

d) 0


Answer: a

Explanation: The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.


36. what is the output of the following code snippet?

    dict1 = {'a': 10, 'b': 8}
    dict2 = {'d': 6, 'c': 4}
    print(dict(dict1, **dict2))
Answer: {'a': 10, 'b': 8, 'd': 6, 'c': 4}
========================================================

37. What will be the output of the following Python code?

print(list(zip((1,2,3),('a'),('xxx','yyy'))))

print(list(zip((2,4),('b','c'),('yy','xx'))))

a) [(1,2,3),(‘a’),(‘xxx’,’yyy’)]

[(2,4),(‘b’,’c’),(‘yy’,’xx’)]

b) [(1, 'a', 'xxx'),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)]

[(2, 'b', 'yy'), (4, 'c', 'xx')]

c) Syntax error

d) [(1, 'a', 'xxx')]

[(2, 'b', 'yy'), (4, 'c', 'xx')]


Answer: d

Explanation: The zip function combines the individual attributes of the lists into a list of tuples.

 

No comments:

Post a Comment