Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

File Operations Quiz

Total questions: 100

Worksheet time: 50mins

Name
Class
Date
1.

Which of the following is a topic listed in the content?

a)

File Operations

b)

Animal Habitats

c)

Weather Patterns

d)

Math Equations

2.

Which topic is about sending file stream objects to functions?

a)

Passing File Stream Objects to Functions

b)

Binary Files

c)

Random-Access Files

d)

Member Functions for Reading and Writing Files

3.

Which topic deals with reading and writing files?

a)

Member Functions for Reading and Writing Files

b)

File Operations

c)

Binary Files

d)

Random-Access Files

4.

Which topic is about files that store data in a special format?

a)

Binary Files

b)

File Operations

c)

Random-Access Files

d)

Passing File Stream Objects to Functions

5.

Which topic is about accessing files in any order?

a)

Random-Access Files

b)

File Operations

c)

Binary Files

d)

Member Functions for Reading and Writing Files

6.

What is a file?

a)

A collection of data stored on a computer's disk.

b)

A type of fruit.

c)

A kind of animal.

d)

A piece of clothing.

7.

What can you do with data saved in a file?

a)

Reuse it later.

b)

Throw it away.

c)

Eat it.

d)

Paint it.

8.

Which of the following is a program that uses files to store and retrieve data?

a)

Word processing

b)

Cooking recipes

c)

Running shoes

d)

Television

9.

Which of these is NOT mentioned as a program that uses files?

a)

Television

b)

Databases

c)

Spreadsheets

d)

Compilers

10.

Which data type is used only to read data from files into memory?

a)

ifstream

b)

ofstream

c)

fstream

d)

int

11.

What is the main purpose of the ofstream data type?

a)

To create files and write data to them

b)

To read data from files only

c)

To delete files

d)

To sort data in files

12.

Which data type can be used to create files, write data to them, and read data from them?

a)

fstream

b)

ifstream

c)

ofstream

d)

string

13.

What header file is required for ifstream, ofstream, and fstream data types?

a)

fstream

b)

iostream

c)

math.h

d)

stdio.h

14.

Which header file is required to use file streams in C++?

a)

fstream

b)

iostream

c)

math.h

d)

stdio.h

15.

Which data type is used for input files in C++?

a)

ifstream

b)

ofstream

c)

fstream

d)

string

16.

Which data type is used for output files in C++?

a)

ofstream

b)

ifstream

c)

fstream

d)

int

17.

Which data type can be used for both input and output files in C++?

a)

fstream

b)

ifstream

c)

ofstream

d)

char

18.

What symbols can be used to read from and write to a file in C++?

a)

>> , <<

b)

++ , --

c)

== , !=

d)

&& , ||

19.

Which member function can be used to test for the end of an input file?

a)

eof

b)

end

c)

close

d)

open

20.

What can the fstream object be used for?

a)

For either input or output

b)

Only for input

c)

Only for output

d)

For printing only

21.

What must you specify on the open statement when using fstream?

a)

Mode

b)

File name only

c)

File size

d)

File type

22.

What does ios::in mode mean?

a)

For input files

b)

For output files

c)

For printing files

d)

For deleting files

23.

What does ios::out mode mean?

a)

For output files

b)

For input files

c)

For copying files

d)

For renaming files

24.

How can you combine modes on an open call in fstream?

a)

By using a vertical bar (|)

b)

By using a comma (,)

c)

By using a plus (+)

d)

By using a dash (-)

25.

What does the file access flag ios::app do?

a)

It appends data to the end of the file.

b)

It deletes the file contents.

c)

It reads data from the file.

d)

It opens the file in binary mode.

26.

Which file access flag allows data to be written or read in pure binary format?

a)

ios::binary

b)

ios::app

c)

ios::in

d)

ios::trunc

27.

What happens when you use the ios::in flag?

a)

Data will be read from the file.

b)

Data will be deleted from the file.

c)

Data will be written to the file.

d)

The file will be opened in binary mode.

28.

Which file access flag causes the file’s contents to be deleted if it already exists?

a)

ios::trunc

b)

ios::app

c)

ios::binary

d)

ios::in

29.

What does the ios::out flag do?

a)

It allows data to be written to the file.

b)

It reads data from the file.

c)

It opens the file in binary mode.

d)

It appends data to the file.

30.

If the file already exists, which flag causes the program to go directly to the end of the file?

a)

ios::ate

b)

ios::app

c)

ios::out

d)

ios::binary

31.

What is the purpose of the example shown in the image?

a)

To copy 10 numbers between files

b)

To add numbers together

c)

To print numbers on the screen

d)

To delete files

32.

Which function is used to open a file for reading in the example?

a)

fstream infile("input.txt", ios::in);

b)

fstream outfile("output.txt", ios::out);

c)

int num;

d)

for (int i = 1; i <= 10; i++)

33.

What does infile.close() do in the code?

a)

Closes the input file

b)

Opens the output file

c)

Reads a number from the file

d)

Writes a number to the file

34.

How many times does the loop run in the example?

a)

10 times

b)

5 times

c)

1 time

d)

20 times

35.

What is the main purpose of ifstream in file operations?

a)

Open for input only

b)

Open for output only

c)

Open for both input and output

d)

Open for deleting files

36.

What happens if you try to open a file with ifstream and the file does not exist?

a)

The file is created

b)

The open fails

c)

The file is deleted

d)

The file is renamed

37.

What is the main purpose of ofstream in file operations?

a)

Open for input only

b)

Open for output only

c)

Open for both input and output

d)

Open for copying files

38.

What happens if you open a file with ofstream and the file already exists?

a)

The file is left unchanged

b)

The file contents are erased

c)

The file is renamed

d)

The file is copied

39.

What type must the filename be when used by ifstream, ifstream, and fstream objects?

a)

An integer

b)

A c-string (array of char)

c)

A float

d)

A boolean

40.

If you have a C++ string and want to use it as a filename in ifstream, what should you do?

a)

Use it directly

b)

Convert it to a c-string using c_str()

c)

Change it to an integer

d)

Delete the string

41.

What is the correct way to open a file called "grades.txt" using ifstream in C++?

a)

ifstream gradeList("grades.txt");

b)

ifstream openFile("grades.txt");

c)

file gradeList("grades.txt");

d)

open grades.txt;

42.

What does it mean if the file stream object is set to NULL or 0 (false) after trying to open a file?

a)

It means the file could not be opened.

b)

It means the file was opened successfully.

c)

It means the file is empty.

d)

It means the file is read-only.

43.

Which code can be used to check if a file failed to open using the fail member function?

a)

if (gradeList.fail())

b)

if (gradeList.open())

c)

if (fail.gradeList())

d)

if (open.fail(gradeList))

44.

Why is it useful to pass file stream objects to functions?

a)

Because it helps in handling files easily.

b)

Because it makes the code longer.

c)

Because it is not allowed.

d)

Because it makes the program slower.

45.

How should you always pass file stream objects to functions?

a)

By reference

b)

By value

c)

By copying

d)

By deleting

46.

What is the main topic of the program shown in the image?

a)

Passing file stream objects to functions

b)

Reading numbers from a file

c)

Sorting a list of numbers

d)

Drawing shapes on the screen

47.

Which header file is included to use file streams in the program?

a)

#include

b)

#include

c)

#include

d)

#include

48.

What is the name of the function that tries to open a file in the program?

a)

openFileIn

b)

showContents

c)

main

d)

readFile

49.

What does the program do if the file cannot be opened?

a)

It prints "File open error!" and exits

b)

It tries to open another file

c)

It continues running without reading the file

d)

It prints "File opened successfully!"

50.

What is the name of the file the program tries to open?

a)

demofile.txt

b)

myfile.txt

c)

data.txt

d)

output.txt

51.

What is the maximum amount to read from a line in the file, as defined in the program?

a)

81

b)

100

c)

50

d)

10

52.

What is a function call in a program?

a)

When you use a function in your program

b)

When you write the function for the first time

c)

When you close the program

d)

When you save your file

53.

What is a function definition?

a)

When you write what a function does

b)

When you call a function

c)

When you print something

d)

When you open a file

54.

What does the function openFileIn do?

a)

Opens a file for input

b)

Prints a message

c)

Closes a file

d)

Reads a number

55.

What does the function openFileIn return if the file fails to open?

a)

false

b)

true

c)

1

d)

0

56.

What does the function openFileIn return if the file opens successfully?

a)

true

b)

false

c)

0

d)

-1

57.

What does the function showContents do in the program?

a)

It reads each name from a file and displays it on the screen.

b)

It adds numbers together.

c)

It draws a picture.

d)

It sends an email.

58.

Which of the following names is NOT shown in the program screen output?

a)

Brown

b)

Jones

c)

Smith

d)

Willis

59.

What message is displayed when the file is opened successfully?

a)

File opened successfully.

b)

File closed.

c)

Error in file.

d)

File not found.

60.

What is the last word shown in the program screen output?

a)

Done.

b)

Start.

c)

Hello.

d)

End.

61.

Which member function reads input including whitespace?

a)

getline

b)

get

c)

put

d)

write

62.

What does the 'get' member function do?

a)

Reads input including whitespace

b)

Reads a single character

c)

Writes a single character

d)

Deletes a character

63.

Which member function writes a single character?

a)

getline

b)

get

c)

put

d)

read

64.

What is a c-string used for in the getline method?

a)

To hold input

b)

To display output

c)

To add numbers

d)

To print text

65.

How many arguments are needed for the getline method when working with C-strings?

a)

Three

b)

One

c)

Two

d)

Four

66.

What is the default delimiter for the getline method?

a)

'\n'

b)

'\t'

c)

' '

d)

','

67.

Which of the following is NOT an argument for the getline method?

a)

File name

b)

C-string to hold input

c)

Number of characters to read

d)

Terminator (or delimiter)

68.

What does the terminator (or delimiter) do in the getline method?

a)

Stops reading if encountered before the number of characters was read in

b)

Starts the program

c)

Prints the output

d)

Adds two numbers

69.

What is the purpose of the getline member function in the program?

a)

To read a line of data from a file.

b)

To write data to a file.

c)

To delete a file.

d)

To print data to the screen.

70.

Which header file is included to use file stream objects in the program?

a)

b)

c)

d)

71.

What does the variable 'input' hold in the program?

a)

File input

b)

File name

c)

File size

d)

File output

72.

What happens if the file "murphy.txt" cannot be opened?

a)

An error message is displayed.

b)

The program continues as normal.

c)

The file is created.

d)

Nothing happens.

73.

What is the output of the program shown in the image?

a)

Jayne Murphy 47 Jones Circle Almond, NC 28702

b)

John Smith 123 Main Street New York, NY 10001

c)

Jane Doe 456 Elm Street Los Angeles, CA 90001

d)

Michael Brown 789 Oak Avenue Chicago, IL 60601

74.

What does the code do after reading each line from the file?

a)

It prints the line to the screen.

b)

It deletes the line.

c)

It saves the line to another file.

d)

It changes the line to uppercase.

75.

What is used as a delimiter in the getline function in the code?

a)

\n (newline)

b)

,

c)

.

d)

;

76.

What is the default delimiter for the getline function in C++?

a)

'\n'

b)

','

c)

';'

d)

' '

77.

Which programming language is the getline function used for, as shown in the material?

a)

C++

b)

Python

c)

Java

d)

HTML

78.

What is the correct prototype for the getline function?

a)

getline(fileObject, stringBuffer, delimiter);

b)

getline(stringBuffer, fileObject, delimiter);

c)

getline(delimiter, fileObject, stringBuffer);

d)

getline(fileObject, delimiter, stringBuffer);

79.

In the example, what is the name of the file being read?

a)

input.txt

b)

output.txt

c)

data.txt

d)

file.txt

80.

What is the purpose of the getline function in the program?

a)

To read a line of data from a file.

b)

To write data to a file.

c)

To delete a file.

d)

To print a file name.

81.

What is the name of the input file used in the program?

a)

murphy.txt

b)

input.txt

c)

data.txt

d)

output.txt

82.

What will the program do if it cannot open the file?

a)

It will show an error message.

b)

It will print the file contents.

c)

It will create a new file.

d)

It will do nothing.

83.

Which of the following is the correct output shown on the screen?

a)

Jayne Murphy 47 Jones Circle Almond, NC 28702

b)

John Smith 123 Main Street New York, NY 10001

c)

Jane Doe 456 Elm Road Boston, MA 02110

d)

Mary Lee 789 Oak Avenue Chicago, IL 60616

84.

What is the name of the file being opened in the example program?

a)

murphy.txt

b)

jones.txt

c)

circle.txt

d)

input.txt

85.

What does the program do if it cannot open the file?

a)

It prints "ERROR: Cannot open file."

b)

It prints "File opened successfully."

c)

It continues reading the file.

d)

It closes the file.

86.

Which programming language is used in the example?

a)

C++

b)

Python

c)

Java

d)

HTML

87.

What is the purpose of the getline function in the program?

a)

To read a line of data from a file

b)

To write data to a file

c)

To close the file

d)

To print data to the screen

88.

What is the first line in the input file?

a)

Jayne Murphy

b)

47 Jones Circle

c)

Almond, NC 28702

d)

NC

89.

What is the output shown on the screen for the first line of the input file?

a)

Jayne

b)

Murphy

c)

47

d)

Circle

90.

What does the "get" function do in single character I/O?

a)

To read a single character from an input file.

b)

To write a single character to an output file.

c)

To delete a file.

d)

To copy a file.

91.

What does the "put" function do in single character I/O?

a)

To write a single character to an output file.

b)

To read a single character from an input file.

c)

To rename a file.

d)

To move a file.

92.

Which function will read any character, including whitespace?

a)

get

b)

put

c)

delete

d)

copy

93.

Which function is used to write a single character to an output file?

a)

put

b)

get

c)

read

d)

open

94.

What type of data do binary files contain?

a)

Unformatted and non-human-readable data

b)

Formatted and human-readable data

c)

Only numbers

d)

Only text

95.

How do you specify that a file should be opened as a binary file?

a)

By using a binary flag

b)

By using a text flag

c)

By using a number flag

d)

By using a color flag

96.

What is used instead of << to read data from files in binary mode?

a)

read

b)

write

c)

print

d)

scan

97.

In the function fin.read(&ch, sizeof(ch)), what does the first parameter represent?

a)

The address of where to put the data being read in

b)

How many bytes to read from the file

c)

The name of the file

d)

The type of data

98.

In the function fin.read(&ch, sizeof(ch)), what does the second parameter represent?

a)

How many bytes to read from the file

b)

The address of the data

c)

The file name

d)

The type of variable

99.

What type does the function expect the data variable to be?

a)

char

b)

int

c)

float

d)

double

100.

If the data variable is not of type char, what must be done?

a)

It must be type casted

b)

It must be deleted

c)

It must be printed

d)

It must be ignored