Free CPP Exam Braindumps (page: 25)

Page 24 of 58

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

#include <deque>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int> v1;
deque<int> d1;
for(int i=0; i<5; i++)
{
v1.push_back(i);v1.push_front(i);
d1.push_back(i);d1.push_front(i);
}
for(int i=0; i<d1.size(); i++)
{
cout<<d1[i]<<" "<<v1[i]<<" ";
}
cout<<endl;
return 0;
}

What will be its output:

  1. 4 4 3 3 2 2 1 1 0 0 0 0 1 1 2 2 3 3 4 4
  2. runtime exception
  3. compilation error due to line 11
  4. compilation error due to line 12

Answer(s): C



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

#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
using namespace std;
bool identical(int a, int b) {
return b == 2*a?true:false;
}
int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};
vector<int> v1(t, t + 15);
deque<int> d1(u, u + 15);

pair<deque<int>::iterator, vector<int>::iterator > result;
result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I
if (result.first == d1.end() && result.second == v1.end()) { //Line II
cout<<"Identical\n";
} else {
cout<<"Not identical\n";
}
return 0;
}

Program outputs:

  1. Identical
  2. Not identical
  3. compilation error at line marked I
  4. compilation error at line marked II

Answer(s): B



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

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

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

Program will output:

  1. truefalse
  2. true0;
  3. 1false
  4. 10
  5. none of these

Answer(s): D



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

#include <vector>
#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};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end(), greater<B>());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}

Program outputs:

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

Answer(s): C






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