國立暨南國際大學 113 學年度第二學期小考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2025.4.25
系所別:
年級:
學號:
姓名:
考試時間 14:10-14:20
  1. (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;
    }


  2. If we run "gdb q1.exe" with the following commands, what value of x shall we obtain, respectively?
    1. break main
      run
      next
      next
      next
      next
      print x
    2. break main
      run
      step
      step
      step
      step
      print x

  3. (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


  4. (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;
    }