國立暨南國際大學 103 學年度第二學期 期末考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2015.6.17
系所別:
年級:
學號:
姓名:
考試時間 8:30-10:30

1
2
3
4
5  
6
7
8
9  
10


11

  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 mistake(s).
    // Comma Operator
    #include <iostream>

    int main()
    {
        int a = 1;
        int b = 2;
        int c = 3;
        int d = 4;

        a,b = c,d;
        std::cout << a + b + c + d << std::endl;
        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 mistake(s).

    // Integer Division
    #include <iostream>

    int main()
    {
        const int k = 5, N = k * k;
        unsigned char a[N+1] = { 0 };
        int i, j, sum;
        for (i=1; i<=k; i++)
            for (j=1; j<=N/i; j++)
                a[i*j] = 1 - a[i*j];
        for (sum = 0, i=1; i<=N; i++)
            sum += a[i];
        std::cout << sum << std::endl;
        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 mistake(s).

    // do-while
    #include <iostream>

    int main()
    {
        int n = 307;
        do
        {
            std::cout << n << ' ';
            if (n % 2)
                n = 3 * n - 1;
            else
                n /= 2;
        } while (n > 1);
        std::cout << n << 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 mistake(s).

    // Character Pointer
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    int main()
    {
        int len;
        char s[] = "The Lord of the Rings";
        const int MAX_LEN = sizeof(s);
        char* p = s;
        for(len = 0; *p != '\0'; len++)
        {
            cout<< *p++;
            while(*p == ' ')
                p++;
        }
        cout<<endl;
        return 0;
    }


  5. (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 mistake(s).

    // Recursive Function
    #include <iostream>
    #include <string>
    using std::string;

    char digit(int n)
    {
        if (n < 10)
            return n + 48;
        else
            return 'A' + (n - 10);
    }

    string hex(int n)
    {
        if (n >= 16)
            return hex(n / 16) + digit(n % 16);
        else
            return string(1, digit(n % 16) );
    }

    int main()
    {
        std::cout << hex(707) << std::endl;
        return 0;
    }


  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 mistake(s).

    // Constructor

    #include <iostream>

    class CData
    {
    private:
        int value;

    public:
        CData(int n=0): value(n) {};        // Initialization List

        CData(CData& d)
        { value = d.value * 2; }

        void Print()
        { std::cout << value << std::endl; }
    };

    int main()
    {
        CData a(1);
        CData b;
        b = a;
        CData c = b;
        CData d(c);

        c.Print(); d.Print();
        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 mistake(s).
    // Destructor (P.328)
    #include <iostream>

    class CData
    {
    private:
    int value;
    public:
    CData(int n): value(n) {}

    ~CData()
    {
    std::cout << "Destructor called for object " << value << std::endl;
    }
    };

    int main()
    {
    CData a(6);
    CData* pb(new CData(17));
    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 mistake(s).

    // Assignment Operator (P.340)
    #include <iostream>

    class CData
    {
        private:
        int value;
    public:
        CData(int n=0): value(n) {};

        CData operator=(CData& a)
        {
            value = a.value--;
            return *this;
        }

        void Print()
        {
            std::cout << value << std::endl;
        }
    };

    int main()
    {
        CData a;
        CData b(5), c(7);
        (a = b) = c;
        a.Print(); b.Print(); c.Print();
        return 0;
    }


  9. (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 mistake(s).

    // Assignment Operator (P.340)
    #include <iostream>

    class CData
    {
    private:
    int value;
    public:
    CData(int n=0): value(n) {};

    CData& operator=(CData& a)
    {
    value = a.value--;
    return *this;
    }

    void Print()
    {
    std::cout << value << std::endl;
    }
    };

    int main()
    {
    CData a;
    CData b(5), c(7);
    (a = b) = c;
    a.Print(); b.Print(); c.Print();
    return 0;
    }



  10. (10%) If we replace the OnDraw() function in P.671 as below, what result will be shown on the screen after you run the program? Please specify the
    coordinates of endpoints of each segment, or the coordinates of centers and boundaries.
    void CSketcherView::OnDraw(CDC* pDC)

    {
        CSketcherDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        if (!pDoc)
            return;

        for (int i=0; i<=10; i++)
            pDC->Rectangle(300+i*10, 300+i*10, 200-i*10, 200-i*10);
    }


  11. (10%) After we create a form with a control IDC_MyButton, let us create a method OnBnClickedMybutton() for the button.  Then after we start the program, and click the button twice, what will be the caption of the button?
    void CEx16View::OnBnClickedMybutton()
    {
    CString greeting[] = { CString("Good morning"), CString("Good afternoon"), CString("Good evening") };
    CString name[] = { CString("Alice"), CString("Bob"), CString("Charlie") };
    static int i = 0;
    int j = 2;
    i = (i + 1) % 3;
    j = (j + 1) % 3;
    GetDlgItem(IDC_MyButton)->SetWindowTextW(greeting[i] + ", " + name[j]);
    }