Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Files & Streams Aptitude Questions and Answers
C++ Files & Streams Aptitude: This section contains C++ Files & Streams Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 22, 2021
1) Which of the following header file is used for file handling in C++?
- <file>
- <filestream>
- <stream>
- <fstream>
Correct Answer - 4
<fstream>
Explanation:
The <fstream> header file is used for file handling in C++.
2) Which of the following classes are used for file handling in C++?
- fstream
- ifstream
- ofstream
- ostream
Options:
- A and B
- A and D
- A, B, and C
- A, B, C, and D
Correct Answer - 3
A, B, and C
Explanation:
The fstream, ifstream, and ofstream classes are used for file handling in C++.
3) Which of the following statements are correct about the fstream class in C++?
- This is used to read data from the file.
- This is used to write data into the file
- This is used to create a file.
- This is used to delete a file.
Options:
- A and B
- A and D
- A, B, and C
- A, B, C, and D
Correct Answer - 3
A, B, and C
Explanation:
Statements A, B, and C are correct about the fstream class in C++.
4) Which of the following modes are used to open a file in C++?
- ios::in
- ios::out
- ios::app
- ios::trunc
Options:
- A and B
- A and D
- A, B, and C
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All given options are correct modes that are used to open a file.
5) Which of the following mode is used to open a file and move the read and write control to the end of the file?
- ios::ate
- ios::in
- ios::out
- ios::trunc
Correct Answer - 1
ios::ate
Explanation:
The "ios:ate" mode is used to open a file and move the read and write control to the end of the file.
6) Which of the following mode is used to open an existing file and remove its content before opening the file?
- ios::ate
- ios::in
- ios::out
- ios::trunc
Correct Answer - 4
ios::trunc
Explanation:
The "ios:trunc" mode is used to open an existing file and remove its content before opening the file.
7) Which of the following function is used to check a file is opened or not?
- isopen()
- is_open()
- isopened()
- None of the above
Correct Answer - 2
is_open()
Explanation:
The "is_open()" function is used to check the file is opened or not.
8) What is return type of "is_open()" function?
- int
- bool
- char
- short
Correct Answer - 2
bool
Explanation:
The return type of "is_open()" is bool.
9) Which of the following mode is used to open a file in binary mode?
- ios::bin
- ios::binary
- ios::bins
- None of the above
Correct Answer - 2
ios::binary
Explanation:
The "ios::binary" mode is used to open a file in binary mode.
10) Which of the following function is used to check no space left in the device?
- nospace()
- isspace()
- bad()
- ismem()
Correct Answer - 3
bad()
Explanation:
The bad() function is used to check no space left in the device.
11) Which of the following are correct functions used to handle file streams?
- bad()
- good()
- fail()
- pass()
Options:
- A and B
- A and C
- A, B, and C
- A, B, C, and D
Correct Answer - 3
A, B, and C
Explanation:
Options A, B, and C are correct functions used to handle file streams.
12) Which of the following function is used to reset the state flags?
- readline()
- getline()
- line()
- rline()
Correct Answer - 2
getline()
Explanation:
The getline() function is used to read a line from the specified file.
13) We can write data into the file using an insertion operator?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can write data into the file using an insertion operator (<<).
14) Which of the following function is used to reset the state flags?
- reset()
- resetflags()
- delete()
- clear()
Correct Answer - 4
clear()
Explanation:
The clear() function is used to reset the state flags.
15) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fs;
char data[16];
fs.open("abc.txt");
fs << "hello world" << endl;
getline(fs, data);
fs.close();
cout << data << endl;
return 0;
}
Options:
- hello world
- no output
- syntax error
- Runtime error
Correct Answer - 3
syntax error
Explanation:
The above code will generate a syntax error.
16) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fs;
string data;
fs.open("abc.txt");
fs << "hello world" << endl;
getline(fs, data);
fs.close();
cout << data << endl;
return 0;
}
Options:
- hello world
- no output
- syntax error
- Runtime error
Correct Answer - 2
no output
Explanation:
The above code will not print any output on the console screen.
17) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs;
ifstream ifs;
string data;
ofs.open("abc.txt");
ofs << "hello world" << endl;
getline(ifs, data);
ifs.close();
cout << data << endl;
return 0;
}
Options:
- hello world
- no output
- Syntax error
- Runtime error
Correct Answer - 2
no output
Explanation:
The above code will not print any output on the console screen.
18) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs;
ifstream ifs;
string data;
ofs.open("abc.txt");
ofs << "hello world" << endl;
ofs.close();
ifs.open("abc.txt");
getline(ifs, data);
ifs.close();
cout << data << endl;
return 0;
}
Options:
- hello world
- no output
- Syntax error
- Runtime error
Correct Answer - 1
hello world
Explanation:
The above code will print "hello world" on the console screen.
19) Which of the following functions are used for pointer positioning in a file?
- seekp()
- seekg()
- tellp()
- tellg()
Options:
- A and B
- A and C
- C and D
- A, B, C, and D
Correct Answer - 4
A, B, C, and D
Explanation:
All given functions are used for pointer positioning in a file.
20) Which of the following flags are used for pointer positioning in a file?
- ios::start
- ios::current
- ios::end
- ios::middle
Options:
- A and B
- A and C
- C
- A, C, and D
Correct Answer - 3
C
Explanation:
In the given options "ios:end" flag is used for pointer positioning in a file.
21) Which of the following pointer positioning flags is used to move to get and put pointer at the starting of the file?
- ios::str
- ios::beg
- ios::begninig
- None of the above
Correct Answer - 2
ios::beg
Explanation:
The 2nd option is used to correct pointer positioning flags is used to move to get and put pointer at the starting of file.
22) Which of the following function is used to write a single character into a file?
- write()
- put()
- set()
- putchr()
Correct Answer - 2
put()
Explanation:
The put() function is used to write a single character into a file.
23) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs;
ofs.open("abc.txt");
ofs << "hello world" << endl;
cout << ofs.tellg() << endl;
ofs.close();
return 0;
}
Options:
- 0
- 1
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
24) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs;
ofs.open("abc.txt");
ofs << "hello world" << endl;
cout << ofs.tellp() << endl;
ofs.close();
return 0;
}
Options:
- 11
- 12
- 13
- Syntax error
Correct Answer - 3
13
Explanation:
The code will print "13" on the console screen.
25) What is correct output of given code snippets?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs;
ofs.open("abc.txt");
ofs << "hello world";
cout << ofs.tellp() << endl;
ofs.close();
return 0;
}
Options:
- 11
- 12
- 13
- Syntax error
Correct Answer - 1
11
Explanation:
The code will print "11" on the console screen.
26) Which of the following function is used to read a single character from a file?
- read()
- get()
- getchr()
- readchr()
Correct Answer - 2
get()
Explanation:
The get() function is used to read a single character from a file.