- (10%)
Consider
the following program. Suppose
we compile it with the command "g++ -g q1.cpp -o q1.exe".
//
q1.cpp
#include <iostream>
void f2() {
int x = 30;
int y = 40;
int z = 50;
}
void f1() {
f2();
int x = 3;
int y = 4;
int z = 5;
}
int main()
{
f1();
int x = 1000;
f2();
return 0;
}
If
we run "gdb q1.exe" with the following commands, what value of x shall
we
obtain, respectively?
-
break main
run
next
next
next
next
print x
-
break main
run
step
step
step
step
print x
- (10%)
Consider
the following program. Please note that the line number on the
left is only shown for your convenience. They are not part of the
code.
1 #include
<iostream>
2
3 int main()
4 {
5 int total = 0;
6 int i = 1;
7 while (i <= 100) {
8 total += i;
9 ++i;
10 }
11 std::cout << total <<
std::endl;
12 return 0;
13 }
If
we compile this program with "g++ -g q2.cpp -o q2.exe", and run "gdb
q2.exe" with the following commands, what value of i shall we obtain?
break 9 if total >= 666
run
print i
- (10%) Predict the output of the following code.
// Destructor
#include <iostream>
class CInt {
int value;
public:
CInt(int v=0): value(v) {
std::cout << "Constructor for " << value << std::endl;
}
~CInt() {
std::cout << "Destructor for " << value << std::endl;
}
};
int main() {
CInt a(4);
CInt b(25);
return 0;
}