Final Exam
C++ Programming
NCNU CSIE

Date: June 23rd, 2009
Time: 08:30-10:30
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(624, 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' AND *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)];
    strcpy(color, c);
    }
    CBox(const CBox& b)
    {
    c = new char[strlen(b.color)];
    strcpy(color, c);
    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("Violet")];
    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 main();
    };

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

  8. What is a modeless dialog?  How does it behave differently from a modal dialog?


  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.AddTail(CPoint(50,30));
    PointList.AddTail(CPoint(100,100));
    PointList.AddTail(CPoint(150,30));
    PointList.AddTail(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.AddTail(CPoint(100,100));
        PointList.AddTail(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) moved from California to Massachusetts to teach Arts History in a new school.  What is the name of the school?  (5 points for Chinese answer; 10 points for English answer; 7 points for slightly incorrectly spelled English words)