250+ TOP MCQs on Output Stream and Answers

This section on C++ questions and puzzles on “Output Stream”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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?

  1.     #include 
  2.     using namespace std;
  3.     int main () 
  4.     {
  5.         char str[] = "Steve jobs";
  6.         int val = 65;
  7.         char ch = 'A';
  8.         cout.width (5);
  9.         cout << right;
  10.         cout << val << endl;
  11.         return 0;
  12.     }

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
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65

5. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int main () 
  4.     {
  5.         int n; 
  6.         n = 43;
  7.         cout << hex << n << endl;
  8.         return 0;
  9.     }

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

6. What is the output of this C++ program in the “test.txt” file?

  1.     #include 
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         long pos;
  6.         ofstream outfile;
  7.         outfile.open ("test.txt");
  8.         outfile.write ("This is an apple",16);
  9.         pos = outfile.tellp();
  10.         outfile.seekp (pos - 7);
  11.         outfile.write (" sam", 4);
  12.         outfile.close();
  13.         return 0;
  14.     }

a) This is an apple
b) apple
c) sample
d) This is a sample
Answer: d
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         int n;
  6.         n = -77;
  7.         cout.width(4); 
  8.         cout << internal << n << endl;
  9.         return 0;
  10.     }

a) 77
b) -77
c) – 77
d) None of the mentioned
Answer: c
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         locale mylocale("");
  7.         cout.imbue( mylocale );
  8.         cout << (double) 3.14159 << endl;
  9.         return 0;
  10.     }

a) 3.14
b) 3.14159
c) Error
d) 3.69
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: If you construct an object of class ostream, you must specify a streambuf object to the constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *