wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

2.2 J277 File handling

Total questions: 23

Worksheet time: 10mins

Name
Class
Date
1.

Mode in the following program to open file for writing in a mode where new data will be added at the end of old data

file =open("poem.txt","__")

a)

r

b)

w

c)

a

d)

r+

2.

Choose the correct statement to close a file stream named myfile:

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

3.

What will be the output-

f=open('abc.txt','a')

text="xyz"

f.write(text)

f.close()

a)

Writing into the file without erasing old content

b)

Writing into the file by erasing old content

c)

reading from the file without erasing old content

d)

reading from the file by erasing old content

4.

Which function will read a single line at a time?

a)

read()

b)

read(n)

c)

readline()

d)

readlines()

5.

Which memory is volatile memory?

a)

RAM

b)

Hard Disk

c)

CD

d)

Pen drive

6.

Where files are stored permanently?

a)

RAM

b)

Hard Disk

c)

Cache memory

d)

None of these

7.

What is the use of “a” in file handling?

a)

Read

b)

Write

c)

Append

d)

None of the mentioned

8.
What is the data type of data read from a file?
a)
String
b)
Integer
c)
Real
d)
Boolean
9.
Opening a file with write access causes the contents of the file to removed first.
a)
True
b)
False
10.

_____ function without any argument removes leading and trailing whitespaces.

a)

strip()

b)

lstrip()

c)

rstrip()

d)

trim()

11.

When reading text files, what are the hidden characters typically at the end of each line?

a)

\n

b)

»

c)

#

d)

@

12.

Which is the best type of loop to write a file in?

a)

while loop

b)

for loop

c)

repeat loop

13.

What character is used to comment code out

(a)  

14.

Mode in the following program to open file for writing in a mode where new data will be added at the end of old data


file =open("poem.txt","__")

a)

r

b)

a

c)

w

d)

x

15.

What will be the output-

f=open('abc.txt','a')

text="xyz"

f.write(text)

f.close()

a)

Writing into the file without erasing old content

b)

Writing into the file by erasing old content

c)

reading from the file without erasing old content

d)

reading from the file by erasing old content

16.

What will happen if you try to open a non-existent file in read mode?

a)

The file is opened as an empty file

b)

The file is opened in append mode

c)

An error is raised

d)

A new file is created

17.

Which of the following modes allows both reading and writing to a file?

a)

r+

b)

w

c)

r

d)

a

18.
Opening a file with write access causes the contents of the file to removed first.
a)
True
b)
False
19.
What is the last action that must be performed on a file?
a)
Close
b)
Save
c)
End
d)
Write
20.

What is the correct order of operations for writing data to a file?

a)

Write, open, close

b)

Close, open, write

c)

Open, close, write

d)

Open, write, close

21.

Choose the correct statement to close a file stream named myfile:

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

22.

A text file has the file extension of:

a)

.pdf

b)

.txt

c)

.doc

d)

.text

23.

Organize these files extensions in the correct type.

Categorize the following

Notes.txt

Document.doc

Photo1.jpg

Picture2.png

Image3.gif

Novel.pdf

Song3.mp3

Track4.wav

Image File
Text File
Video File
Music File