科目名稱: 程式設計
第二次期中考 開課系所:資工系 考試日期 2020.4.30
系所別:
年級:
學號:
姓名:
考試時間 08:20-10:00
Open book; turn off computer & mobile phone
1
2
3
4
5
6
7
8
9
10
11

  1. (10%) 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).
    // array and pointer
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        int a[3][2] = { { 11, 12 }, { 21, 22 }, { 31, 32 } };
        int* p = a[0];
        int i, j;
        for (i=0; i<3; ++i)
            for (j=0; j<2; ++j)
                *( p + 2 * i + j ) *= 2;
        for (i=0; i<3; ++i)
        {
            for (j=0; j<2; ++j)
                cout << a[i][j] << '\t';
            cout << '\n';
        }
        return 0;
    }

  2. (10%) 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).

    // char**
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        const char* a[] = { "Alpha", "Bravo", "Charlie", "Delta" };
        const char** p = a + 1;
        cout << *p << endl;
        cout << **p << endl;
        return 0;
    }

  3. (10%) 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).

    // pass-by-reference
    #include <iostream>
    using std::cout;
    using std::endl;

    void f1(int& n)
    { cout << --n; }

    void f2(int n)
    { cout << --n; }

    int main()
    {
        int n = 430;
        cout << --n;
        f1(n);
        f2(n);
        cout << --n << endl;

        return 0;
    }

  4. (10%) Assume the following code is compiled and run on an Intel x86 CPU.  The compiler allocates 4 bytes for each integer.  Please predict the output.

    // reinterpret_cast
    // Assume an x86 CPU and sizeof(int) == 4

    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    int main()
    {
        int a[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };
        int* p0 = a;
        int* p5 = &a[5];
        cout << p5 - p0 << '\t'
             << reinterpret_cast<char*>(p5)  - reinterpret_cast<char*>(p0) << '\t'
             << *reinterpret_cast<char*>(p5) - *reinterpret_cast<char*>(p0)
             << endl;
        return 0;
    }

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

    // cascaded function calls (P.468)
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    class Rational
    {
    public:
        Rational(int n, int d): numerator(n), denominator(d) {};
        Rational& setNumerator(int i)   { numerator = i;   return *this; }
        Rational  setDenominator(int i) { denominator = i; return *this; }
        void show() { cout << numerator << '/' << denominator << endl; }
        int numerator;
        int denominator;
    };

    int main()
    {
        Rational a(1, 2);
        a.setNumerator(3).setDenominator(4).setDenominator(5);
        a.show();
        a.setNumerator(6).setDenominator(7).setDenominator(8);
        a.show();
        return 0;
    }

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

    // Copy Constructor
    #include <iostream>

    class Point {
    public:
        Point(int a = 4, int b = 5): x(a), y(b) {}
        Point(Point& b) { x = b.x * 2; y = b.y; }


        int x;
        int y;
    };

    int main()
    {
        Point p1(1, 7);
        Point p2 = p1;
        Point p3(p1);
        std::cout << p2.x << p3.x << std::endl;
        return 0;
    }



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

    // Overloading Assignment Operator
    #include <iostream>

    class Point {
    public:
        Point(int a = 4, int b = 5): x(a), y(b) {}
        Point operator=(const Point& b) {
            x = b.x * 2; y = b.y;
            return *this; // Return a reference or a copy?
        }

        int x;
        int y;
    };

    int main()
    {
        Point p1(1, 7);
        Point p2(2, 8);
        Point p3(3, 9);
        p1 = p2 = p3;
        std::cout << p1.x << p1.y << std::endl;
        (p3 = p1) = p2;
        std::cout << p3.x << p3.y << std::endl;
        return 0;
    }


  8. (10%) 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).

    // protected
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;

    class Human {
    public:
        Human(string s): name(s) { cout << '+' << s; }
        ~Human() { cout << '-' << name; }
    private:
        string name;
    };

    class Employee : public Human {
    public:
        Employee(string n, string d) : Human(n) {
            department = d;
            cout << '+' << name << department;
        }
        ~Employee() { cout << '-' << name << department; }
    protected:
        string department;
    };

    int main()
    {
        Human a("A");
        Employee e("E", "F");
        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).

    // Software Engineering Observation 12.7
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;

    class String {
    public:
    String(string s): txt(s) { cout << "+S" << txt; }
    ~String() { cout << "-S" << txt; }
    string txt;
    };

    class Human {
    public:
    String name;
    Human(string s): name(s) { cout << "+H" << name.txt; }
    ~Human() { cout << "-H" << name.txt; }
    };

    class Employee : public Human {
    public:
    Employee(string n, string d): Human(n), department(d) {
    cout << "+E" << department.txt;
    }
    ~Employee() { cout << "-E" << department.txt; }
    private:
    String department;
    };

    void f1() {
    Human a("A");
    }

    void f2() {
    Employee e("E", "F");
    f1();
    }

    int main()
    {
    Human b("B");
    Employee c("C", "D");
    cout << '\n';
    f2();
    cout << '\n';
    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).
    // Polymorphism and Virtual Functions
    #include <iostream>
    using std::cout;

    class Polygon {
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { width=a; height=b; }
    };

    class Rectangle: public Polygon {
    public:
    virtual int area()
    { return width*height; }
    };

    class Triangle: public Polygon {
    public:
    virtual int area()
    { return width*height/2; }
    };

    int main () {
    Rectangle rect;
    Triangle tri;
    Polygon* ppoly1 = &rect;
    Polygon* ppoly2 = &tri;
    ppoly1->set_values (4,9);
    ppoly2->set_values (4,9);
    cout << ppoly1->area() << '\n';
    cout << ppoly2->area() << '\n';
    return 0;
    }

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

    // Overloading Increment Operator
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    class Rational
    {
    public:
    Rational(int n, int d): numerator(n), denominator(d) {};
    Rational& operator++() {
    ++numerator;
    return *this;
    }
    Rational operator++(int n) {
    Rational temp(numerator, denominator);
    denominator++;
    return temp;
    }
    void show() { cout << numerator << '/' << denominator << '\t'; }
    int numerator;
    int denominator;
    };

    int main()
    {
    Rational a(1, 2);
    Rational b(3, 4);
    (++a).show();
    b++.show();
    a.show();
    b.show();
    return 0;
    }