CPP

Review 228 CPP practice questions with answer explanations and community insights.

Question 1

What happens when you attempt to compile and run the following code? #include #include #include using namespace std; int main(){ int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; vector v(t, t+10); multiset s1(v.begin(),v.end()); s1.insert(v.begin(),v.end()); pair ::iterator,multiset ::iterator> range; range = s1.equal_range(6); while (range.first != range.second) { cout<<*range.first<<" "; range.first++; } return 0; }

  1. program outputs: 6 6
  2. program outputs: 5 7
  3. program outputs: 5 5 6 6 7 7
  4. program outputs: 5 5 7 7
  5. program outputs: 1 1 6 6 5 5

Question 2

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 v1(10); generate(v1.rbegin(), v1.rend(), Sequence(1)); rotate(v1.begin(),v1.begin() + 1, v1.end() ); for_each(v1.begin(), v1.end(), Out (cout) );cout<<endl; return 0; } Program outputs:

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

Question 3

What happens when you attempt to compile and run the following code? #include #include #include #include #include #include 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 struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) {out l(t, t+10); for_each(l.begin(), l.end(), Out (f)); f.close(); f.open("test.out"); for( ; f.good() ; ) { B i; f>>i; cout<<i<<" "; } f.close(); return 0; }

  1. file test.out will be opened writing
  2. file test.out will be truncated
  3. file test.out will be opened for reading
  4. compilation error
  5. program will display sequence 1 2 3 4 5 6 7 8 9 10

Question 4

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: one two three ? #include #include using namespace std; int main () { string a; cin>>a; cout<<a<<endl; return 0; } Program will output:

  1. one
  2. one two three
  3. runtime exception
  4. compilation error
  5. the result is unspecified

Question 5

What will happen when you attempt to compile and run the following code? #include #include #include #include #include using namespace std; int main() { int t[] = { 3, 4, 2, 1, 0, 3, 4, 1, 2, 0 }; vector v(t, t + 10); multimap m; for (vector ::iterator i = v.begin(); i != v.end(); i++) { stringstream s;s (*i, s.str())); } pair ::iterator, multimap ::iterator> range; range = m.equal_range(2); for (multimap ::iterator i = range.first; i != range.second; i++) { cout first << " "; } return 0; } The output will be:

  1. 2 2
  2. 1 2
  3. 1 3
  4. 2
  5. 0 2
We are moving to a new platform

CertificationTest is being migrated. Some pages may be slow or briefly unavailable; everything will be back to normal within a few hours. If you run into any issue, email support@certificationtest.net.