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

Date: March 3rd, 2010
Time: 14:10-16: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 CStack
    {
    public:
    CStack() // Default Constructor
    {
    index = 0;
    }
    void push(int i)
    {
    a[index++] = i;
    }
    int pop()
    {
    return a[--index];
    }
    private:
    #define StackSize 10
    int a[StackSize];
    int index;
    };

    int main() {
    CStack stack1;
    stack1.push(3); stack1.push(8);
    stack1.pop();
    cout << stack1.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, 4.0, 7.0) << "\n";
        cout << average(1,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 = 33;
        test(n);            // pass-by-value or pass-by-reference?
        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 = 33;
    for (i=1; i<6; i++)
    {
    int n = 0;
    for (int j=1; j<=i; j++)
    n += j; // scope of variable 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 << endl << "Constructor called.";
    }

    // 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 << endl
    << "Volume of box2 = "
    << box2.Volume();

    cout << endl;
    return 0;
    }