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







7
8
9
10



11
12
13
14
15
16
17
18
19
20







  1. (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).
    // operator association (P.210)
    #include <iostream>

    int main()
    {
        if ( 2 * 6 == 3 * 4 == 12 )
            std::cout << "True" << std::endl;
        else
            std::cout << "False" << std::endl;
        return 0;
    }

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

    // comma operator
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int i = 4, j = 17;
        cout << (1, 2, 3) + (4, 5, 6) << endl;
        cout << (i=0, i<10, ++i) + (j=100, j>0, --j) << endl;
        return 0;
    }

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

    // break vs. continue
    #include <iostream>

    int main()
    {
        int i = 5, s = 5;
        for (i=0; i<9; i++)
        switch (i % 2)
        {
            case 0:
                s += 2;
                break;
            case 1:
                ++s;
                continue;
        }
        std::cout << s << std::endl;
        return 0;
    }

  4. (5%) 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[10] = { 0, 4, 9, 2, 9, 1, 5, 2, 2, 5 };
        int* p0 = a;
        int* p3 = &a[3];
        cout << p3 - p0 << '\t'
             << reinterpret_cast<char*>(p3) - reinterpret_cast<char*>(p0) << '\t'
             << *reinterpret_cast<char*>(p3) -
                *reinterpret_cast<char*>(p0)
             << endl;
        return 0;
    }

  5. (5%) 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, 5);
        a.show();
        a.setNumerator(2).setDenominator(7);
        a.show();
        return 0;
    }

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

    // When is static objects destroyed
    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;

    class Object
    {
    public:
        Object(string s): name(s)
        { cout << name << "\tconstructed.\n"; }
        ~Object()
        { cout << name << "\tdestroyed.\n"; }
    private:
        string name;
    };

    void f(string s)
    { static Object a(s);
      Object b("Bravo");
    }

    int main()
    {
        Object a("Alpha");
        f("Foxtrot");
        f("India");
        return 0;
    }



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

    // Overloading Binary Operators
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::ostream;

    class Rational {
    friend ostream& operator<<(ostream& output, const Rational& b) {
        output << b.numerator << '/' << b.denominator;
        return output;
    }

    public:
        Rational(int n=0, int d=1): numerator(n), denominator(d) {}
        Rational operator+(const Rational& b) {
            return Rational(numerator*b.denominator + denominator*b.numerator,
                    numerator * denominator);
        }
    private:
        int numerator;
        int denominator;
    };

    int main()
    {
        Rational a(1, 2);
        Rational b(1, 3);
        Rational c;
        c = a + b;
        cout << c << endl;

        return 0;
    }

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

    // vector of vector
    #include <iostream>
    #include <vector>
    using std::vector;
    using std::cout;
    using std::endl;

    int main()
    {
        typedef vector<int> Row;
        typedef vector<Row> Matrix;
        Row r(3);
        Matrix A;
        A.push_back(r);
        A.push_back(r);
        A.push_back(r);
        for (int i=0; i<3; ++i)
            for (int j=0; j<3; ++j)
                A[i][j] = 10*i + j;
        for (int i=0; i<3; ++i)
            cout << A[i].at(1) << ' ';
        cout << endl;
        return 0;
    }
  9. (5%) 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).

    // Function Pointers (P.393)
    #include <iostream>

    int add(int a, int b)
    { return a + b; }

    int sub(int a, int b)
    { return a - b; }

    int mul(int a, int b)
    { return a * b; }

    int divide(int a, int b)
    { return a / b; }

    int main()
    {
    int *f[](int, int) = { add, sub, mul, divide };

    for (int i=0; i<4; i+=2)
    {
    std::cout << f[i](33, 5) << std::endl;
    }

    return 0;
    }

  10. (5%) 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,5);
    ppoly2->set_values (4,5);
    cout << ppoly1->area() << '\n';
    cout << ppoly2->area() << '\n';
    return 0;
    }

  11. (5%) For the following code, if we input "Game of Thrones" (without the quotation marks) and press ENTER, what will be the output?
    // cin, strlen, sizeof
    #include <iostream>
    #include <cstring>
    using std::cin;
    using std::cout;

    int main()
    {
        char s[20];
        cin >> s;
        cout << strlen(s) << '\t' << sizeof(s) << '\n';
        return 0;
    }

  12. (5%) For the following code, if we input "Game of Thrones" (without the quotation marks) and press ENTER, what will be the output?

    // getline(), string.length()
    #include <iostream>
    #include <string>
    using std::cin;
    using std::cout;
    using std::string;

    int main()
    {
        string s;
        getline(cin, s);
        cout << s.length() << '\n';
        return 0;
    }

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

    // struct
    #include <iostream>

    struct Point {
        int x;
        int y;
    };

    int main()
    {
        Point p1 = {3, 4};
        Point p2 = {5, 4};
        std::cout << p1.x << p2.y << std::endl;
        return 0;
    }

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

    // Constructor
    #include <iostream>

    class Point {
    public:
        Point(int a = 4, int b = 5): x(a), y(b) {}  

        int x;
        int y;
    };

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

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

    // for-loop
    #include <iostream>

    int main()
    {
        int sum = 0;
        int i;
        for (i=99; i>0; )  
            i = i - 1;
            sum += i;

        std::cout << i << '\t' << sum << std::endl;
        return 0;
    }

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

    // while-loop
    #include <iostream>

    int main()
    {
        int total = 0;
        int n = 0;
        while (n <=100)
        {
            n += 2;
            total += n;
        }
        std::cout << n << '\t' << total << std::endl;
        return 0;
    }



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

    // 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 = 1;
        cout << ++n;
        f1(n);
        f2(n);
        cout << ++n << endl;

        return 0;
    }


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

    // function overloading
    #include <iostream>
    using std::cout;
    using std::endl;

    int sum(int x, float y)
    {
        return x;
    }

    float sum(float x, int y)
    {
        return x;
    }

    int main()
    {
        cout << sum(2.5, 3) + sum(1, 1.5) << endl;
        return 0;
    }
  19. (5%) 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).

    // Inheritance
    #include <iostream>

    class Base {
    public:
    Base(int n): value(n) {}
    int getValue() const { return value; }
    protect:
    int value;
    };

    class Derived: public Base {
    public: Derived(int n): Base(n) {}
    };

    int main()
    {
    Base a(4);
    Derived b(17);
    std::cout << a.getValue() << b.getValue() << std::endl;
    return 0;
    }

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

    void f(int a[], int i)
    {
    if (a[i] > 0)
    {
    f(a, i*2);
    f(a, i*2+1);
    cout << a[i] << ' ';
    }
    }

    int main()
    {
    int a[32] = {15, 8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15 };
    f(a, 1);
    cout << std::endl;
    return 0;
    }