CPP

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

Question 36

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main () { int t[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector v1(t, t + 10); deque d1(v1.begin(), v1.end()); deque d2; d2 = d1; d2.insert(d1.rbegin(), 10); for(int i = 0; i<d1.size(); i++) { cout<<d1[i]<<" "; } return 0; }

  1. program outputs: 0 1 2 3 4 5 6 7 8 9 10
  2. program outputs: 10 0 1 2 3 4 5 6 7 8 9
  3. program outputs: 0 1 2 3 4 5 6 7 8 9
  4. compilation error

Question 37

What happens when you attempt to compile and run the following code? #include using namespace std; int main() { cout<<100<<" "; cout.setf(ios::hex); cout<<100<<" "; return 0; } Program outputs:

  1. 100 64
  2. 100 0x64
  3. 0x64 0x64
  4. 64 0x64
  5. 100 100

Question 38

What happens when you attempt to compile and run the following code? #include using namespace std; int main() { cout.setf(ios::hex, ios::basefield); cout<<100<<" "; cout.flags(ios::showbase); cout<<100<<" "; return 0; } Program outputs:

  1. 64 64
  2. 64 0x64
  3. 0x64 0x64
  4. 64 100
  5. compilation error

Question 39

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,2,3,5,1,2,7,3,2,1,10, 4,4,5}; vector v (t,t+15); vector ::iterator it = search_n(v.begin(), v.end(), 4, 2); cout<< it?v.begin()<<endl; return 0; } Program outputs:

  1. 10
  2. 3
  3. 1
  4. 15
  5. compilation error

Question 40

What happens when you attempt to compile and run the following code? #include #include #include #include using namespace std; template void print(T start, T end) { while (start != end) cout v1(t, t+5); deque d1(v1.rbegin(), v1.rend()); d1.push_back("zero"); print(d1[0].rbegin(),d1[0].rend()); return 0; }

  1. program outputs: orez
  2. program outputs: evif
  3. compilation error
  4. program outputs: five