Free CPP Exam Braindumps (page: 21)

Page 20 of 58

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

#include <list>
#include <iostream>
#include <deque>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
class A {
int a;
public:
A(int a):a(a){}
operator int () const { return a;} int getA() const { return a;}
};
struct R {
int val;
R(int v):val(v){}
bool operator ()(const A & a) { return a>val;} };
int main() {
int t1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<A> l1(t1, t1 + 10);
R r(4); l1.remove_if(r);
print(l1.begin(), l1.end()); cout<<endl;
return 0;
}

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

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[]={6,10,8,7,9};
vector<int> 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<int>(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

Answer(s): D



What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1.1 2.2 3.3<enter>?

#include <iostream>
#include <string>
using namespace std;

int main ()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a<<b<<c<<endl;
return 0;
}

Program will output:

  1. 123
  2. 1 2 3
  3. 1.12.23.3
  4. 1.1 2.2 3.3
  5. none of these

Answer(s): E



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;

struct display {
void operator() (int i) {cout << " " << i;}
};

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);

for_each(v1.begin(), v1.end(), display); //Line I

for_each(d1.begin(), d1.end(), *(new display())); // Line II

for_each(s1.begin(), s1.end(), display()); // Line III
return 0;
}

  1. program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10
  2. program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1
  3. compilation error in line I
  4. compilation error in line II
  5. compilation error in line III

Answer(s): C






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