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 <set>
# 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<<" "; } };
template <typename T> struct Sequence {
T start; T step;
Sequence(T start, T step):start(start), step(step){}
T operator()() { T v = start; start+=step; return v; } };
bool Less(float a, float b) { return int(a)<int(b);}
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<float> v1; v1.assign(t, t+10);
stable_sort(v1.begin(), v1.end(), Less);
for_each(v1.begin(), v1.end(), Out<float>(cout));cout<<endl;
return 0;
}
Program outputs:
# include <vector>
# include <set>
# 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<<" "; } };
template <typename T> struct Sequence {
T start; T step;
Sequence(T start, T step):start(start), step(step){}
T operator()() { T v = start; start+=step; return v; } };
bool Less(float a, float b) { return int(a)<int(b);}
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<float> v1; v1.assign(t, t+10);
stable_sort(v1.begin(), v1.end(), Less);
for_each(v1.begin(), v1.end(), Out<float>(cout));cout<<endl;
return 0;
}
Program outputs:
Correct Answer: D
Vote an answer
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
int main ()
{
std::vector<int>v1;
for(int i = 10; i>0; i??)
{
v1.push_back(i);
}
std::vector<int>::iterator it = v1.begin();
int sum = 0;
while(it != v1.end())
{
sum+=it++;
}
std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<<sum <<std::endl;
return 0;
}
#include <vector>
#include <iostream>
int main ()
{
std::vector<int>v1;
for(int i = 10; i>0; i??)
{
v1.push_back(i);
}
std::vector<int>::iterator it = v1.begin();
int sum = 0;
while(it != v1.end())
{
sum+=it++;
}
std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<<sum <<std::endl;
return 0;
}
Correct Answer: B
Vote an answer
What happens when you attempt to compile and run the following code?
# include <vector>
# include <iostream>
# include <algorithm>
# include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add : public binary_function<int, int, int> {
int operator() (const int & a, const int & b) const {
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(), bind1st(Add(), 1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
# include <vector>
# include <iostream>
# include <algorithm>
# include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add : public binary_function<int, int, int> {
int operator() (const int & a, const int & b) const {
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(), bind1st(Add(), 1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
Correct Answer: E
Vote an answer
Which sentence is correct about the code below? Choose all that apply.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class F {
int val;
public:
F(int v):val(v){}
bool operator() (int v) {
if (v == val) return true;
return false;
}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
if (find(v1.begin(), v1.end(), 6) == find(v1.begin(), v1.end(), F(6))) { cout<<"Found!\n";
} else {
cout<<"Not found!\n";
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class F {
int val;
public:
F(int v):val(v){}
bool operator() (int v) {
if (v == val) return true;
return false;
}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
if (find(v1.begin(), v1.end(), 6) == find(v1.begin(), v1.end(), F(6))) { cout<<"Found!\n";
} else {
cout<<"Not found!\n";
}
return 0;
}
Correct Answer: C
Vote an answer
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:
# 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: E
Vote an answer
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(myints, myints+10);
set<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
for(set<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(myints, myints+10);
set<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
for(set<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
Correct Answer: D
Vote an answer
What happens when you attempt to compile and run the following code? Choose all that apply.
# include <iostream>
# include <fstream>
# include <string>
# include <list>
# include <algorithm>
# include <iomanip>
using namespace std;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
operator int() const { return val; };};
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<setw(3)<<hex<<val; } };
int main () {
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out", ios::trunc|ios::out);
list<B> l(t, t+10);
for_each(l.begin(), l.end(), Out<B>(f));
f.close();
f.open("test.out");
for( ; f.good() ; ) {
int i;
f>>i;
cout<<i<<" ";
}
f.close();
return 0;
}
# include <iostream>
# include <fstream>
# include <string>
# include <list>
# include <algorithm>
# include <iomanip>
using namespace std;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
operator int() const { return val; };};
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<setw(3)<<hex<<val; } };
int main () {
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out", ios::trunc|ios::out);
list<B> l(t, t+10);
for_each(l.begin(), l.end(), Out<B>(f));
f.close();
f.open("test.out");
for( ; f.good() ; ) {
int i;
f>>i;
cout<<i<<" ";
}
f.close();
return 0;
}
Correct Answer: B,C,D,E
Vote an answer
What will happen when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
set<int>s(myints, myints+10);
multiset<int> s1(s.begin(),s.end());
s1.insert(s.begin(),s.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
The output will be:
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
set<int>s(myints, myints+10);
multiset<int> s1(s.begin(),s.end());
s1.insert(s.begin(),s.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
The output will be:
Correct Answer: A
Vote an answer
What will happen when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template <class T>
class A {
T_v;
public:
A(T v);
};
template<class T>
#include <iostream>
using namespace std;
template <class T>
class A {
T_v;
public:
A(T v);
};
template<class T>
Correct Answer: C
Vote an answer
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <deque>
# include <list>
# include <queue>
# include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
queue<int> first; queue<int> second(mydeck);
queue<int> third(second); queue<int, list<int> > fourth(mylist);
fourth.push(10);fourth.push(11);fourth.push(12);
queue<int, vector<int> > fifth(myvector);
fifth.push(10);fifth.push(11);fifth.push(12); // Line I
while(!fifth.empty())
{
cout<<fifth.front()<<" "; // Line II
fifth.pop(); // Line III
}
while (!fourth.empty())
{
cout << fourth.front() << " ";
fourth.pop(); // Line IV
}
return 0;
}
# include <iostream>
# include <deque>
# include <list>
# include <queue>
# include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
queue<int> first; queue<int> second(mydeck);
queue<int> third(second); queue<int, list<int> > fourth(mylist);
fourth.push(10);fourth.push(11);fourth.push(12);
queue<int, vector<int> > fifth(myvector);
fifth.push(10);fifth.push(11);fifth.push(12); // Line I
while(!fifth.empty())
{
cout<<fifth.front()<<" "; // Line II
fifth.pop(); // Line III
}
while (!fourth.empty())
{
cout << fourth.front() << " ";
fourth.pop(); // Line IV
}
return 0;
}
Correct Answer: D
Vote an answer