CPP File Handling & Standard Template Library (STL) 1 — Questions and Answers
Question 1: Which header file is required for file handling in C++?
- <iostream>
- <fileio>
- <fstream> (Correct answer)
- <stdio.h>
Correct answer: <fstream>
The `<fstream>` header file in C++ provides classes for file stream operations, including `ifstream` for input from files, `ofstream` for output to files, and `fstream` for both. It is essential for performing various file I/O operations in C++.
Question 2: Which stream is used to write to a file?
- ifstream
- fstream
- ofstream (Correct answer)
- istream
Correct answer: ofstream
`ofstream` (output file stream) is the class specifically designed for writing data to files in C++. You create an `ofstream` object, associate it with a file, and then use stream insertion operators (`<<`) to write data to that file.
Question 3: Which STL container stores key-value pairs?
- vector
- list
- map (Correct answer)
- queue
Correct answer: map
The `std::map` container in the C++ Standard Template Library (STL) stores elements as key-value pairs, where each key is unique and associated with a specific value. It provides efficient lookup, insertion, and deletion operations based on the keys, maintaining sorted order.
Question 4: Which operation checks if a file stream opened successfully?
- file.read()
- file.good()
- file.is_open() (Correct answer)
- file.exists()
Correct answer: file.is_open()
The `is_open()` member function of file stream objects (like `ifstream`, `ofstream`, `fstream`) is used to check if the file stream successfully opened a file. It returns `true` if the file is open and ready for I/O operations, and `false` otherwise, allowing for error handling.
Question 5: Which STL container allows access in constant time using indices?
- list
- deque
- vector (Correct answer)
- stack
Correct answer: vector
`std::vector` is a dynamic array in the C++ STL that provides constant-time (O(1)) access to its elements using their integer indices. This efficiency is due to elements being stored contiguously in memory, allowing direct calculation of an element's address.
Question 6: Which function is used to read data from a file line by line?
- get()
- readline()
- getline() (Correct answer)
- input()
Correct answer: getline()
The `std::getline()` function is used to read an entire line from an input stream (like a file stream or `std::cin`) until a newline character is encountered or the end of the stream is reached. It stores the read line into a `std::string` object, making it ideal for line-by-line file processing.
Question 7: Which STL algorithm is used to sort elements?
- arrange()
- order()
- sort() (Correct answer)
- shuffle()
Correct answer: sort()
The `std::sort()` algorithm from the `<algorithm>` header is a powerful and efficient function in the C++ STL used to sort elements within a specified range. It can sort various container types and allows for custom comparison functions to define specific sorting criteria.
Question 8: What is required to use STL containers like vector or map?
- #include <stl>
- #include <container>
- #include <vector> (Correct answer)
- #include <library>
Correct answer: #include <vector>
To use specific STL containers like `std::vector`, `std::map`, `std::list`, etc., you must include their respective header files. For `std::vector`, the correct header is `<vector>`, which provides the necessary class definition and associated functionalities.
Question 9: Which stream is used to read from a file?
- ofstream
- istream
- ifstream (Correct answer)
- fstreamwrite
Correct answer: ifstream
`ifstream` (input file stream) is the class specifically designed for reading data from files in C++. You create an `ifstream` object, associate it with a file, and then use stream extraction operators (`>>`) or `getline()` to read data from that file.
Which header file is required for file handling in C++?