C++ MCQs with Explanation (Working with files)

Working with Files

1. All file stream classes are derived from

Explanation ≡ Correct answer: d) ios
The file stream classes are derived from the `ios` class. The `ios` class is the base class for handling input and output operations for files in C++. Option a) is incorrect because `filebuf` is a base class for handling the stream buffer for file streams, but not all file stream classes are derived from it. Option b) is incorrect because `fstream` is a derived class itself, not the base class for other file stream classes. Option c) is incorrect because `fstreambase` is not a valid class in C++. Therefore, the correct answer is d) ios.

2. Which of the following classes support simultaneous input and output file operations?

Explanation ≡ Correct answer: c) filebuf
The `filebuf` class is used to support simultaneous input and output file operations. It provides the underlying stream buffer for file streams. Options a) and b) are incorrect because `iostream` and `fstream` are not classes that support simultaneous input and output file operations. Option d) is incorrect because the `filebuf` class does support simultaneous input and output file operations. Therefore, the correct answer is c) filebuf.

3. Which of the following is TRUE?

Explanation ≡ Correct answer: d) Searching all records in a random-access file to locate a record is not necessary
Option a) is incorrect because you do not always need to call the `close()` function explicitly to close a file associated with an `fstream` object. The file will be automatically closed when the object is destructed or when the program ends. Option b) is incorrect because not all sequential files are updated without overwriting any data. It depends on the specific file update operation and how the file is opened. Option c) is incorrect because you can open an existing file for both reading and writing (input/output) purpose, not just for writing only. Therefore, the correct answer is d) Searching all records in a random-access file to locate a record is not necessary.

4. Which of the following is FALSE?

Explanation ≡ Correct answer: d) The open ( ) can take two arguments
Option a) is incorrect because files can be opened using the `open()` function in addition to the constructor function of the stream class. Option b) is incorrect because streams can support both input and output operations. Some streams can be used for both input and output, and some are specialized for only input or output. Option c) is incorrect because the `open()` function is used to open a single file associated with a stream. It cannot be used to open multiple files using one stream. Therefore, the correct answer is d) The open ( ) can take two arguments.

5. When reading a file that is connected to the input stream fin of ifstream, we can detect the end-of-file condition using

Explanation ≡ Correct answer: c) either of them
When reading a file using the input stream `fin` of `ifstream`, you can detect the end-of-file condition in two ways: Option a) using a `while (fin)` statement: The `while (fin)` statement evaluates to true as long as there are more characters to read from the file. It becomes false when the end-of-file condition is reached. Option b) using an `if (fin.eof() != 0)` statement: The `eof()` function of the input stream returns a non-zero value (true) when the end-of-file condition is reached. Therefore, either of these options can be used to detect the end-of-file condition.

6. To open an existing file to add data to the end of the file or to modify the existing data anywhere in the file, the second argument of open ( ) must be

Explanation ≡ Correct answer: b) ios :: app
To open an existing file in C++ to add data to the end of the file or modify the existing data anywhere in the file, you must use the second argument of the `open()` function with the `ios::app` flag. Option a) `ios :: ate` is used to open an existing file and set the file pointer to the end of the file, but it does not append data. It allows both reading and writing. Option c) `ios :: trunc` is used to open an existing file and truncate its content, erasing any existing data. Option d) `ios :: noreplace` is not a valid flag for the `open()` function. Therefore, the correct answer is b) ios :: app.

7. To write floating-point data to a file, we must use

Explanation ≡ Correct answer: d) seekp ( ) function
To write floating-point data to a file, you must use the `seekp()` function. The `seekp()` function sets the position of the output file pointer. Option a) `put()` function is used to write a single character to a file. Option b) `write()` function is used to write raw data to a file, but it requires a pointer to the data buffer. Option c) The insertion operator (`<<`) is used for formatted output, but it does not specifically handle floating-point data. Therefore, the correct answer is d) seekp ( ) function.

8. The statement `file1.write((char*)&M, sizeof(M));` writes

Explanation ≡ Correct answer: d) data in object M to file1
The statement `file1.write((char*)&M, sizeof(M));` writes the binary data contained in the object `M` to the file `file1`. Option a) The expression `(char*)&M` is used to cast the address of the object `M` to a pointer to a character (`char*`). Writing the address to a file would be inappropriate. Option b) Writing the functions of `M` to the file would not make sense in this context. Option c) Writing all the members of `M` to the file would not be appropriate for binary data. Therefore, the correct answer is d) data in object M to file1.

9. Assuming that the length of the file `f1` is 100 bytes, what will be the output of the following code segment?

f1.seekg(0, ios::end);
f1.seekg(-10, ios::cur);
cout << f1.tellg();
Explanation ≡ Correct answer: c) 90
The code segment uses the `seekg()` function to set the get (input) file pointer to specific positions within the file. Here's the step-by-step explanation of the code: 1. `f1.seekg(0, ios::end);`: The `seekg()` function with the second argument `ios::end` sets the get pointer to the end of the file `f1`. 2. `f1.seekg(-10, ios::cur);`: The second `seekg()` function with the second argument `ios::cur` sets the get pointer 10 bytes back from the current position. Since we are already at the end of the file, this moves the pointer 10 bytes backward. 3. `cout << f1.tellg();`: The `tellg()` function returns the current position of the get pointer. Since the pointer is now 10 bytes back from the end, the output will be 90. Therefore, the correct answer is c) 90.

10. Any fatal error occurring during a file operation can be located by using the function

Explanation ≡ Correct answer: d) good()
The function `good()` is used to check if any fatal error occurred during a file operation. The `good()` function returns `true` if no fatal error occurred and `false` otherwise. Option a) The `fail()` function checks if a logical error occurred during the file operation, but it does not handle fatal errors. Option b) The `bad()` function checks if a fatal error occurred during the file operation, but it is not used for error handling. Option c) The `eof()` function checks if the end-of-file condition is reached, but it does not handle fatal errors. Therefore, the correct answer is d) good().


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. THE EXPLANATIONS ARE IN SIMPLE ENGLISH AND THEY PROVIDE EXERCISES TO MAKE YOUR LEARNING EXCELLENT.

Topic wise C++ MCQs Index ≡
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments