CPP File Handling & Standard Template Library (STL) 3 — Questions and Answers
Question 1: What is the effect of opening a file with std::ios::app mode?
- Reads from the end of the file
- All writes go to the end of the file regardless of seekp() (Correct answer)
- Truncates the file to zero length
- Opens the file in binary mode
Correct answer: All writes go to the end of the file regardless of seekp()
In append mode, every write operation automatically moves the put pointer to end-of-file before writing.
Question 2: Which STL container adaptor uses a std::deque as its default underlying container?
- std::priority_queue
- std::stack
- std::queue
- Both std::stack and std::queue (Correct answer)
Correct answer: Both std::stack and std::queue
Both std::stack and std::queue default to std::deque as their underlying container.
Question 3: What does std::lower_bound() return when no element satisfies the condition?
- nullptr
- The iterator to the last element
- The end iterator of the range (Correct answer)
- An iterator to the first element
Correct answer: The end iterator of the range
std::lower_bound returns the end iterator if all elements are less than the value.
Question 4: Which method checks whether a std::fstream has reached end-of-file?
- std::fstream::fail()
- std::fstream::eof() (Correct answer)
- std::fstream::bad()
- std::fstream::good()
Correct answer: std::fstream::eof()
eof() returns true when the eofbit is set, indicating end-of-file has been reached.
Question 5: What is the time complexity of std::list::splice()?
- O(n)
- O(log n)
- O(1) (Correct answer)
- O(n log n)
Correct answer: O(1)
std::list::splice() moves elements between lists in O(1) by relinking pointers.
Question 6: Which STL iterator category supports both increment and decrement but NOT random access?
- Forward iterator
- Input iterator
- Bidirectional iterator (Correct answer)
- Output iterator
Correct answer: Bidirectional iterator
Bidirectional iterators support ++ and -- but not arbitrary +n or -n arithmetic.
Question 7: What does std::transform() do?
- Removes elements matching a predicate
- Applies a function to a range and stores results in another range (Correct answer)
- Sorts a range using a custom comparator
- Finds the first element satisfying a condition
Correct answer: Applies a function to a range and stores results in another range
std::transform applies a unary or binary operation to each element and writes results to an output range.
What is the effect of opening a file with std::ios::app mode?