Free CPP Exam Braindumps (page: 20)

Page 19 of 58

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

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

int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
deque<int> d1(t, t + 10);
set<int> s1(t, t + 10);
cout<<find(v1.begin(), v1.end(), 6)<<" "<<find(d1.begin(), d1.end(), 6)<<" "<<find(s1.begin(), s1.end(), 6);
return 0;
}

  1. program outputs: 6 6 6
  2. program outputs: 3 3 5
  3. program outputs: 3 6 5
  4. compilation error
  5. none of these

Answer(s): D



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

#include <iostream>

using namespace std;

template <typename T>
class A {
T _v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T a) { _v+=a; }
template <class U>
U get(U a) {
return (U)(_v);
}
};

int main()
{
A<int> a(1);
a.add(10);
cout.setf( ios::showpoint);
cout << a.getV() << " " << a.get(1.0)<<endl;
return 0;
}

  1. program will display: 11 11
  2. program will not compile
  3. program will display: 11.0000 11
  4. program will display: 11 11.000

Answer(s): D



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

#include <iostream>

using namespace std;

void g(int a)
{
cout<<a?1<<endl;
}

template<class A>
void g(A a)
{
cout<<a+1<<endl;
}


int main()
{
int a = 1;
g(a);
return 0;
}

  1. program displays: 0
  2. program displays: 2
  3. compilation error
  4. runtime exception

Answer(s): A



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

#include <vector>
#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 t1[]={3,2,4,1,5};
int t2[]={5,6,8,2,1};
vector<int> v1(10);
sort(t1, t1+5);
sort(t2, t2+5);
set_difference(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): B






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