What happens when you attempt to compile and run the following code?
#include <iostream> #include <string> using namespace std;
class B;
class A { int age; public: A () { age=5; }; friend class B; };
class B { string name;
public:
B () { name="Bob"; };
void Print(A ob) {
cout << name << ob.age;
}
};
int main () {
A a;
B b;
b.Print(a);
return 0;
}
A. It prints: Bob5
B. It prints: 5
C. None of these
D. It prints: Bob
正解:A
質問 2:
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:
int *tab;
B() { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
B(string s) : A(s) { tab = new int[size]; for (int i=0; i<size; i++) tab[i]=1;}
~B() { delete tab; }
void Print() {
for (int i=0; i<size; i++) cout << tab[i];
cout << name;
}
};
int main () {
B b1("Alan");
B b2;
b1.tab[0]=0;
b1.Print(); b2.Print();
return 0;
}
A. It prints: Alan
B. It prints: 111
C. It prints: 0
D. It prints: 011Alan111Bob
正解:D
質問 3:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
class First
{
public:
void Print(){ cout<<"from First";}
};
int main()
{
First t[2];
for (int i=0; i<2; i++)
t[i].Print();
}
A. Compilation error
B. Runtime error.
C. It prints: from First
D. It prints: from Firstfrom First
正解:D
質問 4:
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
質問 5:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
namespace myNamespace1
{
int x = 5;
int y = 10;
}
namespace myNamespace2
{
float x = 3.14;
float y = 1.5;
}
int main () {
{
using namespace myNamespace1;
cout << x << " ";
}{
using namespace myNamespace2;
cout << y;
}
return 0;
}
A. It prints: 3.14 10
B. It prints: 5 1.5
C. Compilation error
D. None of these
正解:B
質問 6:
What will variable "y" be in class B?
class A { int x; protected: int y; public: int age;
};
class B : public A {
string name;
public:
void Print() {
cout << name << age;
}
};
A. protected
B. None of these
C. public
D. private
正解:A
1226 お客様のコメント





Iizuka -
本日試験を受験し、合格しました。ありがとうございました。