C++ Certified Professional Programmer v6.0 (CPP)

Page:    1 / 16   
Total 228 questions

What happens 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; }
void add(string & a) {
_v.insert(0, a);
}
};
int main()
{
A<string>a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}

  • A. program will display: Hello world!
  • B. compilation error
  • C. program will display: world!Hello
  • D. program will run without any output


Answer : B

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));
reverse_copy(v1.begin(),v1.end(), v2.rbegin());
sort(v2.begin(), v2.end(), less_equal<int>());
for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl; return 0;
}
Program outputs:

  • A. 1 2 3 4 5 6 7 8 9 10
  • B. 10 9 8 7 6 5 4 3 2 1
  • C. no output
  • D. compilation error


Answer : A

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <set>
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 < (const A & b) const { return a<b.a;}
};
struct Compare {
bool operator ()(A & a) {
if (a.getA() < 5) return true;
return false;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<A> d (t,t+15);
int number = count_if(d.begin(), d.end(), Compare());
cout<< number<<endl;
return 0;
}
Program outputs:

  • A. 12
  • B. 4
  • C. 2
  • D. 0
  • E. compilation error


Answer : E

Given three files: class.h, class.cpp and main.cpp containing small C++ project, which sentences are TRUE if you attempt to compile and run the program? Assume that the whole compiling environment is properly set.
// File: main.cpp
#include <iostream>
#include "class.h"
using namespace std;
int main()
{
A<int> a;
cout << a.getV() << endl;
return 0;
}
//File: class.h
#ifndef _CLASS_
#define _CLASS_
template <class T>
class A {
T_v;
public:
A() {}
A(T v);
T getV();
};
#endif
//File: class.cpp
#include "class.h"
template<typename T>
A<T>::A(T v):_v(v) {}
template<class T>
T A<T>::getV() { return _v; }

  • A. program will display: 0
  • B. program will not compile
  • C. program will display unpredictable number
  • D. program willl be not linked


Answer : D

What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template<class A>
void f(A a)
{
cout<<1<<endl;
}
void f(int a)
{
cout<<2<<endl;
}
int main()
{
int a = 1;
f<float>(a);
return 0;
}

  • A. program displays: 1
  • B. program displays: 2
  • C. compilation error
  • D. runtime exception


Answer : A

What happens when you attempt to compile and run the following code?
#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()
{
int t1[] ={ 1, 7, 8, 4, 5 };
list<int> l1(t1, t1 + 5);
int t2[] ={ 3, 2, 6, 9, 0 };
list<int> l2(t2, t2 + 5);
l1.sort();
list<int>::iterator it = l2.begin();
it++; it++;
l1.splice(l1.end(),l2, it, l2.end());
print(l1.begin(), l1.end()); cout<<"Size:"<<l1.size()<<" "; print(l2.begin(), l2.end()); cout<<"Size:"<<l2.size()<<endl; return 0;
}

  • A. program outputs: 1 4 5 7 8 6 9 0 Size:8 3 2 Size:2
  • B. program outputs: 1 4 5 7 8 6 9 0 Size:8 3 2 6 9 0 Size:5
  • C. compilation error
  • D. program outputs: 0 1 4 5 6 7 8 9 Size:8 3 2 Size:2
  • E. program outputs: 0 1 4 5 6 7 8 9 Size:8 3 2 6 9 0 Size:5


Answer : A

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 10*(1+(start++ %3)); } };
int main() {
vector<int> v1(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
sort(v1.rbegin(), v1.rend());
unique_copy(v1.begin(),v1.end(), v2.begin());
for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl; return 0;
}
Program outputs:

  • A. 20 30 10 20 30 10 20 30 10 20
  • B. 30 20 10 0 0 0 0 0 0 0
  • C. 30 0 0 0 0 0 0 0 20 10
  • D. compilation error


Answer : B

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <set>
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; } operator int() const {return a;}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<A> s (t,t+15);
cout<<equal(s.begin(), s.end(), t)<<endl;
return 0;
}
Program outputs:

  • A. true
  • B. false
  • C. 1
  • D. 0
  • E. compilation error


Answer : D

Which method added to class B at the marked spot will allow the code below to compile?
Choose all possible solutions.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;}
/* Insert Code Here */
};
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;
}

  • A. bool operator < (const B & v) const { return val<v.val;}
  • B. bool operator > (const B & v) const { return val<v.val;}
  • C. bool operator > (const B & v) const { return val>v.val;}
  • D. bool operator == (const B & v) const { return val==v.val;}
  • E. operator int () const { return val; }


Answer : B,C,D

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
int main(){
int t[] ={ 3, 4, 2, 1, 0, 1, 2, 3, 4, 0 };
vector<int> v(t, t+10);
multimap<int,string> m;
for(vector<int>::iterator i=v.begin(); i!=v.end(); i++) {
stringstream s; s<<*i<<*i; m.insert(pair<int,string>(*i,s.str()));
}
for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++) { cout<<*i<<" ";
}
return 0;
}

  • A. program outputs: 3 4 2 1 0 1 2 3 4 0
  • B. program outputs: 00 11 22 33 44
  • C. program outputs: 0 0 1 1 2 2 3 3 4 4
  • D. program outputs: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
  • E. compilation error


Answer : E

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; }
};
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);
int number = count(d.begin(), d.end(), 2);
cout<< number<<endl;
return 0;
}
Program outputs:

  • A. 4
  • B. 3
  • C. 2
  • D. 0
  • E. compilation error


Answer : A

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;
}

  • A. program outputs 3 55
  • B. compilation error
  • C. program outputs 3 45
  • D. program outputs 7 55


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;
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);
cout<<find(v1.begin(), v1.end(), 6)<<" "<<find(d1.begin(), d1.end(), 6)<<"
"<<find(s1.begin(), s1.end(), 6);
return 0;
}

  • A. program outputs: 6 6 6
  • B. program outputs: 3 3 5
  • C. program outputs: 3 6 5
  • D. compilation error
  • E. none of these


Answer : D

What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template <typename T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T a) { _v+=a; }
template <class U>
U get(U a) {
return (U)(_v);
}
};
int main()
{
A<int> a(1);
a.add(10);
cout.setf( ios::showpoint);
cout << a.getV() << " " << a.get(1.0)<<endl; return 0;
}

  • A. program will display: 11 11
  • B. program will not compile
  • C. program will display: 11.0000 11
  • D. program will display: 11 11.000


Answer : D

What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
void g(int a)
{
cout<<a?1<<endl;
}
template<class A>
void g(A a)
{
cout<<a+1<<endl;
}
int main()
{
int a = 1;
g(a);
return 0;
}

  • A. program displays: 0
  • B. program displays: 2
  • C. compilation error
  • D. runtime exception


Answer : A

Page:    1 / 16   
Total 228 questions