Preview

11 - Dictionary

 1. An empty dictionary without any items is written with just two curly braces, like this: {}. mydictionary={}
Note: The following presentation on dictionaries can be found on www.teachyourselfpython.com (assume we are referring to the Python language for this test)

  TRUE

  FALSE

 2. The following shows the creation of a simple dictionary. What is the output when run?
#creating a dictionary of key value pairs that stores the names of the upcoming iphones and their release date

#NOT SORTED  - note they are not necessarily printed/displayed in the order you create them! 
future_release = {
		"iphone10" : 2017,
		"iphone 11" : 2018,
		"iphone 12" : 2019,
		"iphone 13" : 2020,
		"iphone 14" : 2021,
		"iphone 15" : 2022
	}

print(future_release.keys())

  A list of all the phones and the years together.

  A list of all the keys (e.g. 01, 02, 03, etc)

  A list of all the years (e.g. 2017, 2018 etc)

  A list of the phone versions (e.g. Iphone10, Iphone11 etc)

 3. In this Python code that has created a specific dictionary, what is happening on line 7?
def main():
    #create a dictionary to store teachers and the subject they teach
    teacher_details={"Name": "Mr Moose","Subject": "Philosophy","Hobby":"Chess","School House":"Red House"}
    print(teacher_details)
    print()
    
    teacher_details["Salary"]="50K"
    print(teacher_details)
          
main()

  We have added another value to the dictionary

  We are adding another key and associated value

  We have added another key to the dictionary

  We have added another index number with a value to the dictionary

 4. In the following code the keys are:
d = {"Ruth":30, "Jonathan":35}

  printing out all the keys and the index numbers

  printing out all the keys (e.g. Name, Subject)

  printing out all the values (e.g. Mr Moose, Philosophy)

  printing out all the keys and values

 5. For the following code, what would the output be?
d = {"Ruth":40, "Jonathan":45}
"Ruth" in d

  TRUE

  FALSE

 6. What is the output of the following code?
d = {"john":40, "peter":45}
d["john"]

  45

  40

  "peter"

  "john"

 7. In Python: Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use a) d.delete(“john”:40)

   d.delete(“john”:40)

  del d(“john”:40)

  del john from d

  del d[“john”].

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

  size of d()

  d.size()

  len(d)

  d.len{}

 9. Suppose d = {“john”:40, “peter”:45}, on trying to retrieve a value using the expression d[“susan”], there is a keyError exception.

  FALSE

  TRUE

 10. What is the output of the following code that is making use of the dictionary structure?
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)

  14

  16

  17

  12

 11. In the following example, what is the output?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])

  1,2

  3

  1

  2,3,4

 12. What is the result of this code?
>>> a={'B':5,'A':9,'C':7}
>>> sorted(a)

  [‘B’,’C’,’A’].

   [9,5,7].

  [5,7,9].

  [‘A’,’B’,’C’].

 13. What is the result of the following code snippet?
>>> a={i: i*i for i in range(6)}
>>> a  (note: dictionary comprehension is used in this example)

  {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}

  {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}

  None of these answers are valid

   {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

 14. Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.
>>> b={}
>>> all(b)

  TRUE

  FALSE

 15. This code snippet has the result of inverting the key-value pairs in the dictionary
>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b

  FALSE

  TRUE