Here is a listing of C++ Programming quiz on “Input Stream” along with answers, explanations and/or solutions:
1. Which operator is used for input stream?
a) >
b) >>
c) <
d) <<
Answer: b
Clarification: 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 <
Answer: a
Clarification: 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
Answer: b
Clarification: cin is mainly used to get the input during the runtime.
4. What will be the output of the following C++ code?
-
#include -
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
Answer: c
Clarification: We are not allowed to do addition operation on cin.
5. What will be the output of the following C++ code?
-
#include -
#include -
#include -
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
Answer: b
Clarification: 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 -
#include -
#include -
#include -
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
Answer: d
Clarification: 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.
