WorksheetsPYTHON QUIZ2
Total questions: 10
Worksheet time: 8mins
1. Which of the following is correct with respect to the python code d = {"a":3,"b":7}?
Dictionary d is created.
a and b are the keys of dictionary d.
3 and 7 are the values of dictionary d
All of the above.
2. What will be the output of the Python code?
d1={"abc":5,"def":6,"ghi":7}
print(d1[0])
abc
5
{“abc”:5}
Error
3. What will be the output of the following Python code snippet?
>>> d = {"john":40, "peter":45}
>>> d["john"]
40
45
“john”
“peter”
4. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
d.size()
len(d)
size(d)
d.len()
5. What will be the output of the following Python code snippet?
a = {i: i * i for i in range(6)}
print (a)
Dictionary comprehension doesn’t exist
{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}
{ 0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
6. To read two characters from a file object infile, we use ________.
infile.read(2)
infile.read()
infile.readline()
infile.readlines()
7. Which of the following command is used to open a file “c:\temp.txt” in read-mode only?
infile = open(“c:\temp.txt”, “r”)
infile = open(“c:\\temp.txt”, “r”)
infile = open(file = “c:\temp.txt”, “r+”)
infile = open(file = “c:\\temp.txt”, “r+”)
8. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?
file.readlines()
n = file.read()
file.read(n)
file.readline(n)
9. What is the last action that must be performed on a file?
Close
Save
End
Write
10. Which of the following is not a method of opening files?
Read
Append
Write
Replace
