C++ Programming Multiple Choice Questions - File Handling
This section focuses on the "File Handling" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which stream class is to only write on files ?
View Answer
2. It is not possible to combine two or more file opening mode in open () method.
View Answer
3. Which of these is the correct statement about eof() ?
View Answer
4. Which of the following true about FILE *fp
View Answer
5. Which of the following methods can be used to open a file in file handling?
View Answer
6. Which operator is used to insert the data into file?
View Answer
7. Which is correct syntax ?
View Answer
8. What will be the output of the following program?
Note:Includes all required header files
using namespace std;
int main()
{
ofstream ofile;
ofile.open ("find.txt");
ofile << "letsfindcourse" << endl;
cout << "Data written to file" << endl;
ofile.close();
return 0;
}
View Answer
9. 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;
}
View Answer
10. What is the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
{
ofstream outfile ("find.txt");
for (int i = 0; i < 70; i++)
{
outfile << i;
outfile.flush();
}
cout << "Done";
outfile.close();
return 0;
}
View Answer
11. What is the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
{
int p = 1000;
double q = 3.14;
cout << p;
cout << endl;
cout << q << endl << p * q;
endl (cout);
return 0;
}
View Answer
12. Which of the following is true about the following program
Note:Includes all required header files
using namespace std;
int main ()
{
char i;
streambuf * p;
ofstream of ("find.txt");
pbuf = of.rdbuf();
do {
i = cin.get();
p -> sputc(i);
} while (i != '.');
of.close();
return 0;
}
View Answer
13. What will be the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
{
int p = 10;
double q = 1.14;
cout << p + q;
endl (cout);
return 0;
}
View Answer
14. What will be the output of this program?
Note:Includes all required header files
using namespace std;
int main ()
{
FILE *fp;
char x[1024];
fp = fopen("find.txt", "r"); // "ayushjain and prateek"
x[0] = getc(fp);
fseek(fp, 0, SEEK_END);
fseek(fp, -7L, SEEK_CUR);
fgets(x, 6, fp);
puts(x);
return 0;
}
View Answer
15. If we have object from ofstream class, then default mode of opening the file is _____
View Answer
16. Which is correct syntax for, position n bytes back from end of fileObject ?
View Answer
17. When fopen() is not able to open a file, it returns
View Answer
18. By default, all the files are opened in which of the following mode?
View Answer
19. How many objects are used for input and output to a string?
View Answer
20. Calling the stream's member function sync() causes an immediate synchronization.
View Answer
21. Which of the following is not used to seek a file pointer?
View Answer
22. 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;
}
View Answer
23. 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;
}
View Answer
24. ios::trunc is used for ?
View Answer
25. In fopen(), the open mode "wx" is sometimes preferred "w" because. i) Use of wx is more efficient. ii) If w is used, old contents of file are erased and a new empty file is created. When wx is used, fopen() returns NULL if file already exists.
View Answer
26. Which member function is used to determine whether the stream object is currently associated with a file?
View Answer
27. getc() returns EOF when
View Answer
28. fseek() should be preferred over rewind() mainly because
View Answer
29. Which function is used to position back from the end of file object?
View Answer
Also check :
Discussion