C++ Programming Questions and Answers – File Streams and String Streams

This section on C++ interview questions and answers focuses on “File Streams and String Streams”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “File Streams and String Streams” along with answers, explanations and/or solutions:

1. Which operator is used to insert the data into file?
a) >>
b) <<
c) <
d) >
View Answer

Answer: b
Explanation: You can write information to a file from your program using the stream insertion operator <<.

2. Which function is used to position back from the end of file object?
a) seekg
b) seekp
c) both seekg & seekp
d) seekf
View Answer

Answer: a
Explanation: The member function seekg is used to position back from the end of file object.

3. How many objects are used for input and output to a string?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: The stringstream, ostringstream, and istringstream objects are used for input and output to a string.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         int length;
  7.         char * buffer;
  8.         ifstream is;
  9.         is.open ("sample.txt", ios :: binary );
  10.         is.seekg (0, ios :: end);
  11.         length = is.tellg();
  12.         is.seekg (0, ios :: beg);
  13.         buffer = new char [length];
  14.         is.read (buffer, length);
  15.         is.close();
  16.         cout.write (buffer, length);
  17.         delete[] buffer;
  18.         return 0;
  19.     }

a) This is sample
b) sample
c) Error
d) Runtime error
View Answer

Answer: d
Explanation: In this program, if the file exist, it will read the file. Otherwise it will throw an exception. A runtime error will occur because the value of the length variable will be “-1” if file doesn’t exist and in line 13 we are trying to allocate an array of size “-1”.

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         char first, second;
  6.         cout << "Enter a word: ";
  7.         first = cin.get();
  8.         cin.sync();
  9.         second = cin.get();
  10.         cout << first << endl;
  11.         cout << second << endl;
  12.         return 0;
  13.     }

a) first
b) second
c) returns first 2 letter or number from the entered word
d) third
View Answer

Answer: c
Explanation: In this program, We are using the sync function to return the first two letters of the entered word.
Output:

advertisement
$ g++ stream.cpp
$ a.out
Enter a word: steve
s
t

6. What will be the output of the following C++ code?

  1.     #include<iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main () 
  5.     {
  6.         ofstream outfile ("test.txt");
  7.         for (int n = 0; n < 100; n++)
  8.         {
  9.             outfile << n;
  10.             outfile.flush();
  11.         }
  12.         cout << "Done";
  13.         outfile.close();
  14.         return 0;
  15.     }

a) Done
b) Error
c) Runtime error
d) DoneDoneDone
View Answer

Answer: a
Explanation: In this program, We are using the flush function to update the contents in a file.
Output:

$ g++ stream1.cpp
$ a.out
Done

7. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         int a = 100;
  6.         double b = 3.14;
  7.         cout << a;
  8.         cout << endl;
  9.         cout << b << endl << a * b;
  10.         endl (cout);
  11.         return 0;
  12.     }

a) 100
b) 3.14
c) 314
d) All of the mentioned
View Answer

Answer: d
Explanation: In this program, We are printing the given value and manipulating the given value by using endl.
Output:

$ g++ stream2.cpp
$ a.out
100
3.14
314

8. By seeing which operator thus this C++ program stops getting the input?

  1.     #include <iostream>
  2.     #include <fstream>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         char ch;
  7.         streambuf * p;
  8.         ofstream os ("test.txt");
  9.         pbuf = os.rdbuf();
  10.         do {
  11.             ch = cin.get();
  12.             p -> sputc(ch);
  13.         } while (ch != '.');
  14.         os.close();
  15.         return 0;
  16.     }

a) dot operator
b) insertion operator
c) $ symbol
d) @ symbol
View Answer

Answer: a
Explanation: This program will stop getting the input, When it occurs the dot(.) operator.
Output:

$ g++ stream3.cpp
$ a.out
Steve.

9. Which member function is used to determine whether the stream object is currently associated with a file?
a) is_open
b) buf
c) string
d) is_out
View Answer

Answer: a
Explanation: The member function is_open can be used to determine whether the stream object is currently associated with a file.

10. Which header file is used for reading and writing to a file?
a) #include<iostream>
b) #include<fstream>
c) #include<file>
d) #include<fe>
View Answer

Answer: b
Explanation: <fstream> header file contains all the file reading and writing functions. Also <ifstream> and <ofstream> contains functions only reading and only writing to files related functions respectively.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.