CPP practice questions, page 9 of 9, with answer review and explanations.
Question 41
Which pieces of code inserted independently into places marked 1 and 2 will cause the program to compile and display: 0 1 2 3 4 5 6 7 8 9? Choose all that apply. #include #include using namespace std; class A { int a; public: A(int a){ this?>a=a;} //insert code here 1 }; //insert code here 2 template void print(T start, T end) { while (start != end) { std::cout l1(t1, t1 + 5); A t2[] ={ 3, 2, 6, 9, 0 };list l2(t2, t2 + 5); l1.sort();l2.sort();l1.merge(l2); print(l1.begin(), l1.end()); print(l2.begin(), l2.end()); cout<<endl; return 0; }
place 1: operator int() { return a; }
place 1: operator int() { return a; } bool operator < (const A & b) { return this?>a< b.a;}
place 1: bool operator < (const A & b) { return this?>a< b.a;}
place 1: bool operator < (const A & b) { return this?>a< b.a;} friend ostream & operator <<(ostream & c, const A & a); place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;}
place 1: bool operator < (const A & b) { return this?>a< b.a;} place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;}
Question 42
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main () { int t[] = {1,2,3,4,5,1,2,3,4,5}; vector v (t,t+10); vector ::iterator it; int m1[] = {1, 2, 3}; it = search (v.begin(), v.end(), m1, m1+3); cout << "found at position: " << it?v.begin() << endl; return 0; } Program outputs:
found at position: 5
found at position: 0
found at position: 6
found at position: 1
found at position: 10
Question 43
What happens when you attempt to compile and run the following code? #include #include #include using namespace std; template struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out d1(10); generate(d1.begin(), d1.end(), Sequence(1)); sort(d1.begin(), d1.end()); pair ::iterator, deque ::iterator > result = equal_range(d1.begin(), d1.end(), 20); for_each(result.first, result.second, Out (cout));cout<<endl; return 0; } Program outputs:
10 10 10 20 20 20 20 30 30 30
20 20 20 20
10 20 20 20 20
20 20 20 20 30
10 20 20 20 20 30
Question 44
Which changes, introduced independently, will allow the code to compile and display “one” “eight” “nine” “ten”? Choose all that apply #include #include #include using namespace std; class A { int a; public: A(int a):a(a){} int getA() const { return a;} /* Insert Code Here 1 */ }; /* Insert Code Here 2 */ int main(){ int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 }; string s[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","ten"}; map m;/* Replace Code Here 3 */ for(int i=0; i (A(t[i]),s[i])); } m.erase(m.lower_bound(2),m.upper_bound(7)); map ::iterator i=m.begin(); /* Replace Code Here 4 */ for( ;i!= m.end(); i++) { cout second<<" "; } cout<<endl; return 0; }
operator int() const { return a;} inserted at Place 1
bool operator < (const A & b) const { return a<b.a;} inserted at Place 1
bool operator < (const A & b) const { return b.a<a;} inserted at Place 1
struct R { bool operator ()(const A & a, const A & b) { return a.getA()<b.getA();} }; inserted at Place 2 replacing line marked 3 with map<A, string, R> m; replacing line marked 4 with map<A, string,R>::iterator i=m.begin();
Question 45
What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; template struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(5); transform(t1,t1+5,t2,v1.rbegin(), plus ()); for_each(v1.rbegin(), v1.rend(), Out (cout));cout<<endl; return 0; } Program outputs: