CPP : C++ Certified Professional Programmer : Part 07
-
Which pieces of code inserted independently into places marked 1 and 2 will cause the program to compile and display: 0 1 2 3 4 5 6 7 8 9? Choose all that apply.
#include <list>
#include <iostream>
using namespace std;
class A { int a; public:
A(int a){ this?>a=a;}
//insert code here 1
};
//insert code here 2
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << ” “; start++;
}
}
int main() {
A t1[] ={ 1, 7, 8, 4, 5 };list<A> l1(t1, t1 + 5);
A t2[] ={ 3, 2, 6, 9, 0 };list<A> l2(t2, t2 + 5);
l1.sort();l2.sort();l1.merge(l2);
print(l1.begin(), l1.end());
print(l2.begin(), l2.end()); cout<<endl;
return 0;
}- place 1: operator int() { return a; }
- place 1: operator int() { return a; }
bool operator < (const A & b) { return this?>a< b.a;} - place 1: bool operator < (const A & b) { return this?>a< b.a;}
- place 1: bool operator < (const A & b) { return this?>a< b.a;}
friend ostream & operator <<(ostream & c, const A & a);
place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;} - place 1: bool operator < (const A & b) { return this?>a< b.a;}
place 2: ostream & operator <<(ostream & c, const A & a) { c<<a.a; return c;}
-
What happens when you attempt to compile and run the following code?
#include <vector>
using namespace std;
int main ()
{
std::vector<int>v1;
v1.push_back(10);
return 0;
}- compilation fails due to error in line 2
- compilation fails due to error in line 5
- exception is thrown during run time
- code compiles and executes successfully
-
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<<” “; }
};
bool Compare(char a, char b) { return tolower(a) < tolower(b);}
int main() {
char s[]={“qwerty”};
char t1[]={“ert”};
char t2[]={“ERT”};
sort(s, s+6);
cout<<includes(s,s+6, t1,t1+3, Compare)<<” “<<includes(s,s+6, t2,t2+3, Compare)<<endl;
return 0;
}Program outputs:
- 0 0
- 0 1
- 1 0
- 1 1
-
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,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(), 2);
cout<< number<<endl;
return 0;
}Program outputs:
- 4
- 3
- 2
- 0
- compilation error
-
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 t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(t, t+10);
set<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
bool found = s1.find(7);
if (found){
cout<<“Element found!\n”;
}else {
cout<<“Element not found!\n”;
}
return 0;
}- program will display “Element found!”
- program will display “Element not found!\n”
- code will not compile
- changing type of variable found to int will make this code compile
-
What happens when you attempt to compile and run the following code?
#include <deque>
#include <vector>
#include <iostream>
using namespace std;int main ()
{
int t[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> v1(t, t + 10);
deque<int> d1(v1.begin(), v1.end());
deque<int> d2;
d2 = d1;
d2.insert(d1.rbegin(), 10);
for(int i = 0; i<d1.size(); i++)
{
cout<<d1[i]<<” “;
}
return 0;
}- program outputs: 0 1 2 3 4 5 6 7 8 9 10
- program outputs: 10 0 1 2 3 4 5 6 7 8 9
- program outputs: 0 1 2 3 4 5 6 7 8 9
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <deque>
#include <list>
#include <iostream>
using namespace std;
int main ()
{
list<int>l1;
deque<int>d1;
for(int i=0; i<5; i++)
{
l1.push_back(i);l1.push_front(i);
d1.push_back(i);d1.push_front(i);
}
for(int i=0; i<d1.size(); i++)
{
cout<<d1[i]<<” “<<l1[i]<<” “;
}
cout<<endl;
return 0;
}- program displays 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
- compilation error due to line 16
-
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;
}
bool classifier(int v) {
return v%2==0;
}
int main() {
int t[] = { 1, 5, 2, 5, 2, 4, 4, 3, 3, 1 };
vector<int> v1(t, t+10);
set<int> s1(t, t+10);
replace(v1.begin(), v1.end(),classifier, 10);
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}Program outputs:
- 1 5 10 5 10 10 10 3 3 1
- 1 5 2 5 2 4 4 3 3 1
- compilation error
- 10 10 2 10 2 4 4 10 10 10
-
What will happen when you attempt to compile and run the code below, assuming that file test.in contains the following sequence: 1 2 3?
#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 () {
ifstream f(“test.in”);
list<int> l;
for( ; f.good() ; ) {
int i;
f>>i;
l.push_back(i);
}
f.close();
for_each(l.begin(), l.end(), Out<int>(cout));
return 0;
}Program will output:
- 1 2 3
- 1 2 3 3
- no output
- compilation error
- program runs forever without output
-
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;
}- program outputs: Found at position: 5
- program outputs: Found at position: 0
- no output
- program outputs: Found at position: 10
-
What happens when you attempt to compile and run the following code?
#include <string>
#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() {
string t1[] ={ “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”};
list<string> l1(t1, t1 + 10);
list<string> l2(l1);
l2.reverse(); l1.splice(l1.end(),l2);
l1.unique();
print(l1.begin(), l1.end()); cout<<endl;
return 0;
}- compilation error
- program outputs: 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
- program outputs: 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
- program outputs: 1 2 3 4 5 6 7 8 9 10
-
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;
}- program outputs 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- compilation error
- program outputs 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5
- program outputs 1 2 3 4 5 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0
-
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:
- 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 <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 t1[]={1,2,3,4,5,6,7,8,9,10};
int t2[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t1, t1+10);
vector<int> v2(t2, t2+10);
vector<int> v3(10);
transform(v1.begin(), v1.end(), v2.rbegin(), v3.begin(), minus<int>());
for_each(v3.rbegin(), v3.rend(), Out<int>(cout));cout<<endl;
return 0;
}Program outputs:
- 9 7 5 3 1 ?1 ?3 ?5 ?7 ?9
- ?1 ?3 ?5 ?7 ?9 9 7 5 3 1
- 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
- 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
- ?9 ?7 ?5 ?3 ?1 1 3 5 7 9
-
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>
A::A(T v):_v(v) {}int main()
{
A<int> a(2);
cout<<1<<endl;
return 0;
}- program will display: 1
- 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>
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};
int t2[]={5,6,8,2,1};
vector<B> v1(10,0);
sort(t1, t1+5);
sort(t2, t2+5);
set_union(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}Program outputs:
- 3 2 4 1 5 6 8 2 1 0
- 1 2 3 4 5 6 8 2 1 0
- 1 1 2 2 3 4 5 5 6 8
- 1 2 3 4 5 6 8 0 0 0
- compilation error
-
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 t1[]={3,2,4,1,5};
int t2[]={6,10,8,7,9};
vector<int> v1(5);
transform(t1,t1+5,t2,v1.rbegin(), plus<int>());
for_each(v1.rbegin(), v1.rend(), Out<int>(cout));cout<<endl;
return 0;
}Program outputs:
- 9 12 12 8 14
- 14 8 12 12 9
- 3 2 4 1 5 6 10 8 7 9
- 1 2 3 4 5 6 7 8 9 10
-
Which changes, introduced independently, will allow the code to compile and display “one” “eight” “nine” “ten”? Choose all that apply
#include <iostream>
#include <map>
#include <string>
using namespace std;
class A {
int a;
public:
A(int a):a(a){}
int getA() const { return a;}
/* Insert Code Here 1 */
};
/* Insert Code Here 2 */int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string s[] = {“three”, “four”, “two”, “one”, “six”,”five”, “seven”, “nine”,”eight”,”ten”};
map<A, string> m;/* Replace Code Here 3 */
for(int i=0; i<10; i++) {
m.insert(pair<A,string>(A(t[i]),s[i]));
}
m.erase(m.lower_bound(2),m.upper_bound(7));
map<A, string>::iterator i=m.begin(); /* Replace Code Here 4 */
for( ;i!= m.end(); i++) {
cout<<i?>second<<” “;
}
cout<<endl;
return 0;
}- operator int() const { return a;} inserted at Place 1
- bool operator < (const A & b) const { return a<b.a;} inserted at Place 1
- bool operator < (const A & b) const { return b.a<a;} inserted at Place 1
- struct R { bool operator ()(const A & a, const A & b) { return a.getA()<b.getA();} }; inserted at Place 2
replacing line marked 3 with map<A, string, R> m;
replacing line marked 4 with map<A, string,R>::iterator i=m.begin();
-
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()));
}
for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<*i<<” “;
}
return 0;
}- program outputs: 3 4 2 1 6 5 7 9 8 0
- program outputs: 00 11 22 33 44 55 66 77 88 99
- program outputs: 0 1 2 3 4 5 6 7 8 9
- program outputs: 0 00 1 11 2 22 3 33 4 44 5 55 6 66 7 77 8 88 9 99
- compilation error
-
What happens when you attempt to compile and run the following code?
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>using namespace std;
void print(int v) { cout<<v<<” “; }
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; }
};
bool predicate(int v) { return v%2==0; }
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
set<int> s1(v1.begin(), v1.end());
remove_if(s1.begin(), s1.end(), predicate);
for_each(s1.begin(), s1.end(), print);cout<<endl;
return 0;
}Program outputs:
- 1 3 5 7 9 6 7 8 9 10
- 1 3 5 7 9
- 2 4 6 8 10
- compilation error
Subscribe
0 Comments
Newest