科目: 程式設計
學號: 考試日期 2021.4.8
第一次期中考 姓名:
考試時間 08:20-10:00
Open book; turn off computer & mobile phone
1
2
3
4
5
6
7
8
9
10

  1. (10%) For the following code, if we input "Turing Award" (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;
    }


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

    // 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;
    }


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

    // Define a Class
    #include <iostream>

    class Point {
    public:
        int x;
        int y;
    }

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


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

    // Constructor
    #include <iostream>

    class Point {
    public:
        Point(int a, int b = 8)
        {
            x = a;
            y = b;
        }

        int x;
        int y;
    };

    int main()
    {
        Point p1(20, 21);
        Point p2(8);
        std::cout << p1.x << p2.y << std::endl;
        return 0;
    }


  5. (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;
    }



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

    // Function Pointers
    #include <iostream>
    using std::cout;
    using std::endl;

    int add(int a, int b) { return a + b; }
    int subtract(int a, int b) { return a - b; }
    int multiply(int a, int b) { return a * b; }
    int divide(int a, int b) { return a / b; }

    int main()
    {
        int (*f)[](int, int) = {add, subtract, multiply, divide};
        for (int i=0; i<sizeof(f)/sizeof(f[0]); i++)
            cout << f[i](8, 4) << endl;
        return 0;
    }




  7. (10%) 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 = 8;
        cout << ++n;
        f1(n);
        f2(n);
        cout << ++n << 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).

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

    int sum(int x, float y)
    {
        return x + static_cast<int>(y);
    }

    float sum(int x, float y)
    {
        return static_cast<float>(x) + y;
    }

    int main()
    {
        cout << sum(4, 8.0) + 1 << 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).

    // void* and Little Endian
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    short a[2] = { 0x4546, 0x4748 };
    void* p = &a;
    char* p1 = static_cast<char*>(p);
    short* p2 = static_cast<short*>(p);
    int* p3 = static_cast<int*>(p);
    cout << *p1 << ' ' << *p2 << ' ' << *p3 << 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).

    // Copy Constructor and Operator Overloading

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

    class Vector {
    public:
    // Constructor
    Vector(int a=0, int b=0) { x = a; y = b; }
    // Copy Constructor
    Vector(const Vector& b) { x = b.x; y = 2 * b.y; }
    // Assignment Operator
    Vector operator=(const Vector& b) { x = 2 * b.x; y = b.y; }
    // Addition Operator
    Vector operator+(Vector b) {
    return Vector(this->x + b.x, this->y + b.y);
    }
    // Stream Insertion Operator
    friend ostream& operator<<(ostream& output, const Vector& b) {
    output << '(' << b.x << ',' << b.y << ')';
    return output;
    }

    private:
    int x;
    int y;
    };

    int main()
    {
    Vector v(4, 8);
    Vector v1 = v;
    Vector v2(v);
    Vector v3; v3 = v;
    cout << v << v1 << v2 << v3 << endl;
    return 0;
    }