wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python File handling - 2

Total questions: 20

Worksheet time: 15mins

Name
Class
Date
1.

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+”)

2.

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

a)

outfile = open(“c:\temp.txt”, “w”)

b)

outfile = open(“c:\\temp.txt”, “w”)

c)

outfile = open(file = “c:\temp.txt”, “w+”)

d)

outfile = open(file = “c:\\temp.txt”, “w+”)

3.

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

a)

outfile = open(“c:/temp.txt”, “a”)

b)

outfile = open(“c:\temp.txt”, “w+”)

c)

outfile = open(“c:\\temp.txt”, “a”)

d)

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

4.

Which of the following statements are true regarding the opening modes of a file?

A. When you open a file for reading, if the file does not exist, an error occurs.

B. When you open a file for writing, if the file does not exist, an error occurs.

C. When you open a file for reading, if the file does not exist, the program will open an empty file.

D. When you open a file for writing, if the file does not exist, a new file is created.

E. When you open a file for writing, if the file exists, the existing file is overwritten with the new file.

a)

Only A

b)

A and B

c)

A, D and E

d)

A, B and E

5.

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

a)

file.read(n)

b)

n = file.read()

c)

file.readline(n)

d)

file.readlines()

6.

Which of the following commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?

a)

tmpfile.read(n)

b)

tmpfile.read()

c)

tmpfile.readline()

d)

tmpfile.readlines()

7.

Which of the following commands can be used to read the next line in a file using the file object <tmpfile>?

a)

tmpfile.read(n)

b)

tmpfile.read()

c)

tmpfile.readline()

d)

tmpfile.readlines()

8.

What does the <readlines()> method returns?

a)

str

b)

a list of lines

c)

list of single characters

d)

list of integers

9.

Which of the following commands can be used to read the remaining lines in a file using the file object <tmpfile>?

a)

tmpfile.read(n)

b)

tmpfile.read()

c)

tmpfile.readline()

d)

tmpfile.readlines()

10.

What are the two built-in functions to read a line of text from standard input, which is by default the keyboard?

a)

Input

b)

Read

c)

Scanner

11.

What will be the output of the following code snippet?

a)

TechBeamers

Hello viewers!!

b)

Name of the file: myfile.txt

TechBeamers

Hello Viewers!!

c)

TechBeamers Hello viewers!!

d)

Syntax Error

12.

What will be the output of the following code snippet?

a)

TechBeamers

Hello viewers!!

b)

Name of the file: myfile.txt

TechBeamers

Hello Viewers!!

c)

TechBeamers Hello viewers!!

d)

Syntax Error

13.

What will be the output of the following code snippet?

Assuming that input given by user is :Tech Beamers

a)

Tech Beamers

b)

Tech

c)

Tech

Beamers

d)

Syntax Error

14.

What will be the output of the following code snippet?

a)

Runtime Error

b)

Hello World how are you today

c)

[‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]

d)

Hello

15.

What is the correct syntax of open() function?

a)

file_name = open(file_name [, access_mode][, buffering])

b)

file_object = open(file_name [, access_mode][, buffering])

c)

file object = open(file_name)

d)

None of the above

16.

What will be the output of the following code snippet?

a)

Runtime Error

b)

True

c)

False

d)

Hello world

17.

What will be the output of the following code snippet?

a)

Runtime Error

b)

[‘This is 1st line\n’, ‘ This is 2nd line\n’, ‘This is 3rd line’]

c)

[‘ This is 2nd line\n’, ‘This is 3rd line’]

d)

[ ]

18.

Which of the following statements is correct regarding the seek() method?

a)

sets the file position to the end of file

b)

position of file pointer remains unchanged

c)

sets the file's current position at the offset

d)

the file pointer is set to the start of the file

19.

What will be the output of the following code snippet?

a)

red

yellow

blue

b)

[‘red\n’, ‘yellow\n’, ‘blue\n’]

c)

Error: I/O operation on closed file

d)

Compilation error

20.

What is the use of tell() method in python?

a)

tells you the current position within the file

b)

tells you the end position within the file

c)

tells you the file is opened or not

d)

none of the mentioned