MCQ’s on Files and Streams
1.Which operator is used to insert the data into file?
a) >>
b) <<
c) <
d) >
2.Which function is used to position back from the end of file object?
a) seekg
b) seekp
c) both seekg & seekp
d) seekf
3.How many objects are used for input and output to a string?
a) 1
b) 2
c) 3
d) 4
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
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
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
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
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
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
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>
11 is a sequence of bytes.
Stream
Input stream
Output stream
None of above
12. The source stream that provides data to the program is called
Stream
Input stream
Output stream
None of above
13 The destination stream that receives output from the program is called
Stream
Input stream
Output stream
None of above
14 The data in the input stream can come from the
Keyboard
Monitor
A and b
None of above
15 Which class is the base class for rest of the classes in stream classes?
ios
istream
streambuf
none of above
16 The class ios declared as
Pure virtual base class
Virtual base class
Abstract class
None of above
17 ___________class provides the basic support for formatted and unformatted I/O
operations.
ios
istream
streambuf
ostream
18 The class___________provides the facilities for formatted and unformatted input.
istream
istream_withassign
iostream
streambuf
19 The class provides the facilities for formatted and unformatted output.
ostream
ostream_withassign
iostream_withassign
streambuf
20 istream declares input functions such as
get()
getLine()
read()
all of above
21 ostream declares output functions such as
put()
write()
a and b
none of above
22 iostream inherits the properties of
ios
istream
ostream
all of above
23 istream class and << is overloaded in the class
istream
ostream
ios
none of above
24 The operator reads the data character by character and assigns it to the
indicated location.
>>
<<
c. << >>
d. None of above
25 Which of the following is not a part of ios formatted function?
width()
fill()
setf()
height()
26The header file provides a set of manipulator functions to manipulate
output formats.
stdio
iomanip
A and B
None of above
27 Which stream class is to only write on files ?
ofstream
B. ifstream
C. fstream
D. iostream
28 It is not possible to combine two or more file opening mode in open () method.
TRUE
B. FALSE
C. May Be
D. Can't Say
29 Which of these is the correct statement about eof() ?
A.Returns true if a file open for reading has reached the next character.
B. Returns true if a file open for reading has reached the next word.
C. Returns true if a file open for reading has reached the end.
D. Returns true if a file open for reading has reached the middle.
Which of the following true about FILE *fp
30 Which of the following methods can be used to open a file in file handling?
Using Open ( )
B. Constructor method
C. Destructor method
D. Both A and B
31 Which operator is used to insert the data into file?
A. >>
B. <<
C. <
D. None of the above
32. Which is correct syntax ?
A.myfile:open ("example.bin", ios::out);
B. myfile.open ("example.bin", ios::out);
C. myfile::open ("example.bin", ios::out);
D. myfile.open ("example.bin", ios:out);
33 .What is the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
int l;
char * b;
ifstream i;
i.open ("find.txt", ios :: binary );
i.seekg (0, ios :: end);
l = i.tellg();
i.seekg (0, ios :: beg);
b = new char [l];
i.read (b, l);
i.close();
cout.write (b, l);
delete[] b;
return 0;
A.Error
B. find
C. This is find
D. Runtime error
34. What is the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
char fine, course;
cout << "Enter a word: ";
fine = cin.get();
cin.sync();
course = cin.get();
cout << fine << endl;
cout << course << endl;
return 0;
A.course
B. fine
C. Returns fine 2 letter or number from the entered word
D. None of the mentioned
35. ios::trunc is used for ?
If the file is opened for output operations and it already existed, no action is taken.
B. If the file is opened for output operations and it already existed, then a new copy is
created.
C. If the file is opened for output operations and it already existed, its previous content is
deleted and replaced by the new one.
D. None of the above