Free CPP Exam Braindumps (page: 7)

Page 6 of 58

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 end<enter>?

#include <iostream>
#include <string>
#include <list>
#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 ()
{
list<int> l;
for( ; !cin.bad() ; )
{
int i;
cin>>i;
l.push_back(i);
}
for_each(l.begin(), l.end(), Out<int>(cout));
return 0;
}

Program will output:

  1. 1 2 3
  2. 1 2 3 end
  3. 1
  4. compilation error
  5. program runs forever without output

Answer(s): E



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

#include <iostream>
#include <set>
#include <vector>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main(){
vector<int> v;
multiset<int> s;
for(int i=10; i>0; i??) {
v.push_back(i); s.push_back(i);
}
print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl;
return 0;
}

  1. program outputs: 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
  2. program outputs: 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1
  3. program outputs: 10 9 8 7 6 5 4 3 2 1 and unpredictable sequence of numbers range 1 to 10
  4. compilation error

Answer(s): D



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

#include <iostream>
#include <map>
using namespace std;
int main() {
int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"};
map<int, string> m;
for (int i = 0; i < 10; i++) {
m.push_back(pair<int, string>(t[i], s[i]));
}

for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {
cout << i?>first << " ";
}
return 0;
}

  1. program outputs: 1 2 3 4 5
  2. compilation error
  3. program outputs: 1 1 2 2 3 3 4 4 5 5
  4. program outputs: one two three four five
  5. program outputs: one one two two three three four four five five

Answer(s): B



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

#include <deque>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
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<B> d1(t, t+10);
sort(d1.begin(), d1.end());
deque<B>::iterator it = upper_bound(d1.begin(), d1.end(), B(4), greater<B>());
for_each(it, d1.end(), Out<B>(cout)); cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): C






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

CPP Exam Discussions & Posts