C++ Programming Questions and Answers – Input Stream

This section on C++ Programming Multiple Choice Questions focuses on “Input Stream”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions 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 comes with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ Programming Questions & Answers focuses on “Input Stream” along with answers, explanations and/or solutions:

1. Which operator is used for input stream?
a) >
b) >>
c) <
d) <<
View Answer

Answer: b
Explanation: The operator of extraction is >> and it is used on the standard input stream.

2. Where does a cin stops it extraction of data?
a) By seeing a blank space
b) By seeing (
c) By seeing a blank space & (
d) By seeing <
View Answer

Answer: a
Explanation: cin will stop its extraction when it encounters a blank space.

3. Which is used to get the input during runtime?
a) cout
b) cin
c) coi
d) cinout
View Answer

Answer: b
Explanation: cin is mainly used to get the input during the runtime.
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.         int i;
  6.         cout << "Please enter an integer value: ";
  7.         cin >> i + 4;
  8.         return 0;
  9.     }

a) 73
b) your value + 4
c) Error
d) 63
View Answer

Answer: c
Explanation: We are not allowed to do addition operation on cin.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
  1.     #include <iostream>
  2.     #include <string>
  3.     #include <sstream>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         string mystr;
  8.         float price = 0;
  9.         int quantity = 0;
  10.         cout << "Enter price: ";
  11.         getline (cin, mystr);
  12.         stringstream(mystr) >> price;
  13.         cout << "Enter quantity: ";
  14.         getline (cin, mystr);
  15.         stringstream(mystr) >> quantity;
  16.         cout << "Total price: " << price * quantity << endl;
  17.         return 0;
  18.     }

a) 50
b) Depends on value you enter
c) Error
d) 100
View Answer

Answer: b
Explanation: In this program, We are getting the input on runtime and manipulating the value.
Output:

advertisement
$ g++ inp.cpp
$ a.out
Enter price: 3
Enter quantity: 4
Total price: 12

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

  1.     #include <iostream>
  2.     #include <ios>
  3.     #include <istream>
  4.     #include <limits>
  5.     using namespace std;
  6.     template <typename CharT>
  7.     void ignore_line ( basic_istream<CharT>& in )
  8.     {
  9.         in.ignore ( numeric_limits<streamsize> :: max(), in.widen ( '\n' ) );
  10.     }
  11.     int main()
  12.     {
  13.         cout << "First input: ";
  14.         cin.get();
  15.         cout << "Clearing cin.\n";
  16.         cin.clear();
  17.         ignore_line ( cin );
  18.         cout << "All done.\n";
  19.     }

a) First input
b) Clearing cin
c) Error
d) Second input
View Answer

Answer: d
Explanation: In this program, We are getting the input and clearing all the values.
Output:

$ g++ inp1.cpp
$ a.out
First input: 4
Clearing cin.
All done.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main( )
  4.     {
  5.         char line[100];
  6.         cin.getline( line, 100, 't' );
  7.         cout << line;
  8.         return 0;
  9.     }

a) 100
b) t
c) It will print what we enter till character t is encountered in the input data
d) 200
View Answer

Answer: c
Explanation: The program will store all strings entered and will print them only when the character ‘t’ is encountered.

Input >> coding
Input >> is fun
Input >> t

Output:
coding
is fun

8. How many parameters are there in getline function?
a) 1
b) 2
c) 2 or 3
d) 3
View Answer

Answer: c
Explanation: There are two or three parameters in getline() function. They are a pointer to an array of characters and maximum number of characters and an optional delimiter.

9. What can be used to input a string with blank space?
a) inline
b) getline
c) putline
d) setline
View Answer

Answer: b
Explanation: If a user wants to input a sentence with blank spaces, then he may use the function getline.

10. When will the cin can start processing of input?
a) After pressing return key
b) BY pressing blank space
c) After pressing return key & BY pressing blank space
d) BY pressing delete space
View Answer

Answer: a
Explanation: When you give some input to console the processing of the input starts when the user presses enter/return key.

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.

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.