Final Exam
C++ Programming
NCNU CSIE

Date: June 23rd, 2009
Time: 14:10-16:10
Open book; turn off computer & mobile phone

  1. 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).
    // modulus
    #include <iostream>
    using std::cout;
    using std::endl;

    int mod(int a, int b)
    {
    do a -= b;
    while (a >= b);
    return a;
    }

    int main()
    {
    cout << mod(4, 7) << endl;
    }



  2. 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).
    // Caesar Cipher
    #include <iostream>
    using std::cout;
    using std::endl;

    void encrypt(char* a, int key)
    {
    while (*a)
    {
    if (*a >= 'A' && *a <='Z')
    *a = (*a - 'A' + key) % 26 + 'A';
    a++;
    }
    }

    int main()
    {
    char msg[] = "HELLO NCNU";
    encrypt(msg, 3);
    cout << msg << "\n";
    }

  3. 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).
    // Structure
    #include <iostream>
    using std::cout;

    struct Rectangle
    {
    int left;
    int top;
    int right;
    int bottom;
    };

    int main()
    {
    Rectangle R1 = {0,0,10,20};
    int area = (R1.right - R1.left) * (R1.bottom - R1.top);
    cout << area << "\n";
    }

  4. 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).
    // Copy Constructor
    #include <iostream>
    using std::cout;

    class CBox
    {
    public:
    int volume;
    char* color;
    CBox(int v, char* c = "Violet"): volume(v)
    {
    color = new char[strlen(c) + 1];
    strcpy(color, c);
    }
    CBox(const CBox& b)
    {
    color = new char[strlen(b.color) + 1];
    strcpy(color, b.color);
    volume = b.volume;
    }
    };

    int main()
    {
    CBox cigar(10);
    CBox chocolate(cigar);
    cout << chocolate.color << "\n";
    }


  5. 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).

    // Copy Constructor in a Derived Class
    #include <iostream>
    using std::cout;

    class CBox
    {
    public:
    int volume;
    CBox() { volume = 10; }
    };

    class CContainer : private CBox
    {
    char* color;
    public:
    CContainer()
    {
    color = new char[strlen("Pink")+1];
    strcpy(color, "Blue");
    }

    CContainer(CContainer& b)
    {
    color = new char[strlen(b.color)+1];
    strcpy(color, b.color);
    }

    void ShowColor()
    {
    cout << color << "\n";
    }
    };

    int main()
    {
    CContainer cigar;
    CContainer chocolate(cigar);
    chocolate.ShowColor();
    }

  6. 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).
    // Operator Overloading
    #include <iostream>
    using std::cout;

    class CList
    {
    public:
    int count;
    void Print() { cout << count << "\n"; }
    CList(int i) : count(i) {}
    CList& operator++() { count++; return *this; }
    CList operator++(int) { count<<=1; return *this; }
    };


    int main()
    {
    CList L2(5);
    ++L2; L2.Print();
    }


  7. 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).

    // Friendship
    #include <iostream>
    using std::cout;

    class CBox
    {
    private:
    int volume;
    public:
    CBox() : volume(20) {};

    friend int main();
    };

    int main()
    {
    CBox b1;
    cout << b1.volume << "\n";
    }

  8. Point out a printing error in the code on P.831.


  9. If we replace the OnDraw() function in P.715 as below, what result will be shown on the screen after you run the program?
    void CSketcherView::OnDraw(CDC* pDC)
    {
    CSketcherDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
    return;
    CList<CPoint, CPoint&> PointList;
    PointList.AddHead(CPoint(50,30));
    PointList.AddHead(CPoint(100,100));
    PointList.AddHead(CPoint(150,30));
    PointList.AddHead(CPoint(200,100));


    POSITION aPosition = PointList.GetHeadPosition();
    if (aPosition)
    pDC->MoveTo(PointList.GetNext(aPosition));

    PointList.GetNext(aPosition);
    while (aPosition)
    pDC->LineTo(PointList.GetNext(aPosition));
    }

  10. If we replace the OnDraw()function in P.715 as below, what result will be shown on the screen after you run the program?

    void CSketcherView::OnDraw(CDC* pDC)
    {
        CSketcherDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        if (!pDoc)
            return;   
        CList<CPoint, CPoint&> PointList;   
        PointList.AddHead(CPoint(100,100));
        PointList.AddHead(CPoint(150,30));
        PointList.AddHead(CPoint(50,30));
        PointList.AddTail(CPoint(200,100));

       
        POSITION aPosition = PointList.GetHeadPosition();
        if (aPosition)
            pDC->MoveTo(PointList.GetNext(aPosition));   
       
        while (aPosition)   
            pDC->LineTo(PointList.GetNext(aPosition));   
    }


  11. In the movie "Mona Lisa Smiles", Katherine Ann Watson (played by Julia Roberts) did not receive her doctor degree, so how does the students call her?