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
質問 2:
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
float f = 10.126;
cout<<f<<" "<<setprecision(2)<<f<<endl;
return 0;
}
Program outputs:
A. 10.126 10
B. compilation error
C. 10.126 10.12
D. 10.126 10.13
正解:A
質問 3:
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,3,5,1,2,4,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
while ( (it = adjacent_find (it, v.end())) != v.end()) {
cout<<it?v.begin()<<" ";it++;
}
cout<< endl;
return 0;
}
A. program outputs: 2 3
B. compilation error
C. program outputs: 3 8
D. program outputs: 2 7
E. program will run forever
正解:D
質問 4:
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:
A. 2 4 6 8 10
B. compilation error
C. 1 3 5 7 9 6 7 8 9 10
D. 1 3 5 7 9
正解:B
質問 5:
What will happen when you attempt to compile and run the following code?
#include <deque>
#include <vector>
#include <iostream>
using namespace std;
class A
{
int a;
public:
A(int a) {this?>a = a; c++;}
~ A() { c??;}
static int c;
};
int A::c(0);
int main ()
{
A t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8 , 9};
vector<A>v1(t, t+10);
deque<A>d1(v1.begin(), v1.end());
deque<A> d2;
d2 = d1;
cout<<A::c<< endl;
return 0;
}
How many objects of type A will be created:
A. 40
B. 20
C. 10
D. 30
正解:A
質問 6:
What happens when you attempt to compile and run the following code?
#include <deque>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
template<typename T>
void print(T start, T end)
{
while (start != end)
cout<<*start++;
}
int main ()
{
string t[] = {"one", "two" ,"three" ,"four", "five"};
vector<string>v1(t, t+5);
deque<string>d1(v1.rbegin(), v1.rend());
d1.push_back("zero");
print(d1[0].rbegin(),d1[0].rend());
return 0;
}
A. program outputs: five
B. program outputs: orez
C. compilation error
D. program outputs: evif
正解:D
質問 7:
What happens when you attempt to compile and run the following code? Choose all possible answers.
#include <iostream>
using namespace std;
template <class T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
friend ostream & operator<<(ostream & c, const A<T> & v) {
c<<v._v;return c;
}
};
int main()
{
A<int>a(10);
cout<<a<<endl;
return 0;
}
A. program will compile
B. program will display:10
C. program will not compile
D. program will run without output
正解:A,B
質問 8:
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;
}
struct sequence {
int val,inc;
sequence(int s, int i):val(s),inc(i){}
int operator()(){
int r = val; val += inc;
return r;
}
};
int main() {
vector<int> v1(10);
fill(v1.begin(), v1.end(), sequence(1,1));
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}
Program outputs:
A. 1 2 3 4 5 6 7 8 9 10
B. compilation error
C. 10
D. 0 0 0 0 0 0 0 0 0 0
正解:B
774 お客様のコメント





枪田** -
この一冊に絞ってもいいと思います。試験内容をしっかりまとめられています。CPP参考書として、頼れる1冊です。