What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 end<enter>? #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) {out<<val<<" "; } }; int main () { list<int> l; for( ; !cin.bad() ; ) { int i; cin>>i; l.push_back(i); } for_each(l.begin(), l.end(), Out<int>(cout)); return 0; }Program will output:
Answer(s): E
What happens when you attempt to compile and run the following code? #include <iostream> #include <set> #include <vector> using namespace std; template<class T> void print(T start, T end) { while (start != end) { std::cout << *start << " "; start++; } } int main(){ vector<int> v; multiset<int> s; for(int i=10; i>0; i??) { v.push_back(i); s.push_back(i); } print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl; return 0; }
Answer(s): D
What happens when you attempt to compile and run the following code? #include <iostream> #include <map> using namespace std; int main() { int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"}; map<int, string> m; for (int i = 0; i < 10; i++) { m.push_back(pair<int, string>(t[i], s[i])); } for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) { cout << i?>first << " "; } return 0; }
Answer(s): B
What happens when you attempt to compile and run the following code? #include <deque> #include <iostream> #include <algorithm> using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; deque<B> d1(t, t+10); sort(d1.begin(), d1.end()); deque<B>::iterator it = upper_bound(d1.begin(), d1.end(), B(4), greater<B>()); for_each(it, d1.end(), Out<B>(cout)); cout<<endl; return 0; }Program outputs:
Answer(s): C
Which stack initialization (line numbers) are correct? Choose all that apply. #include <iostream> #include <deque> #include <list> #include <stack> #include <vector> using namespace std; int main() { deque<int> mydeck; list<int> mylist; vector<int> myvector; stack<int> first; // Line I stack<int> second(mydeck); // Line II stack<int> third(second); // Line III stack<int, list<int> > fourth(mylist); // Line IV stack<int, vector<int> > fifth(myvector); // Line V return 0; }
Answer(s): A,B,C,D,E
Post your Comments and Discuss C++ Institute CPP exam prep with other Community members: