Welcome to TestSimulate

Pass Your Next Certification Exam Fast!

Everything you need to prepare, learn & pass your certification exam easily.

365 days free updates. First attempt guaranteed success.

C++ Institute C++ Certified Professional Programmer (CPP) Free Practice Test

Question 1
What will happen when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class C {};
template <class T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T a) { _v+=a; }
};
int main()
{
A<int> b;
A<C>a;
a.add(C());
cout << b.getV() <<endl;
return 0;
}

Correct Answer: D
Question 2
What happens when you attempt to compile and run the following code?
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
void print(int v) { cout<<v<<" "; }
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() {
return 10*(1+(start++ %3));
}
};
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
remove(v1.begin(), v1.end(), 10);
for_each(v1.begin(), v1.end(), print);cout<<endl;
return 0;
}
Program outputs:

Correct Answer: B
Question 3
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int t[] = {1,2,3,4,5,1,2,3,5,4};
vector<int> v (t,t+10);
vector<int>::iterator it;
int m1[] = {1, 3, 2};
it = find_end (v.begin(), v.end(), m1, m1+3);
if (it != v.end())
cout << "Found at position: " << it?v.begin() << endl;
return 0;
}

Correct Answer: C
Question 4
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"}; multimap<int, string> m;
for (int i = 0; i < 10; i++) {
m.insert(pair<int, string>(t[i], s[i]));
}
if (m.count(3) == 2) {
m.erase(3);
}
for (multimap<int, string>::iterator i = m.begin(); i != m.end(); i++) { cout << i?>first << " ";
}
return 0;
}

Correct Answer: D
Question 5
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(t, t+10);
multiset<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
pair<multiset<int>::iterator,multiset<int>::iterator> range;
range = s1.equal_range(6);
while (range.first != range.second) {
cout<<*range.first<<" "; range.first++;
}
return 0;
}

Correct Answer: C
Question 6
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 Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; } };
int main() {
vector<int> v1(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
random(v1.begin(),v1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
return 0;
}
Program outputs:

Correct Answer: C
Question 7
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void myfunction(int i) {
cout << " " << i;
}
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
copy_backward(t, t+10, v1.rend());
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}
Program outputs:

Correct Answer: C
Question 8
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:

Correct Answer: A
Question 9
What happens when you attempt to compile and run the following code?
# include <vector>
# include <set>
# include <iostream>
# include <algorithm>
using namespace std;
void print(int v) { cout<<v<<" "; }
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; }
};
bool predicate(int v) { return v%2==0; }
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
set<int> s1(v1.begin(), v1.end());
remove_if(s1.begin(), s1.end(), predicate);
for_each(s1.begin(), s1.end(), print);cout<<endl;
return 0;
}
Program outputs:

Correct Answer: B
Question 10
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; } void setA(int a) { this?>a = a; }
bool operator==(A & b) { return a == b.a; }
};
struct Compare{
bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
};
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it;
A m1[] = {A(1), A(2), A(3)};
it = find_end (v.begin(), v.end(), m1, m1+3, Compare());
cout << "Found at position: " << it?v.begin() << endl;
return 0;
}

Correct Answer: B
Question 11
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 t[]={3,2,4,1,5,10,9,7,8,6};
vector<int> v1(t,t+10);
cout<<*max_element(v1.begin(), v1.end());
return 0;
}
Program outputs:

Correct Answer: E
Question 12
What happens when you attempt to compile and run the following code?
# include <string>
# include <list>
# include <iostream>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main() {
string t1[] ={ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
list<string> l1(t1, t1 + 10);
list<string> l2(l1);
l2.reverse(); l1.splice(l1.end(),l2);
l1.unique();
print(l1.begin(), l1.end()); cout<<endl;
return 0;
}

Correct Answer: D