C++ Institute CPP - C++ Certified Professional Programmer CPP Exam Questions

Page: 1 / 14
Total 228 questions
Question 1

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

#include

#include

#include

#include

#include

using namespace std;

class compare {

bool reverse;

public:

compare(bool revparam = false){ reverse = revparam;}

bool operator()(int lhs, int rhs) const{

if (reverse)return (lhs > rhs);

elsereturn (lhs < rhs);

}

};

int main(){

int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };

priority_queue > first(myints, myints + 10);

priority_queue, compare> second(myints, myints + 10,

compare(false));

while (first.size() > 0){

cout << first.top() << " "; first.pop();

}

while (second.size() > 0) {

cout << second.top() << " ";second.pop();

}

return 0;

}



Answer : B


Question 2

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

#include

#include

#include

using namespace std;

templateclass B { T val;

public:

B(T v):val(v){}

T getV() const {return val;} bool operator < (const B & v) const { return val

templateostream & operator <<(ostream & out, const B & v) { out<

out;}

templatestruct Out {

ostream & out;

Out(ostream & o): out(o){}

void operator() (const T & val ) { out<

bool Less(const B &a, const B &b) { return int(a.getV())

int main() {

float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};

vector > v1; v1.assign(t, t+10);

stable_sort(v1.begin(), v1.end(), Less);

for_each(v1.begin(), v1.end(), Out >(cout));cout<

return 0;

}

Program outputs:



Answer : A


Question 3

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

#include

#include

#include

using namespace std;

template void print(T start, T end) {

while (start != end) {

std::cout << *start << " "; start++;

}

}

int main(){

vectorv;

multiset 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<

return 0;

}



Answer : D


Question 4

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

#include

#include

#include

using namespace std;

int main ()

{

string s;

getline(cin, s);

stringstream input(s);

stringstream output;

for( ; !input.fail() ; )

{

int i;

input>>i;

output<

}

cout<

return 0;

}

Program will output:



Answer : B


Question 5

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: one two three?

#include

#include

using namespace std;

int main ()

{

string a;

cin>>a;

cout<

return 0;

}

Program will output:



Answer : A


Question 6

What will happen when you attempt to compile and run the following code? Choose all possible answers.

#include

using namespace std;

class B {};

template

class A {

T_v;

public:

A() {}

A(T v): _v(v){}

T getV() { return _v; }

void add(T a) { _v+=a; }

};

int main()

{

A a(1);

Ab;

a.add(10);

cout << a.getV() <

return 0;

}



Answer : A, C


Question 7

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

#include

#include

#include

using namespace std;

void myfunction(pair i) {

cout << " " << i.first;

}

int main() {

int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };

map m;

for(int i=0; i < 10; i++) {

m[i]=t[i];

}

for_each(m.begin(), m.end(), myfunction);

return 0;

}

Program outputs:



Answer : B


Page:    1 / 14   
Total 228 questions