CPP : C++ Certified Professional Programmer : Part 11
-
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:
- 20 30 10 20 30 10 20 30 10 20
- 30 20 10 0 0 0 0 0 0 0
- 30 0 0 0 0 0 0 0 20 10
- compilation error
-
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;
}- program outputs: 1 4 5 7 8 6 9 0 Size:8 3 2 Size:2
- program outputs: 1 4 5 7 8 6 9 0 Size:8 3 2 6 9 0 Size:5
- compilation error
- program outputs: 0 1 4 5 6 7 8 9 Size:8 3 2 Size:2
- program outputs: 0 1 4 5 6 7 8 9 Size:8 3 2 6 9 0 Size:5
-
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;
}- program displays: 1
- program displays: 2
- compilation error
- runtime exception
-
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:
- true
- false
- 1
- 0
- compilation error
-
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:
- 4
- 3
- 2
- 0
- compilation error
-
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;
}- program outputs: 3 4 2 1 0 1 2 3 4 0
- program outputs: 00 11 22 33 44
- program outputs: 0 0 1 1 2 2 3 3 4 4
- program outputs: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4
- compilation error
-
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;
}- bool operator < (const B & v) const { return val<v.val;}
- bool operator > (const B & v) const { return val<v.val;}
- bool operator > (const B & v) const { return val>v.val;}
- bool operator == (const B & v) const { return val==v.val;}
- operator int () const { return val; }
-
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;
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<int> l(t, t+10);
for_each(l.begin(), l.end(), Out<int>(f));
f.close(); f.open(“test.out”);
for( ; f.good() ; ) {
int i; f>>i;
cout<<i<<” “;
}
f.close();
return 0;
}- file test.out will be opened writing
- file test.out will be truncated
- file test.out will be opened for reading
- no file will be created nor opened
- program will display sequence 1 2 3 4 5 6 7 8 9 10
-
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
map<int, int> m;
for(int i=0; i < 10; i++) {
m[i]=t[i];
}
map<int, int>::iterator it = find(m.begin(), m.end(), 5);
cout<<it?>first;
return 0;
}Program outputs:
- 5
- 4
- 10
- compilation error
-
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”};
multimap<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));
multimap<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 multimap<A, string, R> m;
replacong line marked 4 with multimap<A, string, R>::iterator i=m.begin();
-
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;
}- program will display: Hello world!
- compilation error
- program will display: world!Hello
- program will run without any output
-
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; }- program will display: 0
- program will not compile
- program will display unpredictable number
- program will be not linked
-
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:
- 12
- 4
- 2
- 0
- 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<<” “; }};
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:
- 1 2 3 4 5 6 7 8 9 10
- 10 9 8 7 6 5 4 3 2 1
- no output
- 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;int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
vector<int> v1(t, t + 15);
set<int> s1(t, t + 15);pair<set<int>::iterator, vector<int>::iterator > resultSet = mismatch(s1.begin(), s1.end(), v1.begin());
cout<<*resultSet.first<<” “<<*resultSet.second<<endl;return 0;
}Program outputs:
- 2 4
- 4 2
- 0 5
- compilation error
-
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;struct display {
void operator() (int i) {cout << ” ” << i;}
};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);for_each(v1.begin(), v1.end(), display); //Line I
for_each(d1.begin(), d1.end(), *(new display())); // Line II
for_each(s1.begin(), s1.end(), display()); // Line III
return 0;
}- program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10
- program outputs: 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1
- compilation error in line I
- compilation error in line II
- compilation error in line III
-
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1.1 2.2 3.3<enter>?
#include <iostream>
#include <string>
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
- 1.12.23.3
- 1.1 2.2 3.3
- none of these
-
Which changes introduced independently will allow the code to compile and display 0 0 1 1 8 8 9 9 (choose all that apply)?
#include <iostream>
#include <set>
#include <vector>
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(){
A t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
set<A>s(t, t+10);/* Replace Code Here 3 */
multiset<A> s1(s.begin(),s.end());/* Replace Code Here 4 */
s1.insert(s.begin(),s.end());
s1.erase(s1.lower_bound(2),s1.upper_bound(7));
multiset<A>::iterator i=s1.begin();/* Replace Code Here 5 */
for( ;i!= s1.end(); i++)
{
cout<<i?>getA()<<” “;
}
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 set<A, R>s(t, t+10);
replacing line marked 4 with multiset<A,R> s1(s.begin(),s.end());
replacing line marked 5 with multiset<A,R>::iterator i=s1.begin();
-
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 t[]={3,2,4,1,5,10,9,7,8,6};
vector<B> v1(t,t+10);
sort(v1.begin(), v1.end(), greater<B>());
cout<<*min_element(v1.begin(), v1.end());
return 0;
}Program outputs:
- 3
- 1
- 6
- 10
- compilation error
-
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;
}- file test.out will be opened writing
- file test.out will be truncated
- file test.out will be opened for reading
- no file will be created nor opened
- program will display sequence 1 2 3 4 5 6 7 8 9 10
Subscribe
0 Comments
Newest