CPP : C++ Certified Professional Programmer : Part 10
-
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 Add(int a, int b) {
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(), bind2nd(ptr_fun (Add),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(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?
#include <iostream>
using namespace std;
int main()
{
cout<<true<<” “<<boolalpha<<false;
return 0;
}Program outputs:
- true false
- 1 0
- 1 false
- true 0
- 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 ()
{
vector<int>v1;
deque<int>d1;
for(int i=0; i<5; i++)
{
v1.push_back(i);v1.push_front(i);
d1.push_back(i);d1.push_front(i);
}
for(int i=0; i<d1.size(); i++)
{
cout<<d1[i]<<” “<<v1[i]<<” “;
}
cout<<endl;
return 0;
}What will be its output:
- 4 4 3 3 2 2 1 1 0 0 0 0 1 1 2 2 3 3 4 4
- runtime exception
- compilation error due to line 11
- compilation error due to line 12
-
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;
}- it will compile successfully
- it will display Found!
- it will display Not found!
- it will not compile successfully
-
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() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}Program outputs:
- 8 10 5 1 4 6 2 7 9 3
- 1 2 3 4 5 6 7 8 9 10
- compilation error
- 10 9 8 7 6 5 4 3 2 1
-
What happens when you attempt to compile and run the following code?
#include <deque>
#include <set>
#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() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<B> d1(t, t+10);
sort(d1.begin(), d1.end());
set<B> s1(t,t+10);
cout<<binary_search(s1.begin(),s1.end(), 4)<<” “<<binary_search(d1.begin(),d1.end(), 4)<<endl;
return 0;
}Program outputs:
- 1 0
- 1 1
- true true
- false false
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Compare {
bool operator ()(int a) {
if (a >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};
vector<int> v (t,t+15);int number = count(v.begin(), v.end(), Compare());
cout<< number<<endl;
return 0;
}Program outputs:
- 4
- 3
- 2
- 0
- 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<enter>?
#include <iostream>
#include <string>
#include <sstream>using namespace std;
int main ()
{
string s;
getline(cin, s);
stringstream input(s);
stringstream output;for( ; !input.fail() ; )
{
int i;
input>>i;
output<<hex<<i<<” “;
}
cout<<output.str();
return 0;
}Program will output:
- 1 2 3
- 1 2 3 3
- 0x1 0x2 0x3
- 0x1 0x2 0x3 0x3
- program runs forever without output
-
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(const A & c) { a = c.a; }
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);
cout<<v[0].getB()<<” “<<v[0].getA()<<endl;
return 0;
}- program outputs 10 11
- the result is unpredictable
- program outputs 10 0
- program outputs 11 0
- compilation error
-
What will happen when you attempt to compile and run the code below, assuming that file test.out do not exist before the program execution?
#include <iostream>
#include <fstream>
#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 (){
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f(“test.out”);
list<int> l(t, t+10);
for_each(l.begin(), l.end(), Out<int>(f));
f.close();
return 0;
}- file test.out will be created and opened for writing
- file test.out will be created and opened for reading
- no file will be created nor opened
- file test.out will contain sequence 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 <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 t1[]={3,2,4,1,5};
int t2[]={5,6,8,2,1};
vector<int> v1(10);
sort(t1, t1+5);
sort(t2, t2+5);
set_symmetric_difference(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}Program outputs:
- 6 8 3 4 0 0 0 0 0 0
- 3 4 0 0 0 0 0 0 0 0
- 6 8 0 0 0 0 0 0 0 0
- compilation error
- 3 4 6 8 0 0 0 0 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=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<<val<<” “; } };struct Add {
B operator()(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(), bind1st(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?
#include <iostream>
#include <deque>
#include <list>
#include <queue>
#include <vector>
using namespace std;
class compare {
bool reverse;
public:
compare(bool revparam = false){ reverse = revparam;}
bool operator()(int lhs, int rhs) const{
if (reverse)return (lhs > rhs);
elsereturn (lhs < rhs);
}
};
int main(){
int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
priority_queue<int, deque<int> > first(myints, myints + 10);
priority_queue<int, vector<int>, compare> second(myints, myints + 10,
compare(false));
while (first.size() > 0){
cout << first.top() << ” “; first.pop();
}
while (second.size() > 0) {
cout << second.top() << ” “;second.pop();
}
return 0;
}- compilation error
- program outputs: 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
- program outputs: 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
- program outputs: 3 4 2 1 6 5 7 9 8 0 3 4 2 1 6 5 7 9 8 0
-
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()
{
int t[] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
deque<int> mydeck(t, t+10);list<int> mylist(t,t+10);
queue<int> first;
queue<int> second(mydeck);
queue<int> third(second);
queue<int, list<int> > fourth(mylist);
mylist.clear();third.clear();
cout<<third.size()<< ” “<<mydeck.size()<< endl;
cout<<fourth.size()<< ” “<<mylist.size()<<endl;
return 0;
}- program outputs: 10 0
10 0 - program outputs: 0 0
0 0 - program outputs: 10 10
10 10 - program outputs: 10 0
0 10 - compilation error
- program outputs: 10 0
-
What happens when you attempt to compile and run the following code?
#include <deque>
#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() {
int t[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};
deque<B> d1(t, t+10);
sort(d1.begin(), d1.end());
pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(), B(20));
for_each(result.first, result.second, Out<B>(cout));cout<<endl;
return 0;
}Program outputs:
- 10 10 10 20 20 20 20 30 30 30
- 20 20 20 20
- 10 20 20 20 20
- 20 20 20 20 30
- 10 20 20 20 20 30
-
What happens when you attempt to compile and run the following code?
#include <deque>
#include <iostream>
#include <algorithm>
#include <set>
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);
set<int> s1(t,t+10);
cout<<binary_search(s1.begin(),s1.end(), 4)<<” “<<binary_search(d1.begin(),d1.end(), 4)<<endl;
return 0;
}Choose all possible outputs (all that apply):
- 1 0
- 1 1
- true true
- false false
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int main(){
int second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
string first[] = {“three”, “four”, “two”, “one”, “six”,”five”, “seven”, “nine”,”eight”,”zero”};
map<int,string> m;
for(int i=0; i<10; i++) {
m.insert(pair<int,string>(second[i],first[i]));
}
m[0]=”ten”;
m.insert(pair<int,string>(1,”eleven”));
for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<i?>second<<” “;
}
return 0;
}- program outputs: zero one two three four five six seven eight nine
- program outputs: ten one two three four five six seven eight nine
- program outputs: zero eleven two three four five six seven eight nine
- program outputs: ten eleven two three four five six seven eight nine
- program outputs: 0 1 2 3 4 5 6 7 8 9
-
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:
- 123
- 1 2 3
- 321
- compilation error
- the result is unspecified
-
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 Add {
int operator()(int & a, int & b) {
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(), bind2nd(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(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 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:
- 64 100
- 100 256
- 100 256 256
- 0x64 0x100
- 0x100 0x256 0x256
Subscribe
0 Comments
Newest