Mid-Term Exam (1')
C++ Programming
NCNU CSIE

Date: June 23rd, 2010
Time: 16:10-17:00
Open book; turn off computer & mobile phone

  1. (20%) 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).
    #include <iostream>
    using std::cout;

    class CQueue
    {
    public:
    CQueue() // Default Constructor
    {
    head = tail = 0;
    }
    void push(int i)
    {
    a[tail++] = i;
    }
    int pop()
    {
    return a[head++];
    }
    private:
    #define QueueSize 10
    int a[QueueSize];
    int head, tail;
    };

    int main() {
    CQueue q;
    q.push(3); q.push(2);
    q.pop(); q.push(6);
    cout << q.pop() << "\n";
    }

  2. (20%) 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).
    #include <iostream>
    using std::cout;

    float average(float a, float b);
    float average(float a, float b, float c);       // overloading

    int main()
    {
        cout << average(1.0, 5.2, 7.0) << "\n";
        cout << average(4,0, 5.0) << "\n";
        return 0;
    }

    float average(float a, float b)
    { return (a+b)/2.0; }

    float average(float a, float b, float c)
    { return (a+b+c)/3.0; }

  3. (20%) 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).
    #include <iostream>
    using std::cout;

    void test(int i)
    { cout << ++i << "\n"; }

    int main()
    {
        int n = 623;
        test(n);   // pass-by-value or pass-by-reference?
        cout << n << "\n";       
        return 0;
    }


  4. (20%) 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).
    #include <iostream>
    using std::cout;

    int main()
    {
    int i, n = 623;
    for (i=1; i<7; i++)
    {
    int n = 0;
    for (int j=1; j<=i; j++)
    n += j; // scope of variable n
    cout << n << "\n";
    }
    cout << n << "\n";
    return 0;
    }
  5. (20%) 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).
    #include <iostream> // Ex7_06.cpp
    using std::cout;
    using std::endl;

    class CBox // Class definition at global scope
    {
    public:
    double m_Length; // Length of a box in inches
    double m_Width; // Width of a box in inches
    double m_Height; // Height of a box in inches

    // Constructor definition
    CBox(double lv = 1.0, double bv = 2.0, double hv = 3.0)
    : m_Length(lv), m_Width(bv), m_Height(hv) // Initialization list
        {
        cout << "Constructor called." << endl;
        }

    CBox(const CBox& b)
    : m_Length(2*b.m_Length), m_Width(3*b.m_Width), m_Height(b.m_Height) // Initialization list
        {
        cout  << "Copy Constructor called." << endl;
        }

    // Function to calculate the volume of a box
    double Volume()
        { return m_Length*m_Width*m_Height; }

    };

    int main()
    {
    CBox box1; // Declare box1 - no initial values
    CBox box2 = box1; // Default Copy Constructor

    cout << "Volume of box2 = " << box2.Volume() << endl;
    return 0;
    }