Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Data file handling

Total questions: 89

Worksheet time: 42mins

Name
Class
Date
1.



(a)  

2.

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

3.

see the program and fill in the blank with correct statement.

(a)  

4.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

5.

See the following code snippet and fill in the blank with appropriate statement

(a)  

6.

Fill in the blank with appropriate statement.

(a)  

7.

which function is used to read records from a binary file?

a)

pickle.load(f)

b)

pickle.read()

c)

pickle.open()

d)

pickle.dump(l,f)

8.

write statement to use the required module for working with comma separated values (csv) files.

(a)  

9.

Write the appropriate statement in the blank.

(a)  

10.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

11.

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

12.

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+

13.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

14.

Fill in the blank with appropriate statement.

(a)  

15.

Write the appropriate statement in the blank.

(a)  

16.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

17.

Which function read single line at a time?

a)

read()

b)

read(n)

c)

readline()

d)

readlines()

18.

Which file is not binary file?

a)

student.mp4

b)

student.jpeg

c)

student.csv

d)

student.exe

19.

Where files are store permanently?

a)

RAM

b)

Hard Disk

c)

Cache memory

d)

None of these

20.

Which function is used to write multiple lines in text file?

a)

write()

b)

writelines()

c)

lines()

d)

print()

21.
Appending to a file means adding extra data into the file.
a)
True
b)
False
22.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

23.

Q.2 Which of the following statements are not 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 writing, if the file does not exist, a new file is created.

d)

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

24.

Q.3 Which module is used to work with binary files?

a)

CSV module

b)

pickle module

c)

math module

d)

binary module

25.

Q.5 'try' block is used to

a)

execute a code

b)

through the exceptions

c)

to try some random code

d)

to give errors

26.

Function used to write data in binary format

a)

write()

b)

output()

c)

load()

d)

dump()

27.
Which of the following is not a method of opening files?
a)
Replace
b)
Append
c)
Write
d)
Read
28.
What is the data type of data read from a file?
a)
String
b)
Integer
c)
Real
d)
Boolean
29.
Reading from a file often involves using...
a)
A loop
b)
An condition
c)
Comments
30.

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

a)

Read

b)

Write

c)

Append

d)

None of the mentioned

31.

Select the statement to open a file named MYDATA.dat in read binary mode. Let the file object name be myfile.

a)

myfile=open("MYDATA.dat","rb")

b)

myfile=open("MYDATA.dat","r+")

c)

myfile=open("MYDATA.dat","r")

32.

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+

33.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

34.

What will be the ouput -

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

x=f.read()

f.close()

print(x)

a)

Error(not readable)

b)

Error(not writable)

c)

It will read all the data of the file.

d)

none

35.

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

36.

Which function is used to write multiple lines in text file?

a)

write()

b)

writelines()

c)

lines()

d)

print()

37.

Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?

a)

file =open("Record.dat","wb")

b)

file =open("Record.dat","rb")

c)

file =open("Record.dat","ab")

d)

file =open("Record.dat","rb+")

38.

The readlines() method returns

a)

a. str

b)

b. a list of lines

c)

c. a list of single characters

d)

d. a list of integers

39.

An absolute path name begins at the

a)

leaf

b)

stem

c)

root

d)

current directory

40.

What will be the output of the following Python code?

a)

True

b)

False

c)

None

d)

error

41.

Q1. Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(song_file.read(4)) ?

a)

I ca

b)

I can

I c

I can't write one song that's not about you

Can't drink without thinkin' about

c)

I c

d)

you

Is it too late to tell you that

Everything means nothing if I can't have you?

42.

Given the following file opened as song_file:


I can't write one song that's not about you

Can't drink without thinkin' about you

Is it too late to tell you that

Everything means nothing if I can't have you?


What is the result of

print(text_file.readlines()[1]) ?

a)

I can't write one song that's not about you

b)

Can't drink without thinkin' about you

I can't write one song that's not about you

Can't drink without thinkin' about

c)

you

Is it too late to tell you that

Everything means nothing if I can't have you?

d)

I

43.

What is the correct way to open the file file_in.txt for reading?

a)

open('file_in.txt','a')

b)

open('file_in.txt','r+')

c)

open('file_in.txt', 'r')

d)

open('file_in.txt','w')

44.

What is the best way to read an entire file into a single string?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

45.

What is the best way to read an entire file into a list of strings?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

46.

What is the best way to read the first line of a file?

a)

.read()

b)

.readline()

c)

.readlines()

d)

.read(all)

47.

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

a)

\n

b)

»

c)

#

d)

@

48.

This command will open MyFile = open('MyText.txt', 'r')

a)

To write to the file

b)

To read the file

c)

To append to the file

49.

If I want to add to the end of a text file, I should:

a)

MyCipher = open("MyCipher.txt", "w")

b)

MyCipher = open("MyCipher.txt", "a")

c)

MyCipher = open("MyCipher.txt", "w+")

d)

MyCipher = open("MyCipher.txt", "s")

50.

Which of these will generate an error?

a)

MyCipher.write("writing")

b)

MyCipher..write(writing)

c)

MyCipher.write("writing",writing)

d)

MyCipher.readline()

51.

How do you close the file?

a)

MyFile = close()

b)

MyFile.close

c)

close = MyFile()

d)

MyFile.close()

52.

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

a)

While loop

b)

for loop

c)

repeat loop

53.

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

a)

myfile.terminate()

b)

myfile.end()

c)

myfile.close()

d)

myfile.remove()

54.

Why is it best to open a file with "with" in Python?

a)

It automatically closes the file

b)

Easier to read the code

c)

Better for binary files

d)

Closers to the pseudocode

55.

Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)

a)

print(file_in.read())

lines = file_in.readlines()

b)

print(file_in)

lines = file_in.readlines()

c)

for line in lines:

print(line)

d)

for i in range(len(lines)):

print(lines)

e)

for line in file_in:

print(line)

56.

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

a)

Read

b)

Write

c)

Append

d)

None of the mentioned

57.

Which function is used to read single line from file?

a)

Readline()

b)

Readlines()

c)

Readstatement()

d)

Readfullline()

58.

Which function is used to close a file in python?

a)

Close()

b)

Stop()

c)

End()

d)

Closefile()

59.

Is it possible to create a text file in python?

a)

Yes

b)

No

c)

Machine dependent

d)

All of the mentioned

60.

What is the use of seek() method in files?

a)

sets the file’s current position at the offset

b)

sets the file’s previous position at the offset

c)

sets the file’s current position within the file

d)

none of the mentioned

61.

Which of the following mode will refer to binary data?

a)

r

b)

w

c)

+

d)

b

62.

EOL stands for

a)

End Of Line

b)

End Of List

c)

End of Lines

d)

End Of Location

63.

Which of the following is nor a proper file access mode?

a)

close

b)

read

c)

write

d)

append

64.

In f=open(“data.txt”, “r”), r refers to __________.

a)

File handle

b)

File object

c)

File Mode

d)

Buffer

65.

Which of the following functions do you use to write data in the binary format?

a)

write

b)

output

c)

dump

d)

send

66.

Which of the following command is used to open a file “c:\temp.txt” for reading in binary format only?

a)

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

b)

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

c)

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

d)

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

67.

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()

68.

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.

69.

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”, “rw”)

c)

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

d)

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

e)

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

70.

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

71.

Which file execute faster?

a)

Text file

b)

Binary file

c)

CSV file

72.

Mode used for reading and writing in binary file

a)

wb

b)

w

c)

wb+

d)

w+

73.

Function used to write data in binary format

a)

write()

b)

output()

c)

load()

d)

dump()

74.

The type of operation possible in an opened file is governed by

a)

File Object

b)

File Mode

c)

File Path

d)

open() Method

75.

Module used to read and write into binary files

a)

dump

b)

load

c)

pickle

d)

open

76.

Function returns the current position of file pointer in the file

a)

tell()

b)

seek()

c)

read()

d)

flush()

77.

The default file - open mode is (a)   mode.

78.

Which of the following is not a valid attribute of a file object (fp)?

a)

fp.name

b)

fp.closed

c)

fp.size

d)

fp.mode

79.

How do you change the file position to an offset value from the start?

a)

fp.seek(offset, 0)

b)

fp.seek(offset, 1)

c)

fp.seek(offset, 2)

d)

none of the mentioned

80.

This command will open MyFile = open("MyText.txt")

a)

To read the file

b)

To append to the file

c)

To write to the file

81.

If I want to add to the end of a text file, I should:

a)

MyCipher = open("MyCipher.txt", "w")

b)

MyCipher = open("MyCipher.txt", "a")

c)

MyCipher = open("MyCipher.txt", "w+")

d)

MyCipher = open("MyCipher.txt", "s")

82.

Which of these will generate an error?

a)

MyCipher.write("writing")

b)

MyCipher..write(writing)

c)

MyCipher.write("writing",writing)

d)

MyCipher.readline()

83.

How do you close the file?

a)

MyFile = close()

b)

MyFile.close

c)

close = MyFile()

d)

MyFile.close()

84.

Why is it best to open a file with with in Python?

a)

It automatically closes the file

b)

Easier to read the code

c)

Better for binary files

d)

Closers to the pseudocode

85.

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

a)

While loop

b)

for loop

c)

repeat loop

86.

What command can you use to separate parts of a line?

a)

separate()

b)

split()

c)

divide()

d)

cut()

87.

What command can you use to send back data from a function?

a)

return

b)

sendback

c)

back

d)

print()

88.

How do you assign variables in pseudocode?

a)

←

b)

=

c)

==

d)

assign

89.

How do you CALL the function name in Python?

a)

name()

b)

def name()

c)

call name()

d)

GOTO name()