Computer

Dictionary In Python MCQs

Practice Dictionary In Python MCQs for competitive exams.

Dictionary In Python MCQs

Practice questions from this topic.

What will be the output of the following Python code snippet? numbers = {} letters = {} comb = {} numbers[1] = 56 numbers[3] = 7 letters[4] = 'B' comb['Numbers'] = numbers comb['Letters'] = letters print(comb)

  1. A. Error, dictionary in a dictionary can't exist
  2. B. Numbers': {1: 56, 3: 7}
  3. C. {'Numbers': {1: 56}, 'Letters': {4: 'B'}}
  4. D. {'Numbers': {1: 56, 3: 7}, 'Letters': {4: 'B'}}
Report Error

What will be the output of the following Python code snippet? >>> import collections >>> a=collections.Counter([2,2,3,3,3,4]) >>> b=collections.Counter([2,2,3,4,4]) >>> a|b

  1. A. Counter({3: 3, 2: 2, 4: 2})
  2. B. Counter({2: 2, 3: 1, 4: 1})
  3. C. Counter({3: 2})
  4. D. Counter({4: 1})
Report Error

What will be the output of the following Python code snippet? d = {"john":40, "peter":45}

  1. A. "john", 40, 45, and "peter"
  2. B. "john" and "peter"
  3. C. 40 and 45
  4. D. d = (40:"john", 45:"peter")
Report Error

What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.get(1,4))

  1. A. 1
  2. B. A
  3. C. 4
  4. D. Invalid syntax for get method
Report Error

What will be the output of the following Python code? >>> b={} >>> all(b)

  1. A. { }
  2. B. False
  3. C. True
  4. D. An exception is thrown
Report Error

What will be the output of the following Python code? >>> a={i: 'A' + str(i) for i in range(5)} >>> a

  1. A. An exception is thrown
  2. B. {0: 'A0', 1: 'A1', 2: 'A2', 3: 'A3', 4: 'A4'}
  3. C. {0: 'A', 1: 'A', 2: 'A', 3: 'A', 4: 'A'}
  4. D. {0: '0', 1: '1', 2: '2', 3: '3', 4: '4'}
Report Error

Which of the following isn't true about dictionary keys?

  1. A. More than one key isn't allowed
  2. B. Keys must be immutable
  3. C. Keys must be integers
  4. D. When duplicate keys encountered, the last assignment wins
Report Error

What will be the output of the following Python code? >>> a={"a":1,"b":2,"c":3} >>> b=dict(zip(a.values(),a.keys())) >>> b

  1. A. {'a': 1, 'b': 2, 'c': 3}
  2. B. An exception is thrown
  3. C. {'a': 'b': 'c': }
  4. D. {1: 'a', 2: 'b', 3: 'c'}
Report Error

What will be the output of the following Python code? >>> import collections >>> b=dict() >>> b=collections.defaultdict(lambda: 7) >>> b[4]

  1. A. 4
  2. B. 0
  3. C. An exception is thrown
  4. D. 7
Report Error

What will be the output of the following Python code? >>> a={'B':5,'A':9,'C':7} >>> sorted(a)

  1. A. ['A','B','C']
  2. B. ['B','C','A']
  3. C. [5,7,9]
  4. D. [9,5,7]
Report Error

What will be the output of the following Python code snippet? a={} a['a']=1 a['b']=[2,3,4] print(a)

  1. A. Exception is thrown
  2. B. {'b': [2], 'a': 1}
  3. C. {'b': [2], 'a': [3]}
  4. D. {'b': [2, 3, 4], 'a': 1}
Report Error

Suppose d = {"john":40, "peter":45}, what happens when we try to retrieve a value using the expression d["susan"]?

  1. A. Since "susan" is not a value in the set, Python raises a KeyError exception
  2. B. It is executed fine and no exception is raised, and it returns None
  3. C. Since "susan" is not a key in the set, Python raises a KeyError exception
  4. D. Since "susan" is not a key in the set, Python raises a syntax error
Report Error