C++ Institute C++ Certified Professional Programmer - CPP Exam Practice Test

What happens when you attempt to compile and run the following code?
# include <vector>
# include <iostream>
# include <algorithm>
# include <functional>
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() {
B t[]={3,2,4,1,5,10,9,7,8,6};
vector<B> v1(t,t+10);
cout<<*max_element(v1.begin(), v1.end(), greater<B>());
cout<<endl;
return 0;
}
Program outputs:
Correct Answer: E
What will happen when you attempt to compile and run the following code?
# include <iostream>
# include <string>
using namespace std;
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<string>a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}
Correct Answer: C
What happens when you attempt to compile and run the following code?
# include <deque>
# include <iostream>
# include <algorithm>
#include <functional>
using namespace std;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
B operator +(const B &b )const { return B(val + b.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<<" "; } };
template<typename A>
struct Add : public binary_function<A, A, A> {
A operator() (const A & a, const A & b) const { return a+b; } };
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
deque<B> d1(t, t+10);
deque<B> d2(10);
transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add<B>(), 1));
for_each(d2.rbegin(), d2.rend(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
Correct Answer: C
What will happen 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;
set<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;
}
The output will be:
Correct Answer: C
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
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
int main ()
{
int t[]={1,2,3,4,5};
std::vector<int>v1(t,t+5);
std::vector<int>v2(v1);
v1.resize(10);
v2.reserve(10);
std::vector<int>::iterator i = v1.begin();int ii = 0;
while (i != v1.end()) { std::cout<<i[ii]<<" ";ii??;i++; }
i = v2.begin();ii=0;
while (i != v2.end()) { std::cout<<i[ii]<<" ";ii??;i++; }
return 0;
}
Correct Answer: A
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <list>
using namespace std;
int main(){
int t[] ={ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
list<int>v(t, t+10);
set<int> s1(v.begin(),v.end());
if (s1.count(3) == 2) {
s1.erase(3);
}
for(set<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
Correct Answer: A
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <deque>
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; }
};
struct Even {
bool operator ()(const A & a, const A &b) {
return (a.getA() % 2)==b.getA() % 2;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
deque<int> d (t,t+15);
deque<int>::iterator it = search_n(d.begin(), d.end(), 3, 2, Even());
cout<< it?d.begin()<<endl;
return 0;
}
Program outputs:
Correct Answer: C
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
using namespace std;
class A
{
int a,b;
public:
A & operator =(const A & c) { a = c.a; return *this;}
A():a(0),b(0){}
void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}
int getA() {return a;} int getB() {return b;}
};
int main ()
{
vector<A>v;
A a;
a.setA(10); a.setB(11);
v.push_back(a);
A b = v.front(); v.pop_back();
cout<<b.getB()<<" "<<b.getA()<<endl;
return 0;
}
Correct Answer: C
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 64 100<enter>?
# include <iostream>
# include <string>
# include <sstream>
# include <iomanip>
using namespace std;
int main ()
{
string s;
getline(cin, s);
stringstream input(s);
stringstream output;
for( ; !input.fail() ; )
{
int i;
input>>hex>>i;
output<<setw(4)<<i;
}
cout<<output.str();
return 0;
}
What will be the result assuming that user will enter following sequence: 64 100:
Correct Answer: B
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
# include <deque>
# include <set>
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);
deque<int> d1(t, t + 10);
set<int> s1(t, t + 10);
for_each(v1.begin(), v1.end(), myfunction); // Line I
for_each(d1.begin(), d1.end(), myfunction); // Line II
for_each(s1.begin(), s1.end(), myfunction); // Line III
return 0;
}
Correct Answer: B
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
# include <algorithm>
# include <functional>
using namespace std;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
B operator +(const B &b )const { return B(val + b.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<<" "; } };
B Add(B a, B b) { return a+b; }
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<B> v1(t, t+10);
vector<B> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(ptr_fun(Add),1));
for_each(v2.rbegin(), v2.rend(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
Correct Answer: C
What happens when you attempt to compile and run the following code?
# include <deque>
# 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[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<int> d1(t, t+10);
sort(d1.begin(), d1.end());
deque<int>::iterator it = upper_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
Correct Answer: B