C++ File Handling Questions Answers

  • 1. Which stream class is to only write on files ?

    1. ofstream
    2. ifstream
    3. fstream
    4. iostream
    Answer And Explanation

    Answer: Option A

  • 2. Which stream class is to only read from files ?

    1. ofstream
    2. ifstream
    3. fstream
    4. iostream
    Answer And Explanation

    Answer: Option B

  • 3. Which stream class is used to both read and write on files ?

    1. ofstream
    2. ifstream
    3. fstream
    4. iostream
    Answer And Explanation

    Answer: Option C

  • 4. Which among following is used to open a file in binary mode ?

    1. ios:app
    2. ios::out
    3. ios::in
    4. ios::binary
    Answer And Explanation

    Answer: Option D

    Explanation:

    ios:app -> All output operations are performed at the end of the file, appending the content to the current content of the file.

    ios::out -> Open for output operations.

    ios::in -> Open for input operations.

  • 5. ios::trunc is used for ?

    1. If the file is opened for output operations and it already existed, no action is taken.
    2. If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.
    3. If the file is opened for output operations and it already existed, then a new copy is created.
    4. None of above
    Answer And Explanation

    Answer: Option B

  • 6. Which is correct syntax ?

    1. myfile:open ("example.bin", ios::out);
    2. myfile.open ("example.bin", ios::out);
    3. myfile::open ("example.bin", ios::out);
    4. myfile.open ("example.bin", ios:out);
    Answer And Explanation

    Answer: Option B

  • 7. Which among following is correct syntax of closing a file in c++ ?

    1. myfile$close();
    2. myfile@close();
    3. myfile:close();
    4. myfile.close();
    Answer And Explanation

    Answer: Option D