C++ Programming Questions and Answers – Output Stream

This section on C++ questions and puzzles focuses 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
View Answer

Answer: b
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

Answer: d
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

Answer: a
Explanation: ofstream is a stream class to write on files.
advertisement
advertisement

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

  1.     #include <iostream>
  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
View Answer

Answer: d
Explanation: In this program, We are printing the five spaces and then we are printing the value of 65.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ ous
.cpp
$ a.out
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;65

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

advertisement
  1.     #include <iostream>
  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
View Answer

Answer: b
Explanation: In this program, We are printing the hexadecimal value of the given decimal number by using hex function.
Output:

advertisement
$ g++ ous1.cpp
$ a.out
2b

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

  1.     #include <fstream>
  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
View Answer

Answer: d
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?

  1.     #include <iostream>
  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
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <locale>
  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
View Answer

Answer: b
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

Answer: c
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

Answer: b
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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.