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
Answer: b
Clarification: 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
Answer: d
Clarification: 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
Answer: a
Clarification: ofstream is a stream class to write on files.
4. What will be the output of the following C++ code?
-
#include
-
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
Answer: d
Clarification: 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
-
using namespace std;
-
int main ()
-
{
-
int n;
-
n = 43;
-
cout << hex << n << endl;
-
return 0;
-
}
a) 2c
b) 2b
c) 20
d) 50
Answer: b
Clarification: 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