CPP : C++ Certified Professional Programmer : Part 03
-
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <iomanip>using namespace std;
int main ()
{
float f = 10.126;
cout.unsetf(ios::floatfield);
cout<<scientific<<f<<” “<<setprecision(3)<<f<<endl;
return 0;
}What will be a mantissa part of the numbers displayed:
- 1.0126 1.013
- 1.012600 10.013
- 10.01260 10.013
- 1.012600 1.013
- 1.0126 1.01
-
Which keywords can be used to define template type parameters? Choose all possible answers:
- class
- typedef
- typename
- static
- volatile
-
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 = 0; i<10; i++) {v1.push_back(i); }
v1.resize(4);
std::vector<int>::iterator it = v1.end();
v1.insert(v1.end()?1, 4);
for(int i=0 ; i<= v1.size(); i++) {std::cout<<v1.at(i)+v1[i]<<” “; }std::cout<<std::endl;
return 0;
}- compilation error
- program outputs 0 1 2 3 4
- program outputs 0 2 4 8 6 and exception
- program outputs 0 2 4 6 8
- program outputs 0 2 4 8 6
-
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;
}- program outputs: 0 1 8 9
- program outputs: 2 3 4 5 6 7
- program outputs: 1 6 5 7
- program outputs: 3 4 9 8 0
-
Which are NOT valid instantiations of priority_queue object:
#include <iostream>
#include <deque>
#include <list>
#include <queue>
#include <vector>
using namespace std;int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
priority_queue<int> first;//line I
priority_queue<int, deque<int> > second;//line II
priority_queue<int> third(first);//line III
priority_queue<int, list<int> > fourth(third);//line IV
priority_queue<int, vector<int> > fifth(myvector.begin(), myvector.end());//line V
return 0;
}- line I
- line II
- line III
- line IV
- line V
-
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);
multiset<int> s1(v.begin(),v.end());
if (s1.count(3) == 2) {
s1.erase(3);
}
for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<” “;
}
return 0;
}- program outputs: 1 2 3 4 5
- program outputs: 1 2 4 5
- program outputs: 1 1 2 2 3 4 4 5 5
- program outputs: 1 1 2 2 3 3 4 4 5 5
- compilation error
-
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 4 quit<enter>?
#include <iostream>
#include <string>
#include <list>
#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 ()
{
list<string> l;
while(cin.good())
{
string s;
cin>>s;
if (s == “quit”) break;
l.push_back(s);
}
for_each(l.begin(), l.end(), Out<string>(cout));
return 0;
}Program will output:
- 1 2 3 4
- 1 2 3 4 quit
- 1
- program runs forever without output
-
What will happen when you attempt to compile and run the following code? Choose all possible answers.
#include <iostream>
using namespace std;
class B {};
template <typename 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> a(1);
A<B> b;
a.add(10);
cout << a.getV() <<endl;
return 0;
}- program will display:11
- program will not compile
- program will compile
- program will cause runtime exception
-
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<<” “; } };int main() {
int t[]={3,2,4,1,5,6,10,8,7,9};
vector<int> v1(t, t+10);
for_each(v1.begin(), v1.end(), bind1st(plus<int>(), 1));
for_each(v1.rbegin(), v1.rend(), Out<int>(cout));cout<<endl;
return 0;
}Program outputs:
- 3 2 4 1 5 6 10 8 7 9
- 4 3 5 2 6 7 11 9 8 10
- 9 7 8 10 6 5 1 4 2 3
- 10 8 9 11 7 6 2 5 3 4
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;void myfunction(int i) {
cout << ” ” << i;
}
int multiply (int a) {
return a*2;
}int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t+10);
set<int> s1(t, t+10);
transform(s1.begin(), s1.end(), v1.begin(), multiply);
transform(v1.begin(), v1.end(), s1.begin(), multiply);
for_each(s1.begin(), s1.end(), myfunction);
return 0;
}Program outputs:
- 20 10 18 12 4 8 14 16 6 2
- 2 4 6 8 10 12 14 16 18 20
- 4 8 12 16 20 24 28 32 36 40
- compilation error
-
What will happen when you attempt to compile and run the following code?
#include <deque>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int t[] = {1, 2 ,3 ,4 ,5, 6 , 7, 8 , 9, 10};deque<int>d1(t, t+10);
vector<int>v1(t, t+10);
cout<<v1.size()<<” “<<v1.capacity()<<” “;
cout<<d1.size()<<” “;<<d1.capacity()<<” “;
d1.resize(12); v1.resize(12);
cout<<v1.size()<<” “<<v1.capacity()<<” “;
cout<<d1.size()<<” “;<<d1.capacity()<<” “;
d1.reserve(20);v1.reserve(20);
cout<<v1.size()<<” “<<v1.capacity()<<” “;
cout<<d1.size()<<” “;<<d1.capacity()<<endl;
return 0;
}- the output is 10 10 10 10 12 12 12 12 20 20
- reserve and resize means exactly the same
- there are compilation errors
- capacity is always smaller then size
-
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:
- 0 0 1 1 8 8 9 9
- 0 1 8 9
- 2 3 4 5 6 7
- 3 4 9 8 0
- 3 3 4 4 9 9 8 8 0 0
-
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
#include <algorithm>
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 t1[]={3,2,4,1,5};
B t2[]={6,10,8,7,9};
vector<B> v1(10,0);
sort(t1, t1+5); sort(t2, t2+5);
copy(t1,t1+5,v1.begin());
copy(t2,t2+5,v1.begin()+5);
inplace_merge(v1.begin(), v1.begin()+5,v1.end());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}Program outputs:
- 1 2 3 4 5 6 10 8 7 9
- 3 2 4 1 5 6 7 8 9 10
- 3 2 4 1 5 6 10 8 7 9
- 1 2 3 4 5 6 7 8 9 10
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <deque>
#include <list>
#include <stack>
#include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
stack<int> first;
stack<int> second(mydeck);
stack<int> third(second);
stack<int, list<int> > fourth(mylist);
fourth.push(10);fourth.push(11);fourth.push(12);
stack<int, vector<int> > fifth(myvector);
fifth.push(10);fifth.push(11);fifth.push(12);
while(!fifth.empty())
{
cout<<fifth.top()<<” “;
fifth.pop();
}
while (!fourth.empty())
{
cout << fourth.front() << ” “;
fourth.pop();
}
return 0;
}- program outputs: 12 11 10 12 11 10
- compilation error
- program outputs: 10 11 12 10 11 12
- runtime exception
-
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: one two three<enter>?
#include <iostream>
#include <string>
using namespace std;int main ()
{
string a;
cin.getline(a);
cout<<a<<endl;
return 0;
}Program will output:
- one
- one two three
- runtime exception
- compilation error
- the result is unspecified
-
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++;
}
}
class A {
int a;
public:
A(int a):a(a){}
operator int () const { return a;}int getA() const { return a;}
};
int main() {
int t1[] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<A> l1(t1, t1 + 10);
list<A> l2(l1);
l2.reverse(); l1.splice(l1.end(),l2);
l1.pop_back();l1.unique();
print(l1.begin(), l1.end()); cout<<endl;
return 0;
}- compilation error
- runtime exception
- program outputs: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2
- program outputs: 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2
- program outputs: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
-
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class B { int val;
public:
B(int v=0):val(v){}
int getV() const {return val;}
operator int () const { return 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<<” “; } };
struct Add {
B operator()(B & a, B & b) { return a+b; } };
int main() {
B 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(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<B>(cout));cout<<endl;
return 0;
}Program outputs:
- 1 2 3 4 5 6 7 8 9 10
- 2 3 4 5 6 7 8 9 10 11
- 10 9 8 7 6 5 4 3 2 1
- 11 10 9 8 7 6 5 4 3 2
- compilation error
-
What happens when you attempt to compile and run the following code? Choose all that apply.
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int>v1(10, 3);
v1.push_back(3);
cout<<v1.capacity()<<” “<< v1.size()<<endl;
return 0;
}- program displays 4 4
- program displays 10 3
- size of vector v1 is 11
- all elements of vector v1 are of the same value
-
What happens when you attempt to compile and run the following code? Choose all possible answers.
#include <iostream>
using namespace std;
class C {
public:
int _c;
C():_c(0){}
C(int c) { _c = c;}
C operator+=(C & b) {
C tmp; tmp._c = _c+b._c;
return tmp;
} };ostream & operator<<(ostream & c, const C & v) {
c<<v._c; return 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(2);
A<C> a (5);
a.add(C());
cout << a.getV() <<endl;
return 0;
}- program will display:5
- program will not compile
- program will compile
- program will cause runtime exception
-
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, 6, 5, 7, 9, 8, 0 };
vector<int> v(t, t + 10);
map<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()));
}
pair<map<int, string>::iterator, map<int, string>::iterator> range;
range = m.equal_range(6);
for (map<int, string>::iterator i = range.first; i != range.second; i++) {
cout << i?>first << ” “;
}
return 0;
}- program outputs: 6
- program outputs: 5 7
- program outputs: 6 7
- program outputs: 1 5
- program outputs: 6 5
Subscribe
0 Comments
Newest