C++ File Handling Questions Answers
-
1. Which stream class is to only write on files ?
- ofstream
- ifstream
- fstream
- iostream
Answer And Explanation
Answer: Option A
-
2. Which stream class is to only read from files ?
- ofstream
- ifstream
- fstream
- iostream
Answer And Explanation
Answer: Option B
-
3. Which stream class is used to both read and write on files ?
- ofstream
- ifstream
- fstream
- iostream
Answer And Explanation
Answer: Option C
-
4. Which among following is used to open a file in binary mode ?
- ios:app
- ios::out
- ios::in
- 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 ?
- If the file is opened for output operations and it already existed, no action is taken.
- If the file is opened for output operations and it already existed, its previous content is deleted and replaced by the new one.
- If the file is opened for output operations and it already existed, then a new copy is created.
- None of above
Answer And Explanation
Answer: Option B
-
6. Which is correct syntax ?
- myfile:open ("example.bin", ios::out);
- myfile.open ("example.bin", ios::out);
- myfile::open ("example.bin", ios::out);
- 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++ ?
- myfile$close();
- myfile@close();
- myfile:close();
- myfile.close();
Answer And Explanation
Answer: Option D