Free CPP Exam Braindumps (page: 22)

Page 21 of 58

What happens when you attempt to compile and run the following code?

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
vector<int> v1(t, t + 15);
set<int> s1(t, t + 15);

pair<set<int>::iterator, vector<int>::iterator > resultSet = mismatch(s1.begin(), s1.end(), v1.begin());
cout<<*resultSet.first<<" "<<*resultSet.second<<endl;

return 0;
}

Program outputs:

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

Answer(s): B



Which changes introduced independently will allow the code to compile and display 0 0 1 1 8 8 9 9 (choose all that apply)?

#include <iostream>
#include <set>
#include <vector>
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(){
A t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
set<A> s(t, t+10); /* Replace Code Here 3 */
multiset<A> s1(s.begin(),s.end()); /* Replace Code Here 4 */
s1.insert(s.begin(),s.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
multiset<A>::iterator i=s1.begin(); /* Replace Code Here 5 */
for( ;i!= s1.end(); i++)
{
cout<<i?>getA()<<" ";
}
cout<<endl;
return 0;
}

  1. operator int() const { return a;} inserted at Place 1
  2. bool operator < (const A & b) const { return a<b.a;} inserted at Place 1
  3. bool operator < (const A & b) const { return b.a<a;} inserted at Place 1
  4. struct R { bool operator ()(const A & a, const A & b) { return a.getA()<b.getA();} }; inserted at Place 2
    replacing line marked 3 with set<A, R> s(t, t+10);
    replacing line marked 4 with multiset<A,R> s1(s.begin(),s.end());
    replacing line marked 5 with multiset<A,R>::iterator i=s1.begin();

Answer(s): A,B,D



What happens when you attempt to compile and run the following code?

#include <deque>
#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<<" "; } };
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<int> d1(t, t+10);
deque<int>::iterator it = lower_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.end(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): A



What happens when you attempt to compile and run the following code? Choose all that apply.

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include <iomanip>
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<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<setw(3)<<hex<<val; } };

int main () {
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out", ios::trunc|ios::out);
list<B> l(t, t+10);
for_each(l.begin(), l.end(), Out<B>(f));
f.close();
f.open("test.out");
for( ; f.good() ; ) {
int 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. no file will be created nor opened
  5. program will display sequence 1 2 3 4 5 6 7 8 9 10

Answer(s): A,B,C,E






Post your Comments and Discuss C++ Institute CPP exam with other Community members: