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
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
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
Explanation: cin is mainly used to get the input during the runtime.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i + 4;
return 0;
}
a) 73
b) your value + 4
c) Error
d) 63
View Answer
Explanation: We are not allowed to do addition operation on cin.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price = 0;
int quantity = 0;
cout << "Enter price: ";
getline (cin, mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin, mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price * quantity << endl;
return 0;
}
a) 50
b) Depends on value you enter
c) Error
d) 100
View Answer
Explanation: In this program, We are getting the input on runtime and manipulating the value.
Output:
$ 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?
#include <iostream>
#include <ios>
#include <istream>
#include <limits>
using namespace std;
template <typename CharT>
void ignore_line ( basic_istream<CharT>& in )
{
in.ignore ( numeric_limits<streamsize> :: max(), in.widen ( '\n' ) );
}
int main()
{
cout << "First input: ";
cin.get();
cout << "Clearing cin.\n";
cin.clear();
ignore_line ( cin );
cout << "All done.\n";
}
a) First input
b) Clearing cin
c) Error
d) Second input
View Answer
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?
#include <iostream>
using namespace std;
int main( )
{
char line[100];
cin.getline( line, 100, 't' );
cout << line;
return 0;
}
a) 100
b) t
c) It will print what we enter till character t is encountered in the input data
d) 200
View Answer
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
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
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
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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Apply for C++ Internship
- Check Programming Books
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice Computer Science MCQs