What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> using namespace std; class A { int a,b; public: A(const A & c) { a = c.a; } A():a(0),b(0){} void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;} int getA() {return a;} int getB() {return b;} }; int main () { vector<A> v; A a; a.setA(10); a.setB(11); v.push_back(a); cout<<v[0].getB()<<" "<<v[0].getA()<<endl; return 0; }
Answer(s): B
What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> #include <algorithm> using namespace std; class B { int val; public: B(int v=0):val(v){} int getV() const {return val;} operator int () const { return val;} }; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; struct Add { B operator()(B & a, B & b) { return a+b; }}; int main() { int t[]={1,2,3,4,5,6,7,8,9,10}; vector<B> v1(t, t+10); vector<B> v2(10); transform(v1.begin(), v1.end(), v2.begin(), bind1st(Add(),1)); for_each(v2.rbegin(), v2.rend(), Out<B>(cout));cout<<endl; return 0; }Program outputs:
Answer(s): E
What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> #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() { int t1[]={3,2,4,1,5}; int t2[]={5,6,8,2,1}; vector<int> v1(10); sort(t1, t1+5); sort(t2, t2+5); set_symmetric_difference(t1,t1+5,t2,t2+5,v1.begin()); for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl; return 0; }Program outputs:
What will happen when you attempt to compile and run the code below, assuming that file test.out do not exist before the program execution? #include <iostream> #include <fstream> #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 (){ int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; fstream f("test.out"); list<int> l(t, t+10); for_each(l.begin(), l.end(), Out<int>(f)); f.close(); return 0; }
Answer(s): C
What happens when you attempt to compile and run the following code? #include <iostream> #include <algorithm> #include <vector> using namespace std; class A { int a; public: A(int a) : a(a) {} int getA() const { return a; } void setA(int a) { this?>a = a; } bool operator==(const A & b) const { return a == b.a; } }; bool compare(const A & a, const A & b) { return a == b; } int main () { int t[] = {1,2,3,3,5,1,2,4,4,5}; vector<A> v (t,t+10); vector<A>::iterator it = v.begin(); while ( (it = adjacent_find (it, v.end(), compare)) != v.end()) { cout<<it?v.begin()<<" ";it++; } cout<< endl; return 0; }
Post your Comments and Discuss C++ Institute CPP exam prep with other Community members:
Our website is free, but we have to fight against AI bots and content theft. We're sorry for the inconvenience caused by these security measures. You can access the rest of the CPP content, but please register or login to continue.