If b is a dictionary, what does any(b) do?
1. Returns True if any key of the dictionary is true
2. Returns False if dictionary is empty
3.Returns True if all keys of the dictionary are true
4.Method any() doesn’t exist for dictionary
What will be the output of the following Python code snippet? >>>import collections >>> a=collections.Counter([1,1,2,3,3,4,4,4]) >>> a
1.{1,2,3,4}
2.Counter({4, 1, 3, 2})
3.Counter({4: 3, 1: 2, 3: 2, 2: 1})
4. {4: 3, 1: 2, 3: 2, 2: 1}
What will be the output of the following Python code snippet? a={} a['a']=1 a['b']=[2,3,4] print(a)
1.Exception is thrown
2. {‘b’: [2], ‘a’: 1}
3.{‘b’: [2], ‘a’: [3]}
4.{‘b’: [2, 3, 4], ‘a’: 1}
What will be the output of the following Python code? >>> a={1:"A",2:"B",3:"C"} >>> a.items()
1.Syntax error
2.dict_items([(‘A’), (‘B’), (‘C’)])
3.dict_items([(1,2,3)])
4.dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
What will be the output of the following Python code? >>> a={} >>> a.fromkeys([1,2,3],"check")
1.Syntax error
2.{1:â€checkâ€,2:â€checkâ€,3:â€checkâ€}
3. “checkâ€
4. {1:None,2:None,3:None}
What will be the output of the following Python code? >>> b={} >>> all(b)
1.{ }
2.False
3.True
4. An exception is thrown
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)
1.Error, copy() method doesn’t exist for dictionaries
2.{1: ‘A’, 2: ‘B’, 3: ‘C’}
3. {1: ‘A’, 2: ‘D’, 3: ‘C’}
4.“None†is printed
What will be the output of the following Python function? any([2>8, 4>2, 1>2])
1.Error
2. True
3. False
4. 4>2
Which of the following is not a declaration of the dictionary?
1.{1: ‘A’, 2: ‘B’}
2. dict([[1,â€Aâ€],[2,â€Bâ€]])
3.{1,â€Aâ€,2â€Bâ€}
4.{ }
If a is a dictionary with some key-value pairs, what does a.popitem() do?
1.Removes an arbitrary element
2. Removes all the key-value pairs
3.Removes the key-value pair for the key given as an argument
4. Invalid method for dictionary
Suppose d = {“johnâ€:40, “peterâ€:45}, what happens when we try to retrieve a value using the expression d[“susanâ€]?
1. Since “susan†is not a value in the set, Python raises a KeyError exception
2.It is executed fine and no exception is raised, and it returns None
3. Since “susan†is not a key in the set, Python raises a KeyError exception
4.Since “susan†is not a key in the set, Python raises a syntax error
What will be the output of the following Python code snippet? >>> a={1:"A",2:"B",3:"C"} >>> del a
1.method del doesn’t exist for the dictionary
2. del deletes the values in the dictionary
3.del deletes the entire dictionary
4. del deletes the keys in the dictionary
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.Counter({3: 3, 2: 2, 4: 2})
2. Counter({2: 2, 3: 1, 4: 1})
3.Counter({3: 2})
4.Counter({4: 1})
What will be the output of the following Python code snippet? >>> import collections >>> a=collections.Counter([3,3,4,5]) >>> b=collections.Counter([3,4,4,5,5,5]) >>> a&b
1.Counter({3: 12, 4: 1, 5: 1})
2. Counter({3: 1, 4: 1, 5: 1})
3.Counter({4: 2})
4. Counter({5: 1})
What will be the output of the following Python code snippet? >>>import collections >>> b=collections.Counter([2,2,3,4,4,4]) >>> b.most_common(1)
1.Counter({4: 3, 2: 2, 3: 1})
2.{3:1}
3.{4:3}
4. [(4, 3)]
What will be the output of the following Python code snippet? a = {} a[1] = 1 a['1'] = 2 a[1.0]=4 count = 0 for i in a: count += a[i] print(count)
1.An exception is thrown
2. 3
3.6
4. 2
What will be the output of the following Python code snippet? a = {} a[1] = 1 a['1'] = 2 a[1]=a[1]+1 count = 0 for i in a: count += a[i] print(count)
1. 1
2.2
3. 4
4. Error, the keys can’t be a mixture of letters and numbers
What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} a.setdefault(4,"D") print(a)
1. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
2.None
3.Error
4.[1,3,6,10]
What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.get(1,4))
1. 1
2.A
3.4
4.Invalid syntax for get method
What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.get(5,4))
1.Error, invalid syntax
2. A
3.5
4.4
What will be the output of the following Python code snippet? a={1:"A",2:"B",3:"C"} print(a.setdefault(3))
1.{1: ‘A’, 2: ‘B’, 3: ‘C’}
2.C
3.{1: 3, 2: 3, 3: 3}
4.No method called setdefault() exists for dictionary
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.Error, dictionary in a dictionary can’t exist
2. ‘Numbers’: {1: 56, 3: 7}
3.{‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
4.{‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
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))
1.0
2.2
3. Error as the key-value pair of 1:’A’ is already deleted
4.1
What will be the output of the following Python code snippet? test = {1:'A', 2:'B', 3:'C'} test = {} print(len(test))
1. 0
2.None
3. 3
4.An exception is thrown
What will be the output of the following Python code snippet? total={} def insert(items): if items in total: total[items] += 1 else: total[items] = 1 insert('Apple') insert('Ball') insert('Apple') print (len(total))
1. 3
2. 1
3. 2
4. 0
What will be the output of the following Python code? >>> a=dict() >>> a[1]
1.An exception is thrown since the dictionary is empty
2. ‘ ‘
3. 1
4. 0
What will be the output of the following Python code? >>> a={'B':5,'A':9,'C':7} >>> sorted(a)
1. [‘A’,’B’,’C’]
2.[‘B’,’C’,’A’]
3.[5,7,9]
4.[9,5,7]
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’: 1, ‘b’: 2, ‘c’: 3}
2.An exception is thrown
3.{‘a’: ‘b’: ‘c’: }
4. {1: ‘a’, 2: ‘b’, 3: ‘c’}
What will be the output of the following Python code? >>> a={i: 'A' + str(i) for i in range(5)} >>> a
1. An exception is thrown
2.{0: ‘A0’, 1: ‘A1’, 2: ‘A2’, 3: ‘A3’, 4: ‘A4’}
3.{0: ‘A’, 1: ‘A’, 2: ‘A’, 3: ‘A’, 4: ‘A’}
4.{0: ‘0’, 1: ‘1’, 2: ‘2’, 3: ‘3’, 4: ‘4’}
What will be the output of the following Python code? >>> a={i: i*i for i in range(6)} >>> a
1.Dictionary comprehension doesn’t exist
2. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
3.{0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
4.{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
What will be the output of the following Python code? >>> import collections >>> a=collections.OrderedDict((str(x),x) for x in range(3)) >>> a
1.{‘2’:2, ‘0’:0, ‘1’:1}
2.OrderedDict([(‘0’, 0), (‘1’, 1), (‘2’, 2)])
3.An exception is thrown
4. ‘ ‘
What will be the output of the following Python code? >>> import collections >>> a=dict() >>> a=collections.defaultdict(int) >>> a[1]
1.1
2.0
3.An exception is thrown
4. ‘ ‘
What will be the output of the following Python code? >>> import collections >>> a=dict() >>> a=collections.defaultdict(str) >>> a['A']
1.An exception is thrown since the dictionary is empty
2.‘ ‘
3. ‘A’
4.0
What will be the output of the following Python code? >>> import collections >>> b=dict() >>> b=collections.defaultdict(lambda: 7) >>> b[4]
1.4
2.0
3.An exception is thrown
4. 7
What will be the output of the following Python code? >>> import collections >>> b=dict() >>> b=collections.defaultdict(lambda: 7) >>> b[4]
1.4
2.0
3.An exception is thrown
4. 7
What will be the output of the following Python code? a={1:"A",2:"B",3:"C"} a.clear() print(a)
1.None
2.{ None:None, None:None, None:None}
3. {1:None, 2:None, 3:None}
4.{ }
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)
1.{1: ‘A’, 2: ‘B’, 3: ‘C’}
2.Method update() doesn’t exist for dictionaries
3. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
4. {4: ‘D’, 5: ‘E’}
What will be the output of the following Python code? a={1:"A",2:"B",3:"C"} for i in a: print(i,end=" ")
1.1 2 3
2. ‘A’ ‘B’ ‘C’
3. 1 ‘A’ 2 ‘B’ 3 ‘C’
4.Error, it should be: for i in a.items():
What will be the output of the following Python code? a={1:5,2:3,3:4} a.pop(3) print(a)
1.{1: 5}
2.{1: 5, 2: 3}
3.Error, syntax error for pop() method
4.{1: 5, 3: 4}
What will be the output of the following Python code? a={1:5,2:3,3:4} print(a.pop(4,9))
1.9
2.3
3.Too many arguments for pop() method
4.4
What will be the output of the following Python code? a={} a[2]=1 a[1]=[2,3,4] print(a[1][1])
1.[2,3,4]
2.3
3. 2
4.An exception is thrown
What will be the output of the following Python code? count={} count[(1,2,4)] = 5 count[(4,2,1)] = 7 count[(1,2)] = 6 count[(4,2,1)] = 2 tot = 0 for i in count: tot=tot+count[i] print(len(count)+tot)
1.25
2.17
3.16
4. Tuples can’t be made keys of a dictionary
What will be the output of the following Python expression? round(4.5676,2)?
1. 4.5
2.4.6
3. 4.57
4.4.56
What will be the output of the following Python function? all(3,0,4.2)
1. True
2.False
3.Error
4. 0
What will be the output of the following Python function? all([2,4,0,6])
1. Error
2.True
3. False
4.0
What will be the output of the following Python function? import math abs(math.sqrt(25))
1. Error
2. -5
3.5
4.5.0
What will be the output of the following Python function? sum(2,4,6) sum([1,2,3])
1.Error, 6
2.12, Error
3.12, 6
4.Error, Error
Which of the following isn’t true about dictionary keys?
1. More than one key isn’t allowed
2.Keys must be immutable
3.Keys must be integers
4.When duplicate keys encountered, the last assignment wins
Which of the statements about dictionary values if false?
1. More than one key can have the same value
2.The values of the dictionary can be accessed as dict[key]
3.Values of a dictionary must be unique
4.Values of a dictionary can be a mixture of letters and numbers
Which of these about a dictionary is false?
1.The values of a dictionary can be accessed using keys
2.The keys of a dictionary can be accessed using values
3.Dictionaries aren’t ordered
4.Dictionaries are mutable