Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 13 File Input and Output


Section 13.2 Simple Text I/O
13.1  To write data to a file, you create an instance of _________.
A. iostream
B. ifstream
C. ofstream
D. stream

13.2  To read data from a file, you create an instance of _________.
A. iostream
B. ifstream
C. ofstream
D. stream

13.3  What do the following statements do?

  ofstream stream;
  stream.open("scores.txt");
A. Open a file for input.
B. Open a file for output, the statement fails if the file already exists.
C. Open a file for output, the contents of the file is destroyed if the file already exists.
D. Open a file for input, the statement fails if the file does not exist.

13.4  What do the following statements do?

  ifstream stream;
  stream.open("scores.txt");
A. Open a file for input.
B. Open a file for output, the statement fails if the file already exists.
C. Open a file for output, the contents of the file is destroyed if the file already exists.
D. Open a file for input, the statement fails if the file does not exist.

13.5  What is the purpose of invoking the close function?
A. If this function is not invoked, the data may not be saved properly in the file.
B. If this function is not invoked, the file may be deleted.

13.6  To open a file in c:\example\scores.txt on Windows, you use _________.
A. stream.open("c:\example\scores.txt");
B. stream.open("scores.txt");
C. stream.open("\example\scores.txt");
D. stream.open("c:\\example\\scores.txt");

13.7  What is the output of the following code?

  #include <iostream>
  #include <fstream>
  using namespace std;
  
  int main()
  {
    ofstream output;
  
    // Create a file
    output.open("scores.txt");
  
    // Write two lines
    output << "John" << " " << "T" << " " << "Smith"
      << " " << 90 << endl;
    output << "Eric" << " " << "K" << " " << "Jones"
      << " " << 85;
  
    output.close();
  
    ifstream input;
  
    // Open a file
    input.open("scores.txt");
  
    // Read data
    char firstName[80];
    char mi;
    char lastName[80];
    int score;
    input >> firstName >> mi >> lastName >> score;
    double sum = score;
  
    input >> firstName >> mi >> lastName >> score;
    sum += score;
  
    cout << "Total score is " << sum << endl;
  
    input.close();
  
    return 0;
  }
A. Total score is 90
B. Total score is 85
C. Total score is 175
D. Total score is 0

13.8  Suppose the file scores.txt does not exist. What will be displayed by the following code?

  // Open a file
  input.open("scores.txt");

  if (input.fail())
  {
    cout << "File does not exist" << endl;
    return 0;
  }
A. File does not exist
B. Nothing printed

13.9  Is there a special character stored in a file to denote the end of file?
A. Yes.
B. No.

Section 13.4 Member Functions: getline, get and put
13.10  Which of the following statements are true?
A. The stream.getline(char array[], int size, char delimitChar) function reads data to array. It stops reading characters when the delimiter character or end-of-file mark is encountered, or when the size - 1 number of characters are read.
B. The stream.get() function reads one character.
C. The stream.put(ch) function writes one character.

Section 13.5 fstream and File Open Modes
13.11  You can open a file using the following modes:
A. ios::in
B. ios::out
C. ios::app
D. ios::ate
E. ios::binary

13.12  You can combine modes using the _________ operator.
A. +
B. |
C. ||
D. &
E. &&

Section 13.6 Testing Stream States
13.13  To know whether it is the end of a file, you use the function ________.
A. stream.eof()
B. stream.fail()
C. stream.bad()
D. stream.good()
E. stream.clear()

13.14  To know whether the I/O operation succeeded, you use the function ________.
A. stream.eof()
B. stream.fail()
C. stream.bad()
D. stream.good()
E. stream.clear()

Section 13.7 Binary I/O
13.15  Which of the following statements are true?
A. Computers do not differentiate binary files and text files. All files are stored in binary format, and thus all files are essentially binary files.
B. Text I/O is built upon binary I/O to provide a level of abstraction for character encoding and decoding.
C. The C++ source programs are stored in text files and can be read by a text editor.
D. The C++ executable files are stored in binary files and are read by the operating system.

13.16  To open a file for binary input, use the mode _______.
A. ios::in | ios::binary
B. ios::out | ios::binary
C. ios::app | ios::binary
D. ios::ate | ios::binary
E. ios::binary | ios::in

13.17  To write a character string to a binary file, use the function _______.
A. biStream.write(char* address, int size)
B. biStream.write(char* address)
C. biStream.write(string address, int size)
D. biStream.write(string address)

13.18  Suppose you declare int value = 99, to write it to a binary file, use ________.
A. binaryio.write(value);
B. binaryio.write(reinterpret_cast<char*>(&value), sizeof(value));
C. binaryio.write(reinterpret_cast<char*>(&value));
D. binaryio.write(reinterpret_cast<char*>(value));
E. binaryio.write(reinterpret_cast<char*>(value), sizeof(value));

13.19  This book uses .dat to denote _______ file.
A. text
B. binary
C. source
D. input
E. output

13.20  To read a character string from a binary file, use the function _______.
A. biStream.read(char* address, int size)
B. biStream.read(char* address)
C. biStream.read(string address, int size)
D. biStream.read(string address)

13.21  Suppose you want to read an int to the variable value from a binary file, use ______.
A. value = biStream.read();
B. biStream.read(value);
C. binaryio.read(reinterpret_cast<char*>(&value), sizeof(value));
D. biStream.read(&value);

13.22  Suppose you declare Student student1. To write student1 to a binary file, use _____.
A. binaryio.write(&student1, sizeof(Student));
B. binaryio.write(student1);
C. binaryio.write(&student1);
D. binaryio.write(reinterpret_cast<char*>(&student1), sizeof(Student));

13.23  Suppose you declare Student student1. To read a Student object from a binary file, use _____.
A. binaryio.read(&studentNew, sizeof(Student));
B. binaryio.read(&studentNew);
C. binaryio.read(reinterpret_cast<char*>(& studentNew), sizeof(Student));
D. binaryio.read(reinterpret_cast<char*>(studentNew), sizeof(Student));

Section 13.8 Random Access File
13.24  You can use the _________ function to move the file pointer for output.
A. stream.seekg(length);
B. stream.seekp(length);
C. stream.tellg();
D. stream.tellp();

13.25  You can use the _________ function to move the file pointer for input.
A. stream.seekg(length);
B. stream.seekp(length);
C. stream.tellg();
D. stream.tellp();

13.26  The functions seekg and seekp may have two arguments. The first argument is the offset and the second argument may be __________ to indicate the base for the offset.
A. ios::beg
B. ios::end
C. ios::cur
D. ios::now

13.27  Can you open a file for both input and output?
A. Yes
B. No