0% found this document useful (0 votes)
12 views4 pages

Dictionary

Uploaded by

Jananicharlesraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Dictionary

Uploaded by

Jananicharlesraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DICTIONARY

1. 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

2. 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

3. 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”
4. 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)

5. 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)
6. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the
expression d[“susan”]?

a) Since “susan” 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 “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
7. 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

8. 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) { }

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

a={1:"A",2:"B",3:"C"}
for i,j in a.items():
print(i,j,end=" ")

a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”

10. 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

11. 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

12. 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’}

13. 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

14. 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) { }
15. What will be the output of the following Python code?

a={1:5,2:3,3:4}
a.pop(3)
print(a)

a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}

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

a={1:5,2:3,3:4}
a.pop(3)
print(a)

a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}

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

a={1:"A",2:"B",3:"C"}
for i in a:
print(i,end=" ")

a) 1 2 3
b) ‘A’ ‘B’ ‘C’
c) 1 ‘A’ 2 ‘B’ 3 ‘C’
d) Error, it should be: for i in a.items():

18. 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’)])

You might also like