Free CPP Exam Braindumps (page: 28)

Page 27 of 58

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<<" "; } };

struct Add {
int operator()(int & a, int & b) {
return a+b;
}
};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): E



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

#include <iostream>

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. 321
  4. compilation error
  5. the result is unspecified

Answer(s): A



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

#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int main(){
int second[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","zero"};
map<int,string> m;
for(int i=0; i<10; i++) {
m.insert(pair<int,string>(second[i],first[i]));
}
m[0]="ten";
m.insert(pair<int,string>(1,"eleven"));
for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<i?>second<<" ";
}
return 0;
}

  1. program outputs: zero one two three four five six seven eight nine
  2. program outputs: ten one two three four five six seven eight nine
  3. program outputs: zero eleven two three four five six seven eight nine
  4. program outputs: ten eleven two three four five six seven eight nine
  5. program outputs: 0 1 2 3 4 5 6 7 8 9

Answer(s): B



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

#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main ()
{
string s;
getline(cin, s);
stringstream input(s);
stringstream output;

for( ; !input.fail() ; )
{
int i;
input>>i;
output<<hex<<i<<" ";
}
cout<<output.str();
return 0;
}

Program will output:

  1. 1 2 3
  2. 1 2 3 3
  3. 0x1 0x2 0x3
  4. 0x1 0x2 0x3 0x3
  5. program runs forever without output

Answer(s): B






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