科目: 程式設計
學號: 考試日期 2026.6.12
期末考 姓名:
考試時間 14:30-15:00
Open book; turn off computer & mobile phone
1
6
2
7
3
8
4
9
5
10

  1. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // Constructor
    #include <iostream>

    class MyInt {
    public:
        int m_value;
        MyInt() { m_value = 0; }            // Default Constructor
        MyInt(int n=0): m_value(n) {}       // initialization list
    };

    int main()
    {
        MyInt a;
        MyInt b(3);
        std::cout << a.m_value << ' ' << b.m_value << '\n';
        return 0;
    }

  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // Copy Constructor vs. Overloading the Assignment Operator
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    class MyInt {
    public:
        int m_value;
        MyInt(int n=0): m_value(n) {}
        MyInt(MyInt& b): m_value(2*b.m_value) {}
        MyInt& operator=(const MyInt& b) {
            m_value = 3*b.m_value;
            return *this;
        }
    };

    int main()
    {
        MyInt a(3);
        MyInt b(a);
        MyInt c;
        c = a;
        MyInt d = a;
        cout << a.m_value << b.m_value << c.m_value << d.m_value << '\n';
        return 0;
    }

  3. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // vector
    #include <iostream>
    #include <vector>
    using std::vector;

    int main()
    {
        vector<int> a = { 10, 20, 30 };
        a.insert(a.begin() + 1, 40);
        for (auto it = a.begin(); it != a.end(); it = it + 1)
            std::cout << *it << ' ';
        std::cout << a.at(1) << std::endl;
        return 0;
    }


  4. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // map
    #include <iostream>
    #include <map>

    int main() {
        std::map<char, int> points = {{'X', 100}, {'Y', 200}};

        auto it = points.find('X');
        if (it != points.end()) {
            it->second += 50;
        }

        for (auto it = points.rbegin(); it != points.rend(); ++it) {
            std::cout << it->first << "->" << it->second << " ";
        }
        std::cout << std::endl;

        return 0;
    }


  5. (10%) Consider the following OnDraw() function, what result will be shown on the screen after you run the program?  Please specify the coordinates of endpoints of each line segment.

    // Drawing in MFC
    void CQ5View::OnDraw(CDC* pDC)
    {
        // pDC->SelectStockObject(NULL_BRUSH);
        for (int i = 0; i < 5; ++i)
            pDC->Rectangle(150 - i * 10, 150 - i * 10, 160 + i * 10, 200 + i * 10);
    }



  6. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // string
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    using std::cin;
    using std::endl;

    void u(string& s) {
        for (size_t i=0; i<s.length(); ++i)
            s[i] = s[i] | 32;
            // 'A' == 65, 'a' == 97
    }

    int main()
    {
        string s("TAYLOR");
        u(s);
        std::cout << s << std::endl;
        return 0;
    }


  7. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // Overloading the Increment Operator
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ostream;

    class CMyInt {
        friend ostream& operator<<(ostream& o, const CMyInt& b);
        int value;
    public:
        CMyInt(int n=0): value(n) {}
        CMyInt& operator++() {
            ++value;
            return *this;
        }
    };

    ostream& operator<<(ostream& o, const CMyInt& b) {
        o << b.value;
        return o;
    }

    int main()
    {
        CMyInt a(6);
        cout << a++ << endl;
        cout << a << endl;
        return 0;
    }


  8. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the line(s) with syntax mistake(s).

    // remove_if & Lambda Function
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using std::vector;
    using std::remove_if;

    int main()
    {
        vector<int> B = {10, 30, 50, 70, 90, 20, 40, 60, 80};
        vector<int>::iterator it, p;
        it = remove_if(B.begin(), B.end(),
                [](int x) { return x < 60; } );
        for (p=it; p!=B.end(); ++p)
            std::cout << *p << ' ';
        return 0;
    }


  9. (10%) The following program cannot be compiled successfully, because strftime() expects its 3rd argument to be char*, but we provide a string here. If you know how to convert a string to a char*, which member function of fmt would you invoke (the 3rd argument becomes fmt.xxxxx()) so that the program can run successfully to show the current time?
    // String Conversion
    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <ctime>
    #include <string>
    using std::string;

    int main()
    {
        time_t t = time(nullptr);
        struct tm* now = localtime(&t);
        char buffer[20];
        string fmt("%Y-%m-%d %H:%M:%S");
        strftime(buffer, sizeof(buffer), fmt, now);
        std::cout << buffer << std::endl;
        return 0;
    }


  10. (10%) Consider the following OnDraw() function, what result will be shown on the screen after you run the program?  Please specify the coordinates of endpoints of each line segment.

    // MoveTo, LineTo
    void CQ10View::OnDraw(CDC* pDC)
    {
        const int x0 = 100;
        const int y0 = 100;
        const int N = 3;
        const int D = 50;
        for (int i = 1; i <=N; ++i) {
            pDC->MoveTo(x0 + i * D, y0);
            pDC->LineTo(x0 + i * D, y0 + D * (N + 1));
            pDC->MoveTo(x0, y0 + i * D);
            pDC->LineTo(x0 + D * (N + 1), y0 + i * D);
        }
    }