What happens when you attempt to compile and run the following code?
#include <iostream> #include <exception> using namespace std;
class myClass : public exception { virtual const char* what() const throw() { return "My exception."; } } obj;
int main () { try { throw obj; } catch (exception& e) { cout << e.what() << endl; } return 0; }
A. Compilation error
B. It prints: 0
C. It prints: My exception.
D. It prints: 1
正解:C
質問 2:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int *a= new int;
*a=100;
cout << *a;
delete a;
}
A. It prints: 100
B. It prints: 0
C. It prints: 1
D. It prints: 10
正解:A
質問 3:
What is the output of the program if character "1" is supplied as input?
#include <iostream>
using namespace std;
int main () {
int c;
cin >> c;
try
{
switch (c)
{
case 1:
throw 20;
case 2:
throw 5.2f;
case 3:
throw 'a';
}
}
catch (int e)
{ cout << "int exception. Exception Nr. " << e; }
catch (float e)
{ cout << "float exception. Exception Nr. " << e; }
catch (...)
{ cout << "An exception occurred."; }
return 0;
}
A. It prints: int exception. Exception Nr. 20
B. It prints: An exception occurred
C. Compilation Error
D. It prints: float exception. Exception Nr. 5.2
正解:A
質問 4:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
int x=20;
int *ptr;
ptr = &x;
cout<<*ptr;
return 0;
}
A. It prints: 20
B. It prints address of ptr
C. It prints: 0
D. It prints: 2
正解:A
質問 5:
What is the output of the program?
#include <iostream>
using namespace std;
#define SQR(x)(x*x)
int main(int argc, char *argv[]) {
int x, y=2;
x = SQR(y);
cout << x << ", " <<y;
return 0;
}
A. It prints: 3, 2
B. It prints: 9, 2
C. It prints: 4, 2
D. It prints: 3, 3
正解:C
質問 6:
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
質問 7:
Which code, inserted at line 10, generate the output "50"?
#include <iostream>
using namespace std;
class Base {
int age;
public:
Base () {
age=5;
};
//insert code here
void Print() { cout << age;}
};
void setAge(Base &ob) {ob.age = 0;}
int main () {
Base a;
a.Print();
setAge(a);
a.Print();
return 0;
}
A. friend void setAge(Base &ob);
B. friend void setAge(Base *ob);
C. friend void setAge(Base ob);
D. None of these
正解:A
質問 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 i;
i = x + y;
return i;
}
int main()
{
int i=1, j=2, k, l;
k = op(i, j);
l = op(j, i);
cout<< k << "," << l;
return 0;
}
A. It prints: ?1,1
B. It prints: 3,3
C. It prints: 1,2
D. It prints: 1,1
正解:B
Niigaki -
全力を尽くして勉強していただきます。CPA学習教材は有効です。CPAの問題集滅多にいいもんないけどこれだけはいいと思う。