科目名稱: 程式設計
第一次期中考 開課系所:資工系 考試日期 2019.2.27
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
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 "Night Fury" (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%) For the following code, if we input "Night Fury" (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;
    }

  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
    // class
    #include <iostream>

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

    int main()
    {
        Point p1 = {1, 3};
        Point p2 = {2, 4};
        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 = 5)
        {
            x = a;
            y = b;
        }

        int x;
        int y;
    };

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

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

    // for-loop (P.189 Common Programming Error 5.3)
    #include <iostream>

    int main()
    {
        int sum = 0;
        int i;
        for (i=99; i>0; i-=2);
        {
            sum += i;
        }
        std::cout << i << '\t' << sum << std::endl;
        return 0;
    }

  6. (10%) 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 += 1;
            total += n;
        std::cout << n << std::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 = 1;
        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 << 2.5 + sum(1, 1.5) << 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).

    // Recursion: ex06_45.cpp
    #include <iostream>
    using std::cout;
    using std::endl;

    int mystery(int, int); // function prototype

    int main()
    {
    int x = 20, y = 19;
    cout << "The result is " << mystery(x, y) << endl;
    return 0;
    }

    int mystery(int a, int b)
    {
    if (b==1) // base case
    return a;
    else // recursion step
    return a + mystery(a, b-1);
    }

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

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

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