C++ File I/O Quiz

C++
0 Passed
0% acceptance

40 in-depth questions covering C++ file input/output operations, stream classes, text and binary file handling, error checking, and RAII patterns — with 16 code examples to solidify understanding.

40 Questions
~80 minutes
1

Question 1

What is the purpose of std::ifstream in C++?

A
To read data from files using stream operations
B
To write data to files
C
To create new files
D
To delete files
2

Question 2

What is the difference between text and binary file modes?

A
Text mode performs character conversion and treats certain characters specially, binary mode reads/writes data exactly as stored in memory
B
They are identical file modes
C
Binary mode is only for text files
D
Text mode is faster than binary mode
3

Question 3

What does the fail() function check in file streams?

cpp
#include <fstream>
#include <iostream>

std::ifstream file("data.txt");
if (file.fail()) {
    std::cout << "Failed to open file" << std::endl;
}
A
Whether the last operation failed due to format errors or stream corruption, separate from eof() which checks for end-of-file
B
Whether the file exists
C
Whether the file is empty
D
Whether the file is read-only
4

Question 4

What is RAII in the context of file I/O?

cpp
class FileHandler {
public:
    FileHandler(const std::string& filename) : file_(filename) {
        if (!file_.is_open()) {
            throw std::runtime_error("Cannot open file");
        }
    }
    
    ~FileHandler() {
        if (file_.is_open()) {
            file_.close();  // Automatic cleanup
        }
    }
    
    std::fstream& get() { return file_; }
    
private:
    std::fstream file_;
};
A
Resource Acquisition Is Initialization ensures file handles are automatically closed when objects go out of scope, preventing resource leaks
B
RAII prevents file operations
C
RAII is only for memory management
D
RAII requires manual cleanup
5

Question 5

What is the difference between ios::app and ios::ate file modes?

A
ios::app appends all writes to end of file, ios::ate moves position to end initially but allows writing anywhere
B
They are identical modes
C
ios::ate is for reading only
D
ios::app prevents reading
6

Question 6

What is the purpose of std::getline for file reading?

cpp
#include <fstream>
#include <string>

std::ifstream file("data.txt");
std::string line;
while (std::getline(file, line)) {
    // Process each line
    std::cout << line << std::endl;
}
A
To read entire lines from a file including whitespace, stopping at newline characters and handling different line ending formats
B
To read single characters only
C
To read binary data
D
To skip lines in a file
7

Question 7

What is the difference between tellg() and tellp() in file streams?

A
tellg() returns current get position for reading, tellp() returns current put position for writing
B
They are identical functions
C
tellg() is for writing, tellp() is for reading
D
tellp() doesn't exist in C++
8

Question 8

What is the purpose of stream manipulators in file I/O?

cpp
#include <fstream>
#include <iomanip>

std::ofstream file("data.txt");
file << std::setw(10) << std::left << "Name"
     << std::setw(5) << "Age" << std::endl;
file << std::setprecision(2) << std::fixed << 3.14159;
A
To control formatting of output data including width, precision, alignment, and number base without changing the data itself
B
To read data from files
C
To change file permissions
D
To compress file data
9

Question 9

What is the difference between ios::binary and ios::text modes?

A
ios::binary prevents character translation and newline conversion, ios::text performs platform-specific character and line ending conversions
B
They are identical modes
C
ios::text is faster for binary data
D
ios::binary converts line endings
10

Question 10

What is the purpose of std::fstream?

cpp
#include <fstream>

std::fstream file("data.txt", std::ios::in | std::ios::out);
if (file.is_open()) {
    std::string data;
    file >> data;  // Read
    file.seekp(0); // Move to beginning
    file << "New data"; // Write
}
A
To provide bidirectional file access for both reading and writing operations in a single stream object
B
To read files only
C
To write files only
D
To create directories
11

Question 11

What is the difference between seekg() and seekp()?

A
seekg() sets get position for reading operations, seekp() sets put position for writing operations
B
They are identical functions
C
seekg() is for writing, seekp() is for reading
D
seekp() moves the file pointer randomly
12

Question 12

What is the purpose of std::ios_base::failure exception?

cpp
#include <fstream>
#include <ios>

std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
try {
    file.open("nonexistent.txt");
} catch (const std::ios_base::failure& e) {
    std::cout << "File error: " << e.what() << std::endl;
}
A
To throw exceptions when file operations fail, providing an alternative to manual error checking with fail() and bad()
B
To create new files
C
To close files automatically
D
To read file metadata
13

Question 13

What is the difference between read() and operator>> for file input?

cpp
#include <fstream>

std::ifstream file("data.bin", std::ios::binary);
int value;
file.read(reinterpret_cast<char*>(&value), sizeof(value)); // Binary

std::ifstream textFile("data.txt");
textFile >> value; // Formatted text
A
read() performs unformatted binary input of exact byte counts, operator>> performs formatted text input with type conversion and whitespace skipping
B
They are identical operations
C
operator>> is for binary data
D
read() performs type conversion
14

Question 14

What is the purpose of stream buffers in file I/O?

A
To provide an abstraction layer between high-level stream operations and low-level file system calls, enabling buffering for performance and custom stream implementations
B
To store file names
C
To compress file data
D
To check file permissions
15

Question 15

What is the difference between gcount() and peek() in input streams?

cpp
#include <fstream>

std::ifstream file("data.txt");
char buffer[100];
file.read(buffer, 50);
std::streamsize bytesRead = file.gcount(); // How many bytes actually read

char nextChar = file.peek(); // Look at next character without extracting
A
gcount() returns number of characters extracted by last unformatted input operation, peek() examines next character without removing it from stream
B
They are identical functions
C
peek() removes characters from stream
D
gcount() examines characters without removing them
16

Question 16

What is the purpose of std::stringstream for file operations?

cpp
#include <sstream>
#include <fstream>

std::ifstream file("data.txt");
std::stringstream buffer;
buffer << file.rdbuf(); // Read entire file into stringstream
std::string content = buffer.str(); // Get as string
A
To provide string-based stream operations that can interface with file streams for parsing, formatting, and data manipulation without direct file access
B
To create new files
C
To delete files
D
To change file permissions
17

Question 17

What is the difference between file stream opening modes?

A
ios::in opens for reading, ios::out opens for writing, ios::trunc truncates existing content, ios::app appends to existing content
B
All modes are identical
C
Modes don't affect file operations
D
ios::in prevents writing
18

Question 18

What is the difference between formatted and unformatted I/O?

A
Formatted I/O converts data types and handles text representation, unformatted I/O transfers raw bytes without type conversion
B
They are identical approaches
C
Unformatted I/O is only for text
D
Formatted I/O is faster
19

Question 19

What is the purpose of stream iterators in file I/O?

cpp
#include <fstream>
#include <iterator>
#include <vector>

std::ifstream file("numbers.txt");
std::vector<int> numbers(
    std::istream_iterator<int>(file),
    std::istream_iterator<int>()
); // Read all integers from file
A
To provide iterator interface for stream operations, enabling use of algorithms and containers with file data through generic programming
B
To create new files
C
To delete files
D
To change file permissions
20

Question 20

What is the difference between file position and stream position?

A
File position is physical location in file, stream position includes buffering effects and may differ from actual file position
B
They are identical concepts
C
Stream position is always ahead of file position
D
File position includes buffering
21

Question 21

What is the difference between synchronous and asynchronous file I/O?

A
Synchronous I/O blocks until operation completes, asynchronous I/O allows program to continue while I/O operations happen in background
B
They are identical approaches
C
Asynchronous I/O is slower
D
Synchronous I/O requires threading
22

Question 22

What is the difference between file locking and stream synchronization?

A
File locking prevents concurrent access at OS level, stream synchronization coordinates access to stream objects in multithreaded programs
B
They are identical concepts
C
Stream synchronization prevents file access
D
File locking is automatic
23

Question 23

What is the difference between file streams and string streams?

A
File streams connect to external files, string streams work with internal string buffers for parsing and formatting operations
B
They are identical classes
C
String streams can only read files
D
File streams work with strings
24

Question 24

What is the difference between buffered and unbuffered I/O?

A
Buffered I/O collects data in memory before physical transfer for efficiency, unbuffered I/O transfers data immediately for real-time requirements
B
They are identical approaches
C
Unbuffered I/O is always faster
D
Buffered I/O prevents data transfer
25

Question 25

What is the difference between file I/O and memory-mapped I/O?

A
File I/O requires explicit read/write operations, memory-mapped I/O maps file contents directly into process address space for pointer-based access
B
They are identical approaches
C
Memory-mapped I/O is slower
D
File I/O requires mapping
26

Question 26

What is the difference between file streams and C FILE* operations?

A
File streams provide type-safe operations with operator overloading, C FILE* requires manual formatting and buffer management
B
They are identical approaches
C
C FILE* is type-safe
D
File streams require manual buffer management
27

Question 27

What is the difference between stream state flags and error conditions?

A
State flags track stream status (good, eof, fail, bad), error conditions are the reasons for state changes (end-of-file, format errors, I/O failures)
B
They are identical concepts
C
Error conditions are flags
D
State flags cause errors
28

Question 28

What is the difference between file opening and file stream construction?

A
Construction creates stream object, opening associates it with actual file and may involve OS file operations and permission checks
B
They are identical operations
C
Opening creates the stream object
D
Construction opens the file
29

Question 29

What is the difference between stream manipulators and stream member functions?

A
Manipulators are objects that modify stream behavior when inserted/extracted, member functions are called directly on stream objects
B
They are identical approaches
C
Member functions modify behavior
D
Manipulators are called directly
30

Question 30

What is the difference between file I/O performance and memory I/O performance?

A
File I/O involves disk access with mechanical delays, memory I/O is limited by CPU cache and RAM speed with no mechanical delays
B
They have identical performance characteristics
C
File I/O is always faster
D
Memory I/O involves disk access
31

Question 31

What is the difference between file streams and network streams?

A
File streams work with local files, network streams handle remote connections with additional latency, packetization, and connection management
B
They are identical classes
C
Network streams are faster
D
File streams handle remote connections
32

Question 32

What is the difference between stream flushing and stream synchronization?

A
Flushing forces buffered data to be written immediately, synchronization ensures stream state consistency across operations
B
They are identical operations
C
Synchronization forces writing
D
Flushing ensures consistency
33

Question 33

What is the difference between file I/O and console I/O?

A
File I/O works with persistent storage that survives program termination, console I/O is temporary and disappears when program ends
B
They are identical operations
C
Console I/O is persistent
D
File I/O is temporary
34

Question 34

What is the difference between stream position and file offset?

A
Stream position is logical position in stream buffer, file offset is physical position in file on disk
B
They are identical concepts
C
File offset includes buffering
D
Stream position is physical
35

Question 35

What is the difference between file I/O and database I/O?

A
File I/O provides raw data access with manual structure, database I/O provides structured data access with query languages and automatic indexing
B
They are identical approaches
C
Database I/O is slower
D
File I/O provides query languages
36

Question 36

What is the difference between stream inheritance and stream composition?

A
Inheritance creates derived stream classes with extended behavior, composition uses existing streams as members for combined functionality
B
They are identical design patterns
C
Composition requires inheritance
D
Inheritance uses streams as members
37

Question 37

What is the difference between file I/O error handling and network I/O error handling?

A
File I/O errors are usually permanent (disk full, file not found), network I/O errors may be temporary (connection lost, timeout) and worth retrying
B
They are identical approaches
C
Network errors are always permanent
D
File errors are temporary
38

Question 38

What is the difference between stream formatting and data serialization?

A
Stream formatting controls text representation and layout, serialization converts objects to byte streams for storage or transmission
B
They are identical processes
C
Serialization controls text layout
D
Formatting converts objects to bytes
39

Question 39

What is the difference between file I/O performance and stream I/O performance?

A
File I/O performance depends on disk speed and file system, stream I/O performance includes additional overhead of formatting and buffering
B
They are identical concerns
C
Stream I/O is always faster
D
File I/O includes formatting overhead
40

Question 40

What are the fundamental principles for effective file I/O in C++?

A
Use RAII for automatic resource management, check stream states after operations, choose appropriate file modes for data types, handle errors gracefully, and consider buffering trade-offs for performance
B
Never use file I/O in C++
C
Use C-style file operations instead
D
Ignore error checking for performance

QUIZZES IN C++