Question 1Medium
eof( )
is the function used for
- asserting no errors in a file
- appending data to a file
- counting the amount of data in a file
- checking for end of file
Answer: D. checking for end of file
Explanation
C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise.
Step-by-step solution
- The eof() function returns true when the input stream has no more data to read.
- So it is used to check for the end of a file.
- This matches option D, checking for end of file.
Why the other options are wrong
- A. eof() does not assert that there are no errors; it checks for the end of input.
- B. Appending is done by opening the file in append mode, not by eof().
- C. Counting data is not what eof() does.
Tip: eof() is a check, not a counter: use it in a loop to know when input has ended.
Question 2Medium
Which function moves the get (read) position of an input file stream, for example back from the end of the file?
- seekg
- seekp
- Both a & b
- None of the mentioned
Answer: A. seekg
Explanation
The member function seekg is used to position back from the end of file object.
Step-by-step solution
- Seekg moves the get pointer, which is the read position of an input stream.
- Seekp moves the put pointer, which is the write position of an output stream.
- Moving back from the end of a file to read is done with seekg.
- The answer is seekg.
Why the other options are wrong
- B. Seekp moves the write position, not the read position.
- C. Only seekg is for reading; seekp is for writing.
- D. Seekg is one of the options, so none of the mentioned is wrong.
Tip: Seekg is for input streams and seekp is for output streams.
Question 3Medium
In the code
fout.open("scores.dat", ios::out);
ios::out is the stream operation mode.fout is the header file reference.ios::out is the stream variable name..fout is the name of the file.
Answer: A. ios::out is the stream operation mode.
Explanation
ios::out If FileName is a new file, then it gets created fine as an empty file.
Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new.
Therefore you can create new data to write to it.
Then,if you save the file, which is the main purpose of this mode, the new content is saved it.This operation is typically used when you want to save a file
Step-by-step solution
- The call is fout.open("scores.dat", ios::out).
- The second argument ios::out is an open mode flag that tells the stream to write.
- So ios::out is the stream operation mode, which is option A.
Why the other options are wrong
- B. Fout is a file stream object, not a header file.
- C. Ios::out is a mode constant, not a variable name.
- D. The file name is "scores.dat"; fout is only the stream variable.
Tip: In open(name, mode), the first argument is the file and the second is the mode, such as ios::in, ios::out or ios::app.
Question 4Medium
Which of the following header files is required for creating and reading data files?
- ofstream.h
- fstream.h
- ifstream.h
- console.h
Answer: B. fstream.h
Explanation
to achieve file operation in c++ ,First of all u need "fstream" header file.So include it
Step-by-step solution
- ofstream.h is for writing files only.
- ifstream.h is for reading files only.
- fstream.h combines both input and output operations.
- console.h is not related to file handling.
- Thus, fstream.h is required for creating and reading data files.
Why the other options are wrong
- A. ofstream.h only handles writing, not reading.
- C. ifstream.h only handles reading, not writing.
- D. console.h is for console input/output, not file operations.
Tip: Use fstream.h for any file operation that involves both reading and writing.
Question 5Medium
Which operator is used to insert the data into file?
- >>
- <<
- <
- none of the mentioned
Answer: B. <<
Explanation
You can write information to a file from your program using the stream insertion operator <<.
Step-by-step solution
- Writing data to a file uses the stream insertion operator.
- The insertion operator is written as << and sends data into the stream.
- Writing to a file with an output file stream uses the same operator.
- So the answer is option B, <<.
Why the other options are wrong
- A. >> is the extraction operator, used for reading data.
- C. < is a comparison operator, not a stream operator.
- D. Option B is the insertion operator, so 'none of the mentioned' is wrong.
Tip: << inserts data into an output stream such as cout or a file; >> extracts data from an input stream.
Question 6Medium
In order, the three-step process of using a file in a C++ program involves:
- Name the file, open the file, and delete the file
- Insert a diskette, open a file, and remove the diskette
- Open the existing file, read/write data, close the file
- Create the file contents, close the file, and name the file
Answer: C. Open the existing file, read/write data, close the file
Explanation
A file must be opened before you can read from it or write to it.
Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Step-by-step solution
- Open the existing file to access its data.
- Read/write data to modify or retrieve information.
- Close the file to save changes and free resources.
Why the other options are wrong
- A. Deleting a file is not part of standard file handling steps.
- B. Inserting/removing diskettes is irrelevant in modern C++ file operations.
- D. Creating file contents is not a required step before opening a file.
Tip: Always open, use, then close files in C++.