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(), bind1st(1,Add()));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
A. 1 2 3 4 5 6 7 8 9 10
B. 2 3 4 5 6 7 8 9 10 11
C. 11 10 9 8 7 6 5 4 3 2
D. compilation error
E. 10 9 8 7 6 5 4 3 2 1
正解:D
質問 2:
What will happen 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; }
};
int main()
{
A<string>a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}
A. program will run without any output
B. program will display: Hello
C. program will not compile
D. program will display: Hello world!
正解:D
質問 3:
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(), bind1st(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
A. 1 2 3 4 5 6 7 8 9 10
B. 2 3 4 5 6 7 8 9 10 11
C. 11 10 9 8 7 6 5 4 3 2
D. compilation error
E. 10 9 8 7 6 5 4 3 2 1
正解:D
質問 4:
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 unpredictable number
B. program will display: 0
C. program will not compile
D. program willl be not linked
正解:D
質問 5:
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:
A. 0x100 0x256 0x256
B. 64 100
C. 100 256 256
D. 100 256
E. 0x64 0x100
正解:C
質問 6:
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:
A. compilation error
B. 3 4 6 8 0 0 0 0 0 0
C. 6 8 3 4 0 0 0 0 0 0
D. 6 8 0 0 0 0 0 0 0 0
E. 3 4 0 0 0 0 0 0 0 0
正解:B
奈良** -
この問題集はCPP試験の出題範囲を完全分析・網羅したテキストでして、本当に使ってみて最高って思いました。