112 solutions available
Question 1Why are dictionaries called mutable types?
Question 2What are different ways of creating dictionaries?
Question 3What values can we have in a dictionary?
Question 4How are individual elements of dictionaries accessed?
Question 5How is indexing of a dictionary different from that of a list or a string?
Question 6Which of the following types qualify to be the keys of a dictionary?StringtupleIntegerfloatlistdictionary
Question 7Can you change an element of a sequence or collection? What if a collection is a dictionary? What if a sequence is a string?
Question 8What do you understand by ordered collection and unordered collection ? Give examples.
Question 9How do you add key:value pairs to an existing dictionary?
Question 10Can you remove key:value pairs from a dictionary and if so, how?
Question 1The keys of a dictionary must be of immutable types.
Question 2The order of a dictionary's elements is undefined or unordered.
Question 3To delete an element using a key, del statement or pop function is used.
Question 4To get all the keys of a dictionary, keys() method is used.
Question 5To create a new dictionary from a set of keys, fromkeys() function is used.
Question 6The copy( ) method creates a shallow copy of a dictionary.
Question 7The del statement will raise an error if the given key is not found in the dictionary.
Question 8The pop() function allows to specify own value/message, if the given key is not found in the dictionary.
Question 9The popitem( ) function will always remove the last entered value of a dictionary.
Question 10For sum() function to work, the keys of a dictionary must be addition compatible.
Question 1Dictionaries are ............... set of elements.sortedorderedunorderedrandom
Question 2Dictionaries are also called ...............mappingshashesassociative arraysall of these
Question 3Dictionaries are ............. data types of Python.mutableimmutablesimpleall of these
Question 4Which of the following functions will return the key, value pairs of a dictionary ?keys( )values( )items( )all of these
Question 5Which of the following will add a key to the dictionary only if it does not already exist in the dictionary ?fromkeys( )update(...
Question 6Which of the following will create a dictionary with given keys and a common value ?fromkeys( )update( )setdefault( )all of these
Question 7Which value is assigned to keys, if no value is specified with the fromkeys() method ?01Noneany of these
Question 8Which of the following can be used to delete item(s) from a dictionary?del statementpop( )popitem( )all of these
Question 9Which of the following will raise an error if the given key is not found in the dictionary ?del statementpop( )popitem()all of these
Question 10Which of the following will raise an error if the given dictionary is empty ?del statementpop( )popitem( )all of these
Question 11A copy of the dictionary where only the copy of the keys is created for the new dictionary, is called ............... copy.key copyshallow...
Question 12A copy of the dictionary where the copy of the keys as well as the values is created for the new dictionary, is called ..................
Question 13Which of the following is correct with respect to above Python code ?d = {"a" : 3,"b" : 7}a dictionary d is created.a and b are the keys...
Question 14What would the following code print ?d = {'spring':'autumn','autumn':'fall','fall':'spring'} print(d['autumn'])autumnfallspringError
Question 15What is printed by the following statements ?D1 = {"cat":12,"dog":6,"elephant":23,"bear":20} print("dog" in D1)TrueFalseErrorNone
Question 16What is printed by the following statements ?D1 = {"cat":12,"dog":6,"elephant":23,"bear":20} print(25 in D1)TrueFalseErrorNone
Question 17What will be the result of the following code ?d1 = {"abc":5,"def":6,"ghi":7} print(d1[0])abc5{"abc":5}Error
Question 18What will the following code do?d = {"Phy":94, "Che":70, "Bio":82, "Eng":95} d.update({"Che":72, "Bio":80})It will create new dictionary...
Question 19What will be the result of the following code?d = {"Jo":1,"Ra":2} d.update({"Phoebe":2})...
Question 20Which of the following will delete key_value pair for key="tiger" in dictionary?di = {"lion":"wild","tiger":"wild","cat": "domestic",...
Question 21Which of the following will give error if d1 is as shown below?d1 = {"a":1, "b":2, "c":3}print(len(d1))print(d1.get("b"))d1["a"] = 5None...
Question 22Which of the following Python codes will give the same output ifdict = {"diary":1, "book":3, "novel":5}(i) dict.pop("book")(ii) del...
Question 23What will be the output of following Python code?d1 = {"a":10,"b":2,"c":3} str1="" for i in d1: str1 = str1 + str(d1[i]) + " "...
Question 24Running the code sorted(my_dictionary, reverse = True) on a dictionary named my_dictionary will return results sorted in what...
Question 1In Python, a dictionary can have two same keys with different values.False
Question 2In Python, a dictionary can have two same values with different keys.True
Question 3Dictionaries are unordered set of elements.True
Question 4A dictionary can have duplicate keys.False
Question 5In Python, a dictionary can have two same keys or same values but cannot have two same key-value pair.False
Question 6In Python, a dictionary can neither have two same keys nor two same values.False
Question 7Values of a dictionary can be string, integers or combination of both.True
Question 8Keys of a dictionary can be string, integers or combination of both.True
Question 9The value of a dictionary can be accessed with the help of indices.False
Question 10A dictionary is immutable.False
Question 11The del statement raises error if the given key is not found in the dictionary.True
Question 12The popitem( ) removes the last entered element from a dictionary.True
Question 13The sum( ) can work with all types of dictionaries.False
Question 14The sorted( ) function cannot work with dictionaries.False
Question 15The copy( ) method of dictionaries creates a shallow copy of a dictionary.True
Question 16The fromkeys( ) creates a dictionary with given keys, each having a different value.False
Question 1Why is a dictionary termed as an unordered collection of object?
Question 2What type of objects can be used as keys in dictionaries ?
Question 3Though tuples are immutable type, yet they cannot always be used as keys in a dictionary. What is the condition to use tuples as a key in a...
Question 4What all types of values can you store in :dictionary-values ?dictionary-keys ?
Question 5Can you change the order of dictionary's contents, i.e., can you sort the contents of a dictionary ?
Question 6In no more than one sentence, explain the following Python error and how it could arise:TypeError: unhashable type: 'list'
Question 7Can you check for a value inside a dictionary using in operator? How will you check for a value inside a dictionary using in operator ?
Question 8Dictionary is a mutable type, which means you can modify its contents ? What all is modifiable in a dictionary ? Can you modify the keys of...
Question 9How is del D and del D[<key>] different from one another if D is a dictionary ?
Question 10How is clear( ) function different from del <dict> statement ?
Question 11What does fromkeys( ) method do?
Question 12How is pop( ) different from popitem( ) ?
Question 13If sorted( ) is applied on a dictionary, what does it return ?
Question 14Will max( ) and min( ) always work for a dictionary ?
Question 15Can you use sum( ) for calculating the sum of the values of a dictionary ?
Question 16What do you understand by shallow copy of a dictionary ?
Question 17What is the use of copy( ) function ?
Question 18Discuss the working of copy( ) if(i) the values are of immutable types,(ii) the values are of mutable types.
Question 1Which of the following will result in an error for a given valid dictionary D?D + 3D * 3D + {3 : "3"}D.update( {3 : "3"})D.update { {"3" :...
Question 2The following code is giving some error. Find out the error and correct it.d1 = {"a" : 1, 1 : "a", [1, "a"] : "two"}
Question 3The following code has two dictionaries with tuples as keys. While one of these dictionaries being successfully created, the other is...
Question 4Nesting of dictionary allows you to store a dictionary inside another dictionary. Then why is following code raising error ? What can you...
Question 5Why is following code not giving correct output even when 25 is a member of the dictionary?dic1 = {'age': 25, 'name': 'xyz', 'salary':...
Question 6What is the output produced by the following code :d1 = {5 : [6, 7, 8], "a" : (1, 2, 3)} print(d1.keys()) print(d1.values())
Question 7Consider the following code and then answer the questions that follow :myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30} valA ='' for i in...
Question 8What will be the output produced by following code ?d1 = { 5 : "number", "a" : "string", (1, 2): "tuple" } print("Dictionary contents")...
Question 9(a)Predict the output:d = dict() d['left'] = '<' d['right'] = '>' print('{left} and {right} or {right} and {left}')
Question 9(b)Predict the output:d = dict() d['left'] = '<' d['right'] = '>' d['end'] = ' ' print(d['left'] and d['right'] or d['right'] and...
Question 9(c)Predict the output:text = "abracadabraaabbccrr" counts = {} ct = 0 lst = [] for word in text: if word not in lst:...
Question 9(d)Predict the output:list1 = [2, 3, 3, 2, 5,3, 2, 5, 1,1] counts = {} ct = 0 lst = [] for num in list1: if num not in lst:...
Question 10Create a dictionary 'ODD' of odd numbers between 1 and 10, where the key is the decimal number and the value is the corresponding number...
Question 11(a)Find the errors:text = "abracadbra" counts = {} for word in text : counts[word] = counts[word] + 1
Question 11(b)my_dict = {} my_dict[(1,2,4)] = 8 my_dict[[4,2,1]] = 10 print(my_dict)
Question 12(a)Predict the output:fruit = {} L1 = ['Apple', 'banana', 'apple'] for index in L1 : if index in fruit: fruit[index] += 1 else :...
Question 12(b)Predict the output:arr = {} arr[1] = 1 arr['1'] = 2 arr[1] += 1 sum = 0 for k in arr: sum += arr[k] print(sum)
Question 13(a)Predict the outputa = {(1,2):1,(2,3):2} print(a[1,2])
Question 13(b)Predict the outputa = {'a':1, 'b':2, 'c':3} print(a['a','b'])
Question 14Find the error/output. Consider below given two sets of codes. Which one will produce an error? Also, predict the output produced by the...
Question 15Predict the output :dct = {} dct[1] = 1 dct ['1'] = 2 dct[1.0] = 4 sum = 0 for k in dct: print(k, sum) sum += dct[k] print(sum)
Question 16Fill in the blanks of the following code so that the values and keys of dictionary d are inverted to create dictionary fd.d = {'a':1,...
Question 1Write a program to enter names of employees and their salaries as input and store them in a dictionary.Solutiond = {} ans = "y" while ans...
Question 2Write a program to count the number of times a character appears in a given string.Solutionstr = input("Enter the string: ") ch =...
Question 3Write a program to convert a number entered by the user into its corresponding number in words. For example, if the input is 876 then the...
Question 4Repeatedly ask the user to enter a team name and how many games the team has won and how many they lost. Store this information in a...
Question 5Write a program that repeatedly asks the user to enter product names and prices. Store all of these in a dictionary whose keys are the...
Question 6Create a dictionary whose keys are month names and whose values are the number of days in the corresponding months.(a) Ask the user to...
Question 7Can you store the details of 10 students in a dictionary at the same time ? Details include - rollno, name, marks, grade etc. Give example...
Question 8Given the dictionary x = {'k1':'v1', 'k2':'v2', 'k3':'v3'}, create a dictionary with the opposite mapping, i.e., write a program to create...
Question 9Given two dictionaries say D1 and D2. Write a program that lists the overlapping keys of the two dictionaries, i.e., if a key of D1 is also...
Question 10Write a program that checks if two same values in a dictionary have different keys. That is, for dictionary D1 = { 'a' : 10, 'b': 20, 'c'...
Question 11Write a program to check if a dictionary is contained in another dictionary e.g., ifd1 = {1:11, 2:12}d2 = {1:11, 2:12, 3:13, 4:15}then d1...
Question 12A dictionary D1 has values in the form of lists of numbers. Write a program to create a new dictionary D2 having same keys as D1 but...