CPP

CPP practice questions, page 2 of 9, with answer review and explanations.

Question 6

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator v.val;} }; ostream & operator struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(10,0); sort(t1, t1+5); sort(t2, t2+5); set_intersection(t1,t1+5,t2,t2+5,v1.begin()); for_each(v1.begin(), v1.end(), Out (cout));cout<<endl; return 0; } Program outputs:

  1. compilation error
  2. 1 2 3 4 5 6 8 0 0 0
  3. 1 2 3 4 5 6 8 2 1 0
  4. 5 2 1 0 0 0 0 0 0 0
  5. 1 2 5 0 0 0 0 0 0 0

Question 7

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}; vector v1(t, t+5); list l1; l1.assign(v1.end(), v1.begin()); for(int i=0; i<l1.size(); i++) { cout<<l1.at(i)<<" "; } cout<<endl; return 0; }

  1. program displays 5 4 3 2 1
  2. program displays 1 2 3 4 5
  3. compilation error
  4. segmentation fault runtime exception

Question 8

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out v1(10); sort(t1, t1+5); sort(t2, t2+5); merge(t1,t1+5,t2,t2+5,v1.begin()); for_each(v1.begin(), v1.end(), Out (cout));cout<<endl; return 0; } Program outputs:

  1. 1 2 3 4 5 6 10 8 7 9
  2. 3 2 4 1 5 6 7 8 9 10
  3. 3 2 4 1 5 6 10 8 7 9
  4. 1 2 3 4 5 6 7 8 9 10
  5. compilation error

Question 9

Which sentence is correct about the code below? #include #include #include 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; } /* Insert Code Here */ }; struct add10 { void operator()(A & a) { a.setA(a.getA() + 10); } }; int main() { int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 }; vector v1(t, t + 10); for_each(v1.begin(), v1.end(), add10()); vector ::iterator it = find(v1.begin(), v1.end(), A(7)); cout getA() << endl; return 0; }

  1. it will compile and print 7
  2. it will not compile
  3. it will compile but the program result is unpredictable
  4. adding code: bool operator !=(const A & b) const { if (this?>a != b.a) { return true; } return false; } at Place 1 will allow the program to compile

Question 10

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; void myfunction(int i) { cout v1(t, t+10); for_each(v1.begin(), v1.end(), multiply); iter_swap(v1.begin(),t+9); for_each(v1.begin(), v1.end(), myfunction); return 0; } Program outputs:

  1. 1 5 9 6 2 4 7 8 3 1
  2. compilation error
  3. 1 2 3 4 5 6 7 8 9 10
  4. 10 9 8 7 6 5 4 3 2 1
  5. 10 5 9 6 2 4 7 8 3 1