What is the output of the program?
#include <iostream> #include <string>
using namespace std;
struct t { int tab[2];
};
class First
{
struct t u;
public:
First() {
u.tab[0] = 1;
u.tab[1] = 0;
}
void Print(){
cout << u.tab[0] << " " << u.tab[1];
}
};
int main()
{
First t;
t.Print();
}
A. It prints: 0 0
B. It prints: 1 0
C. It prints: 1 1
D. It prints: 2 2
正解:B
質問 2:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class A {
public:
int x;
A() { x=0;}
};
class B : protected A {
public:
int y;
using A::x;
B(int y) {this->y = y;}
void Print() { cout << x << y; }
};
int main () {
B b(5);
b.Print();
return 0;
}
A. It prints: 15
B. It prints: 0
C. It prints: 05
D. It prints: 5
正解:C
質問 3:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
class A
{
public:
virtual void Print(){ cout<<"A";}
};
class B:public A
{
public:
virtual void Print(){ cout<< "B";}
};
int main()
{
A *obj;
A ob1;
obj = &ob1;
obj->Print();
B ob2;
obj = &ob2;
obj->Print();
}
A. It prints: BA
B. It prints: AA
C. It prints: AB
D. It prints: BB
正解:C
質問 4:
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
const int size = 3;
class A {
public:
string name;
A() { name = "Bob";}
A(string s) { name = s;}
A(A &a) { name = a.name;}
};
class B : public A {
public:
B() { }
B(string s) : A(s) { }
void Print() {
cout << name;
}
};
int main () {
B b1("Alan");
b1.Print();
return 0;
}
A. It prints: Alan
B. It prints: 111Alan
C. It prints: 0
D. It prints: Bob
正解:A
質問 5:
Given:
#include <iostream>
#include <exception>
using namespace std;
int main () {
try
{
int * myarray= new int[1000];
}
catch (bad_alloc&)
{ cout << "Error allocating memory"; } catch (exception& e) { cout << "Standard exception"; } catch (...) { cout << "Unknown exception"; } return 0; }
What will happen if we use the operator "new" and the memory cannot be allocated?
A. It prints: Standard exception
B. It prints: Unknown exception
C. Compilation error
D. It prints: Error allocating memory
正解:D
質問 6:
What happens when you attempt to compile and run the following code?
#include <iostream> #include <string>
using namespace std;
class A {
public:
A() { cout << "A no parameters";}
A(string s) { cout << "A string parameter";}
A(A &a) { cout << "A object A parameter";}
};
class B : public A {
public:
B() { cout << "B no parameters";}
B(string s) { cout << "B string parameter";}
B(int s) { cout << "B int parameter";}
};
int main () {
A a2("Test");
B b1(10);
B b2(b1);
return 0;
}
A. It prints: A no parametersA no parametersB string parameter
B. It prints: A string parameterA no parametersB int parameterA object A parameter
C. It prints: A no parametersA no parameters
D. It prints: A no parametersB string parameter
正解:B
質問 7:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
union un
{
int x;
char c;
};
union un u1 = {10};
union un u2 = {'a'};
union un u3 = {20, 'a'};
cout<<u1.x;
cout<<u2.c;
cout<<u3.c;
return 0;
}
A. It prints: 1a
B. Compilation error
C. It prints: 10a20a
D. It prints: 10aa
正解:B
質問 8:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int op(int x, int y);
int main()
{
int i=2, j=2, k;
float f=0.3;
k = op(i, j);
cout<< k << "," << op(1, f);
return 0;
}
int op(int x, int y)
{
return x+y;
}
A. It prints: 4,0
B. It prints: 4,1
C. It prints: 0,4
D. It prints: 4,0.7
正解:B
浜丘** -
図解は教科書的な必要事項を記したものの他、挿絵のようなポップなものもあり書籍全体の物々しさを軽減しています解説が丁寧で分かりやすいのでしっかりと頭に入ってきます。