Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

PYTHON QUIZ2

Total questions: 10

Worksheet time: 8mins

Name
Class
Date
1.

1. Which of the following is correct with respect to the python code d = {"a":3,"b":7}?

a)

Dictionary d is created.

b)

a and b are the keys of dictionary d.

c)

3 and 7 are the values of dictionary d

d)

All of the above.

2.

2. What will be the output of the Python code?

d1={"abc":5,"def":6,"ghi":7}

print(d1[0])

a)

abc

b)

5

c)

{“abc”:5}

d)

Error

3.

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.

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

a)

d.size()

b)

len(d)

c)

size(d)

d)

d.len()

5.

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

a = {i: i * i for i in range(6)}

print (a)

a)

Dictionary comprehension doesn’t exist

b)

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

c)

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

d)

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

6.

6. To read two characters from a file object infile, we use ________.

a)

infile.read(2)

b)

infile.read()

c)

infile.readline()

d)

infile.readlines()

7.

7. Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

a)

infile = open(“c:\temp.txt”, “r”)

b)

infile = open(“c:\\temp.txt”, “r”)

c)

infile = open(file = “c:\temp.txt”, “r+”)

d)

infile = open(file = “c:\\temp.txt”, “r+”)

8.

8. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

a)

file.readlines()

b)

n = file.read()

c)

file.read(n)

d)

file.readline(n)

9.

9. What is the last action that must be performed on a file?

a)

Close

b)

Save

c)

End

d)

Write

10.

10. Which of the following is not a method of opening files?

a)

Read

b)

Append

c)

Write

d)

Replace