Mid-term Exam (2)
Introduction to Computer Science
NCNU CSIE

Date:  March 26, 2013
Time: 14:10-16:00
Open book; turn off computer & mobile phone

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

    // Class definition
    #include <iostream>
    #include <cstring>

    class CMessage
    {
    public:
        CMessage(char* s)
        {
            str = new char[strlen(s) + 1];
            strcpy(str, s);
        }

        ~CMessage()
        {
            delete str;
            str = NULL;
        }

    private:
            char* str;   
    }

    int main()
    {
        CMessage city("Vatican");   
        std::cout << sizeof(city) << std::endl;   
        return 0;
    }



  2. (10%) What will be the output of the following program?

    // Copy Constructor
    #include <iostream>

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

        CBox(int lv = 0, int wv = 0, int hv = 0)    // Constructor
            : m_Length(lv), m_Width(wv), m_Height(hv)
        { }

        CBox(const CBox& aBox)            // Copy Constructor
        {
            m_Length = aBox.m_Length;
            m_Width  = aBox.m_Width;
            m_Height = aBox.m_Height * 2;
        }

        int Volume()
        {
            return m_Length * m_Width * m_Height;
        }
    };

    int main()
    {
      CBox box1(3, 4, 5);               // Declare box1 of type CBox
      CBox box2 = box1;                 // Declare box2 of type CBox
      CBox box3;                        // Declare box3 of type CBox
      box3 = box1;
      std::cout << box2.Volume() << std::endl;
      std::cout << box3.Volume() << std::endl;
      return 0;
    }


  3. (10%) What will be the output of the following program?

    // Default values for constructor arguments
    #include <iostream>
    using std::cout;
    using std::endl;

    class CRational
    {
    private:
        int denominator;
        int numerator;
    public:
        void Print()
        {
            cout << numerator << "/" << denominator << endl;
        }
        CRational()    // Default Constructor
        {
            numerator = 0;
            denominator = 1;
        }
        CRational(int a, int b = 1)
        {
            numerator=a;
            denominator=b;
        }
    };

    int main()
    {
        CRational p(3, 4);
        CRational q;
        CRational r(5);
        p.Print(); q.Print(); r.Print();
        return 0;
    }







  4. (10%) What will be the output of the following program?

    // Overloading the Assignment Operator
    #include <iostream>
    using std::endl;
    using std::cout;

    class CData
    {
        public:
            int* pdata;

        CData(int n=0)
        {
            pdata = new(int);
            *pdata = n;       
        }

        ~CData()
        { delete pdata; }

        CData& operator=(const CData& d)
        {
            if (this == &d)
               return *this;
            delete pdata;
            pdata = new int;
            *pdata = ( *(d.pdata) ) --;
            return *this;
        }
    };


    int main()
    {
        CData a(3);
        CData b(2);
        CData c(9);
        a = b = c;
        cout << *(a.pdata) << *(b.pdata) << *(c.pdata) << endl;
        return 0;
    }



  5. (10%) What will be the output of the following program?

    //
    Overloading the Assignment Operator
    #include <iostream>
    using std::endl;
    using std::cout;

    class CData
    {
        public:
            int* pdata;

        CData(int n=0)
        {
            pdata = new(int);
            *pdata = n;       
        }

        ~CData()
        {
            cout << "Destructor called to release the memory storing "
                    << *pdata << endl;
            delete pdata;
        }

        CData& operator=(const CData& d)
        {
            if (this == &d)
               return *this;
            delete pdata;
            pdata = new int;
            *pdata = ( *(d.pdata) ) --;
            return *this;
        }
    };


    int main()
    {
        CData a(3);
        CData b(2);
        CData c(9);
        (a = b) = c;
        cout << *(a.pdata) << endl;
        return 0;
    }


  6. (10%) What will be the output of the following program?

    //
    Overloading the Assignment Operator
    #include <iostream>
    using std::endl;
    using std::cout;

    class CData
    {
        public:
            int* pdata;

        CData(int n=0)
        {
            pdata = new(int);
            *pdata = n;      
        }

        ~CData()
        {
            cout << "Destructor called to release the memory storing "
                    << *pdata << endl;
            delete pdata;
        }

        CData& operator=(const CData& d)
        {
            if (this == &d)
               return *this;
            delete pdata;
            pdata = new int;
            *pdata =  ++*(d.pdata);
            return *this;
        }
    };


    int main()
    {
        CData a(3);
        CData b(2);
        CData c(9);
        a = b = c;
        cout << *(a.pdata) << endl;
        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).

    // Static data members
    #include <iostream>

    class CData
    {
    public:
        int value;
        static int nCapacity;
        CData(int n): value(n) { --nCapacity; }
    };

    CData::nCapacity = 5;

    int main()
    {
        CData a(5);
        CData b(4);
        std::cout << a.nCapacity << std::endl;
        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).

    // Size of a string
    #include <iostream>
    #include <cstring>

    class CMessage
    {
    public:
        char str[20];   
        CMessage(char* s)
        { strcpy(str, s); }
    };

    int main()
    {
        CMessage city("Vatican");   
        std::cout << sizeof(city) << std::endl;
        std::cout << strlen(city.str) << std::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).
    // Size of an object
    #include <iostream>
    #include <cstring>

    class CMessage
    {
    public:
    char* str;
    CMessage(char* s)
    {
    str = new char[strlen(s) + 1];
    strcpy(str, s);
    }

    ~CMessage()
    {
    delete str;
    str = NULL;
    }
    };

    int main()
    {
    CMessage city("Vatican");
    std::cout << sizeof(city) << std::endl;
    // Assume that you are running this program on a 32-bit operating system
    std::cout << strlen(city.str) << std::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).
    // Access Control
    #include <iostream>

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

    CBox(int lv, int wv, int hv) // Constructor
    {
    m_Length = lv;
    m_Width = wv;
    m_Height = hv;
    }

    public:
    int Volume()
    {
    return m_Length * m_Width * m_Height;
    }
    };

    int main()
    {
    CBox box1(2, 3, 4); // Declare box1 of type CBox
    std::cout << box1.Volume() << std::endl;
    return 0;
    }


  11. (10%) Consider the (unmodified) code of Ex8_14.cpp.  If you provide "is is is *" as the input, what would be the output?