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
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
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
Explanation: The stringstream, ostringstream, and istringstream objects are used for input and output to a string.
4. What will be the output of the following C++ code?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
ifstream is;
is.open ("sample.txt", ios :: binary );
is.seekg (0, ios :: end);
length = is.tellg();
is.seekg (0, ios :: beg);
buffer = new char [length];
is.read (buffer, length);
is.close();
cout.write (buffer, length);
delete[] buffer;
return 0;
}
a) This is sample
b) sample
c) Error
d) Runtime error
View Answer
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?
#include <iostream>
using namespace std;
int main ()
{
char first, second;
cout << "Enter a word: ";
first = cin.get();
cin.sync();
second = cin.get();
cout << first << endl;
cout << second << endl;
return 0;
}
a) first
b) second
c) returns first 2 letter or number from the entered word
d) third
View Answer
Explanation: In this program, We are using the sync function to return the first two letters of the entered word.
Output:
$ g++ stream.cpp $ a.out Enter a word: steve s t
6. What will be the output of the following C++ code?
#include<iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream outfile ("test.txt");
for (int n = 0; n < 100; n++)
{
outfile << n;
outfile.flush();
}
cout << "Done";
outfile.close();
return 0;
}
a) Done
b) Error
c) Runtime error
d) DoneDoneDone
View Answer
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?
#include <iostream>
using namespace std;
int main ()
{
int a = 100;
double b = 3.14;
cout << a;
cout << endl;
cout << b << endl << a * b;
endl (cout);
return 0;
}
a) 100
b) 3.14
c) 314
d) All of the mentioned
View Answer
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?
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
char ch;
streambuf * p;
ofstream os ("test.txt");
pbuf = os.rdbuf();
do {
ch = cin.get();
p -> sputc(ch);
} while (ch != '.');
os.close();
return 0;
}
a) dot operator
b) insertion operator
c) $ symbol
d) @ symbol
View Answer
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
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
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.
- Practice Programming MCQs
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Check Programming Books