What happens when you attempt to compile and run the following code? #include <iostream> #include <string> using namespace std; template <class T> class A { T _v; public: A() {} A(T v): _v(v){} T getV() { return _v; } void add(T & a) { _v+=a; } void add(string & a) { _v.insert(0, a); } }; int main() { A<string> a("Hello"); string s(" world!"); a.add(s); cout << a.getV() <<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; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator()(const T & val ) { out<<val<<" "; }}; struct Sequence { int start; Sequence(int start):start(start){} int operator()() { return start++; } }; int main() { vector<int> v1(10); vector<int> v2(10); generate(v1.begin(), v1.end(), Sequence(1)); reverse_copy(v1.begin(),v1.end(), v2.rbegin()); sort(v2.begin(), v2.end(), less_equal<int>()); for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl; return 0; }Program outputs:
Answer(s): A
What happens when you attempt to compile and run the following code? #include <iostream> #include <algorithm> #include <set> 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;} }; struct Compare { bool operator ()(A & a) { if (a.getA() < 5) return true; return false; } }; int main () { int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5}; set<A> d (t,t+15); int number = count_if(d.begin(), d.end(), Compare()); cout<< number<<endl; return 0; }Program outputs:
Answer(s): E
Given three files: class.h, class.cpp and main.cpp containing small C++ project, which sentences are TRUE if you attempt to compile and run the program? Assume that the whole compiling environment is properly set. // File: main.cpp #include <iostream> #include "class.h" using namespace std; int main() { A<int> a; cout << a.getV() << endl; return 0; } //File: class.h #ifndef _CLASS_ #define _CLASS_ template <class T> class A { T _v; public: A() {} A(T v); T getV(); }; #endif //File: class.cpp #include "class.h" template<typename T> A<T>::A(T v):_v(v) {} template<class T> T A<T>::getV() { return _v; }
Answer(s): D
What happens when you attempt to compile and run the following code? #include <iostream> using namespace std; template<class A> void f(A a) { cout<<1<<endl; } void f(int a) { cout<<2<<endl; } int main() { int a = 1; f<float>(a); 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.