AlgoMaster Logo

File I/O - Quiz

Last Updated: December 6, 2025

1 min read

File I/O Quiz

31 quizzes

1
Multiple Choice

Which header must you include to use std::ifstream and std::ofstream?

2
Multiple Choice

What is the safest way to open a file for reading text?

3
Multiple Choice

Which open mode will append to an existing text file without truncating it?

4
Multiple Choice

Which is the most memory‑efficient way to process a huge log file?

5
Multiple Choice

Which statement correctly opens a binary file for both reading and writing?

6
Multiple Choice

Which function retrieves the current read position in an ifstream?

7
Multiple Choice

What is a practical use of std::istringstream?

8
Multiple Choice

Why is std::getline often preferred over operator>> for reading config lines?

9
Multiple Choice

Which approach best preserves class invariants when saving an object to a text file?

10
Multiple Choice

When writing portable binary files containing integers, what is MOST important?

11
Multiple Choice

Which is a good pattern to ensure a file handle is always closed?

12
Code Completion

Open a text file for reading using std::ifstream and check success.

cpp
1
std::ifstream in("config.txt"); if (!in.()) { throw std::runtime_error("open failed"); }

Click an option to fill the blank:

13
Code Completion

Write a POD struct to a binary file using std::ofstream::write.

cpp
1
out.write(reinterpret_cast<char*>(&person), (person));

Click an option to fill the blank:

14
Code Completion

Construct a string stream for parsing a log line.

cpp
1
std::istringstream iss();

Click an option to fill the blank:

15
Output Prediction

What is the output of this code reading line by line?

1#include <iostream>
2#include <fstream>
3#include <string>
4int main() {
5    std::istringstream file("one\ntwo\nthree\n");
6    std::string s;
7    int count = 0;
8    while (std::getline(file, s)) {
9        if (!s.empty()) ++count;
10    }
11    std::cout << count << "\n";
12    return 0;
13}
16
Output Prediction

What does the program print about the file size and position?

1#include <iostream>
2#include <fstream>
3int main() {
4    std::istringstream in("abcd");
5    in.seekg(0, std::ios::end);
6    auto size = in.tellg();
7    in.seekg(1, std::ios::beg);
8    auto pos = in.tellg();
9    std::cout << size << " " << pos << "\n";
10    return 0;
11}
17
Output Prediction

What is printed after writing and rereading from a stringstream?

1#include <iostream>
2#include <sstream>
3int main() {
4    std::stringstream ss;
5    ss << 10 << " " << 20;
6    int a = 0, b = 0;
7    ss >> a;
8    ss >> b;
9    std::cout << a + b << "\n";
10    return 0;
11}
18
Output Prediction

What will this binary write/read example output?

1#include <iostream>
2#include <sstream>
3#include <vector>
4int main() {
5    std::stringstream buf(std::ios::in | std::ios::out | std::ios::binary);
6    std::vector<int> v{1,2,3};
7    buf.write(reinterpret_cast<char*>(v.data()), v.size() * sizeof(int));
8    buf.seekg(0);
9    int x[3] = {0,0,0};
10    buf.read(reinterpret_cast<char*>(x), 3 * sizeof(int));
11    std::cout << x[0] << x[1] << x[2] << "\n";
12    return 0;
13}
19
Output Prediction

What is the output when parsing with std::istringstream?

1#include <iostream>
2#include <sstream>
3#include <string>
4int main() {
5    std::string data = "42 cpp 3.5";
6    std::istringstream iss(data);
7    int i; std::string s; double d;
8    iss >> i >> s >> d;
9    std::cout << i << ":" << s << ":" << d << "\n";
10    return 0;
11}
20
Bug Spotting

Find the bug in this file reading code

Click on the line(s) that contain the bug.

cpp
1
#include <fstream>
2
#include <string>
3
void load(const std::string& path) {
4
    std::ifstream in(path);
5
    std::string line;
6
    while (!in.eof()) {
7
        std::getline(in, line);
8
        // process line
9
    }
10
}
21
Bug Spotting

Find the bug in this binary serialization code

Click on the line(s) that contain the bug.

cpp
1
#include <fstream>
2
struct Record {
3
    int id;
4
    double value;
5
};
6
void save(const Record& r) {
7
    std::ofstream out("rec.dat", std::ios::binary);
8
    out.write(reinterpret_cast<const char*>(&r), sizeof(&r));
9
}
10
 
22
Hotspot Selection

Click the line that risks losing buffered data when writing to a file

Click on the line to select.

cpp
1
#include <fstream>
2
#include <string>
3
void writeLog(const std::string& msg) {
4
    std::ofstream log("log.txt");
5
    log << msg;
6
    if (!log) return; // check error
7
}
8
 
23
Hotspot Selection

Click the line that moves the file pointer to the end of the file

Click on the line to select.

cpp
1
#include <fstream>
2
void inspect() {
3
    std::ifstream in("data.txt", std::ios::binary);
4
    in.seekg(0, std::ios::end);
5
    auto size = in.tellg();
6
}
7
 
24
Fill in the Blanks

Complete the loop that reads a text file line by line and prints it.

cpp
1
#include <fstream>
2
#include <string>
3
#include <iostream>
4
int main() {
5
std::ifstream in("data.txt");
6
std::string line;
7
while ((in, line)) {
8
std::cout << << '\n';
9
}
10
}
11

Click an option to fill blank 1:

25
Fill in the Blanks

Fill in code to write binary data and then seek back to the beginning.

cpp
1
#include <fstream>
2
int main() {
3
int value = 123;
4
std::ofstream out("num.bin", std::ios:: | std::ios::);
5
out.write(reinterpret_cast<char*>(&value), sizeof(value));
6
out.(0, std::ios::beg);
7
}
8

Click an option to fill blank 1:

26
Fill in the Blanks

Complete this parsing code using std::istringstream.

cpp
1
#include <sstream>
2
#include <string>
3
#include <iostream>
4
int main() {
5
std::string data = "10 20";
6
std:: iss(data);
7
int a = 0, b = 0;
8
iss >> a >> b;
9
std::cout << << '\n';
10
}
11

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code that positions within a file to read its last character.

cpp
1
#include <fstream>
2
#include <iostream>
3
int main() {
4
std::ifstream in("text.txt", std::ios::in);
5
in.seekg(0, std::ios::);
6
auto size = in.();
7
in.seekg(size - 1, std::ios::beg);
8
char c;
9
in.get(c);
10
std::cout << c << '\n';
11
}
12

Click an option to fill blank 1:

28
Matching

Match each stream type with its primary use in file/string I/O.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Matching

Match each flag with what it controls when opening file streams.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

30
Sequencing

Order the typical steps to safely read configuration from a text file.

Drag and drop to reorder, or use the arrows.

31
Sequencing

Order the steps for writing a POD object to and then reading it from a binary file.

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.