國立暨南國際大學 102 學年度第二學期 第三次期中考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.6.3
系所別:
年級:
學號:
姓名:
考試時間 14:10-16:00
1

2

3
 
4

5
 
6
 
7
 
8
 
9
 
10

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

    // Initializing Multidimensional Arrays (P.227)
    #include <iostream>

    int main()
    {
        unsigned short data[2][4] = { { 1, 2, 3   }, {5, 6     } };
        std::cout << data[0][2] << std::endl;
        return 0;
    }


  2. (10%) If we replace the OnDraw() function in P.953 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;

        CPoint FourPoints[4] = { CPoint(100,100), CPoint(100,300), CPoint(300,300), CPoint(300,100) };
        CPoint TempPoints[4];
       
        int i, j;
        for (i=0; i<3; i++)
        {
            pDC->MoveTo(FourPoints[0]);
            for (j=1; j<=4; j++)
                pDC->LineTo(FourPoints[j % 4]);
            for (j=0; j<4; j++)
                TempPoints[j] = CPoint( (FourPoints[j].x + FourPoints[(j+1) % 4].x)/2,
                                        (FourPoints[j].y + FourPoints[(j+1) % 4].y)/2 );
            for (j=0; j<4; j++)
                FourPoints[j] = TempPoints[j];
        }
    }


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

    // Pass-by-Reference (P.268)
    #include <iostream>

    void swap(int &i, int j)
    {
        int temp;
        temp = i;
        i = j;
        j = temp;
        return;
    }

    int main()
    {
        int a = 6;
        int b = 3;
        std::cout << a << b << std::endl;
        swap(a, b);
        std::cout << a << b << std::endl;
        return 0;
    }


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

    // Recursive Functions (P.285)
    #include <iostream>

    void f(int i)
    {
        if (i >= 2)
            f( i / 2 );
        std::cout << i % 2;
        return;
    }

    int main()
    {
        f(51);
        std::cout << std::endl;
        return 0;
    }


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

    // Class Destructors (P.435)
    #include <iostream>
    using std::cout;
    using std::endl;
    class CText
    {
    public:
        char* ptext;
        CText(const char* s)
        {
            ptext = new char[strlen(s)+1];
            strcpy(ptext, s);
        }
        ~CText()
        {
            cout << "Destructor called to release " << strlen(ptext)+1
                 << " bytes memory of " << ptext << endl;
            delete [] ptext;
        }
    };
    int main()
    {
        CText c("Coconut"), m("Mango");
        return 0;
    }



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

    // Overloading the Assignment Operator (P.454)
    #include <iostream>
    using std::cout;
    using std::endl;
    class CData
    {
    public:
        int* pdata;
        CData(int v = 0)
        { pdata = new int(v); }
        CData operator=(CData d)
        { pdata = d.pdata;
          return CData( *d.pdata );
        }
        void Print()
        { cout << *pdata << endl; }
    };
    int main()
    {
        CData a(6), b(3);
        CData c;
        c = a;
        a = b;
        a.Print();
        b.Print();
        c.Print();
        return 0;
    }


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

    // Recursive Function Calls (P.285)
    #include <iostream>
    using std::cout;

    void octal(int n)
    {
        if (n >= 8 )
           octal(n / 8);
        cout << n;
    }

    int main()
    {
        octal(63);
        return 0;
    }

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

    // String Objects (P.510)
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::string;
    int main()
    {
    int a = 6;
    string b("B");
    string c(a, 'b');
    string d( c + b );
    cout << c << endl;
    cout << d << endl;
    return 0;
    }

  9. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Searching Strings (P.523)
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::string;
    int main()
    {
    string a("NNNU");
    string b("N");
    cout << a.find(b) << a.find(b, 2) << endl;
    return 0;
    }

  10. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // The Size of a Vector (P.655)
    #include <iostream>
    #include <string>
    #include <vector>
    using std::cout;
    using std::endl;
    using std::string;
    using std::vector;

    int main()
    {
        vector<string> a;
        int b = 6;
        int c = 3;

        cout << a.size() << endl;
        a.push_back(b);
        a.push_back(c);
        cout << a.size() << endl;

        return 0;
    }