國立暨南國際大學 104 學年度第二學期 第一次期中考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2016.4.7
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
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).

    // Fundamental Data Types (P.45)
    // The sizeof Operator (P.154)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
       char a = 1;
       unsigned char b = 2;
       short c = 3;
       unsigned short d = 4;
       int e = 5;
       unsigned int f = 6;

       cout << sizeof(a)*sizeof(a) + sizeof(b)*sizeof(b) +
           sizeof(c)*sizeof(c) + sizeof(d)*sizeof(d) +
           sizeof(e)*sizeof(e) + sizeof(f)*sizeof(f) << endl;
       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).

    // Range of Values (P.45)
    // Type Conversion (P.64)
    // Overflow (P.125)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
       unsigned char b = 102;
       unsigned char a = b*b;
       unsigned short d = 65535;
       short c = static_cast<short>( d );
       int e = a + c;

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

    // for Loop (P.140)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        unsigned short points[] = { 33, 7, 94, 68, 18, 6, 71, 70, 99, 85 };
        unsigned short K = sizeof points / sizeof( points[0] );
        unsigned short i=0, max, max2, p, p2;
        for (max=points[i++]; i < K ; i++)
        {
            if (max < points[i])
            {
                max = points[i];
                p = i;
            }
        }

        for (max2 = 0, i=0; i < K; i++)
        {
            if (max2 < points[i] && points[i] != max)
            {
               max2 = points[i];
               p2 = i;
            }
        }

        cout << p  << '\t' << max  << '\n';
        cout << p2 << '\t' << max2 << '\n';
        return 0;
    }


  4. (10%) Suppose you compile and run the following code on a 64-bit computer (e.g. STU.ipv6.club.tw).  What will be its output?

    // Pointers to char (P.150)
    // sizeof (P.154)

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

    int main()
    {
        const char name[][10] = { "Alice", "Bob", "Charlie", "Dennis", "Emily" };
        const char* pstr[] = { "Alice", "Bob", "Charlie", "Dennis", "Emily" };

        cout << sizeof(name) << '\t'
                << sizeof(name[1]) << '\t'
                << sizeof(name[1][2]) << endl;
        cout << sizeof(pstr) << '\t'
                << pstr[1] << '\t'
                << *(pstr[1] + 2) << 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).

    // Type Conversion (P.64)
    // switch (P.105)

    #include <iostream>
    using std::cout;

    int main()
    {
        int even = 0, odd = 0;
        char str[] = "BELGIUM"; // 'A' == 65
        for (int i=0; i<7; i++)
            switch ( str[i] % 2 )
            {
                case 0:
                    even++;
                case 1:
                    odd++;
            }
        cout << "odd = " << odd << '\n'
             << "even = " << even << '\n';
        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).

    // Initialize an Array (P.172)
    // Increment Operator (P.59)

    #include <iostream>
    using std::cout;

    int main()
    {
        int data[5] = { 3 * 5 };
        int i;
        for (i=0; i<5; ++i)
            data[i] = i;
        for (i=0; i<5; i++)
            cout << data[++i];
        return 0;
    }

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

    // continue (P.116) vs. break (P.115)
    #include <iostream>

    int main()
    {
        const int K = 5;
        int count = 0;
        int i, j;
        for (i=K; i>0; i--)
            for (j=K; j>0; j--)
            {
                ++count;
                if (i == j) break;
            }
        std::cout << count << 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).

    // Short-Circuit
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    int i, j, n = 0, m = 0;
    for (i=0; i<10; i++)
    for (j=0; j<10; j++)
    if (i<j && n++ > 0)
    m++;
    cout << n << m << endl;
    return 0;
    }

  9. (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).
    // Math Library
    // Type Conversion (P.63)
    #include <iostream>
    #include <cmath>
    using std::cout;
    using std::endl;

    int main()
    {
    cout << (9/4)+1 << '\t'
    << floor(9/4)+1 << '\t'
    << (-9/4)+1 << '\t'
    << floor(-9/4)+1 << '\t'
    << (-9.0/4)+1 << '\t'
    << floor(-9.0/4)+1 << '\n';
    return 0;
    }

  10. (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;

    void swap1(int a, int b)
    {
      int temp;
      temp = a; a = b; b = temp;
    }

    void swap2(int* a, int* b)
    {
      int temp;
      temp = *a; *a = *b; *b = temp;
    }

    void swap3(int& a, int& b)
    {
      int temp;
      temp = a; a = b; b = temp;
    }

    void print(int a, int b)
    {
        cout << a << '\t' << b << '\n';
    }

    int main()
    {
        int x = 10;
        int y = 20;
        swap1( x,  y); print(x, y);
        swap2(*x, *y); print(x, y);
        swap3(&x, &y); print(x, y);
        return 0;
    }

  11. (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).
    // Transposition Cipher
    #include <iostream>
    #include <cstring>
    using std::cout;
    using std::endl;

    int main()
    {
    const unsigned short N = 4;
    char matrix[N * N + 1];
    char plaintext[] = "Glp oerFo iogAlolabo iunGlt m tspscsa khrblAkaaA";

    char ciphertext[ sizeof(plaintext) ];
    char *p = plaintext;
    unsigned i, j;

    while (p < plaintext + strlen(plaintext) )
    {
    for (i=0; i<N; i++)
    for (j=0; j<N; j++)
    {
    matrix[i*N + j] = *p ? *p : 'A';
    ++p;
    }

    for (j=0; j<N; j++)
    for (i=0; i<N; i++)
    cout << matrix[i*N + j];
    }
    cout << endl;

    return 0;
    }