Here is a listing of C++ questions and puzzles on “Output Stream” along with answers, explanations and/or solutions:
1. How many groups of output of operation are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are two groups of output operation in c++. They are formatted output and unformatted output.
2. Pick out the correct objects about the instantiation of output stream.
a) cout
b) cerr
c) clog
d) all of the mentioned
View Answer
Explanation: cout, cerr and clog are the standard objects for the instantiation of output stream class.
3. What is meant by ofstream in c++?
a) Writes to a file
b) Reads from a file
c) Writes to a file & Reads from a file
d) delete a file
View Answer
Explanation: ofstream is a stream class to write on files.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
char str[] = "Steve jobs";
int val = 65;
char ch = 'A';
cout.width (5);
cout << right;
cout << val << endl;
return 0;
}
a) Steve jobs
b) A
c) 65
d) 65
View Answer
Explanation: In this program, We are printing the five spaces and then we are printing the value of 65.
Output:
$ g++ ous .cpp $ a.out 65
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int n;
n = 43;
cout << hex << n << endl;
return 0;
}
a) 2c
b) 2b
c) 20
d) 50
View Answer
Explanation: In this program, We are printing the hexadecimal value of the given decimal number by using hex function.
Output:
$ g++ ous1.cpp $ a.out 2b
6. What is the output of this C++ program in the “test.txt” file?
#include <fstream>
using namespace std;
int main ()
{
long pos;
ofstream outfile;
outfile.open ("test.txt");
outfile.write ("This is an apple",16);
pos = outfile.tellp();
outfile.seekp (pos - 7);
outfile.write (" sam", 4);
outfile.close();
return 0;
}
a) This is an apple
b) apple
c) sample
d) This is a sample
View Answer
Explanation: In this program, We are changing the ap to sam by using the pos function.
Output:
$ g++ ous2.cpp $ a.out This is a sample
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int n;
n = -77;
cout.width(4);
cout << internal << n << endl;
return 0;
}
a) 77
b) -77
c) – 77
d) None of the mentioned
View Answer
Explanation: In this program, We are using the internal function and moving the 77 to one position.
Output:
$ g++ ous3.cpp $ a.out - 77
8. What will be the output of the following C++ code?
#include <iostream>
#include <locale>
using namespace std;
int main()
{
locale mylocale("");
cout.imbue( mylocale );
cout << (double) 3.14159 << endl;
return 0;
}
a) 3.14
b) 3.14159
c) Error
d) 3.69
View Answer
Explanation: In this program, We are using the locale and then assigning the type to the value.
Output:
$ g++ ous4.cpp $ a.out 3.14159
9. How many types of output stream classes are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are three output stream classes in c++. They are ostream, ofstream and ostrstream.
10. What must be specified when we construct an object of class ostream?
a) stream
b) streambuf
c) memory
d) steamostream
View Answer
Explanation: If you construct an object of class ostream, you must specify a streambuf object to the constructor.
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]
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check Programming Books