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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2017.3.29
系所別:
年級:
學號:
姓名:
考試時間 08:30-10:30
1
2

3  

4
5  
6  
7



8  
9  
10

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

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

    int main()
    {
        signed short n = 32767;
        n += 2;
        cout << n << endl;

        unsigned char c = 'w';
        c = c * 3;
        cout << c << 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).

    // Conditional Operator (P.103)
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        for (int i=0; i<10; i++)
            cout << (i%2 ? i : ++i) << 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).

    // Bitwise Operator (P.67)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        unsigned char m = 0x4d;
        unsigned char n = 0x6e;
        cout << m << n << endl;
        cout << static_cast<char>(m & n) << endl;
        cout << static_cast<char>(m | n) << endl;
        cout << static_cast<int>(m ^ n) << 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).

    // continue (P.116) vs. break (P.115)
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        const int K=10;
        int m=0, n=0;
        int i, j;

        for (i=0; i<K; i++)
            for (j=0; j<K; j++)
            {
                if (i+j == K)
                    continue;
                ++m;
            }

        for (i=0; i<K; i++)
            for (j=0; j<K; j++)
            {
                if (i+j == K)
                    break;
                ++n;
            }

        cout << m << n << 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).

    // ASCII Code (P.42, P.95)
    #include <iostream>
    #include <cstring>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        char text[]="this is a string.";
        char *p = text;
        bool afterSpace = false;
        while (*p)
        {
            if (*p == ' ')
                afterSpace = true;
            else
            {
                if (afterSpace)
                    *p &= 0xDF;
                afterSpace = false;
            }
            ++p;
        }
        cout << text << 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).

    // switch (P.105)
    #include <iostream>
    #include <cstring>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
        char msg[] = "SECRETMEETING";
        int even = 0, odd = 0;
        int i;
        for (i=0; i<strlen(msg); i++)
        {
            switch (msg[i] % 2)
            {
                case 0:
                    ++even;
                case 1:
                    ++odd;
            }
        }
        cout << "Even: " << even << endl
             << "Odd: "  << odd  << endl;
        return 0;
    }

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

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

    int main()
    {
        char strAry[] = "This is a string";
        char *aryPtr = strAry;
        short *shortPtr = (short*)strAry;

        cout << "[Line01] sizeof(strAry)=" << sizeof(strAry) << endl;
        cout << "[Line02] sizeof(aryPtr)=" << sizeof(aryPtr) << endl;
        cout << "[Line03] sizeof(*aryPtr)=" << sizeof(*aryPtr) << endl;
        cout << "[Line04] strAry=" << strAry << endl;
        cout << "[Line05] aryPtr=" << aryPtr << endl;
        cout << "[Line06] *aryPtr=" << *aryPtr << endl;
        cout << "[Line07] *aryPtr+1=" << *aryPtr+1 << endl;
        cout << "[Line08] *(aryPtr+1)=" << *(aryPtr+1) << endl;
        cout << "[Line09] sizeof(shortPtr)=" << sizeof(shortPtr) << endl;
        cout << "[Line10] sizeof(*shortPtr)=" << sizeof(*shortPtr) << 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).

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

    int main()
    {
    const int MAX_SIZE = 80;
    char plaintext[] = "It's easy to read this sentence";
    char buffer[MAX_SIZE];
    char* p = plaintext;
    char* p2 = buffer;
    while (*p)
    if ('A' <= *p & *p <= 'Z' || 'a' <= *p & *p <= 'z')
    *p2++ = (*p++ & 0xDF);
    else
    p++;
    *p2 = '\0';

    p=buffer;
    while (*p)
    {
    cout << *p++;
    if ((p - buffer) % 5 == 0)
    cout << ' ';
    }
    cout << 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).
    // Transposition Cipher
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {
    char cipher[] = "CFITSLINMEOVGECCEAERKOTTE";
    int i, j, p=0;
    char matrix[5][5];
    for (j=0; j<5; j++)
    for (i=4; i>=0; i--)
    matrix[i][j] = cipher[p++];

    for (i=0; i<5; i++)
    for (j=0; j<5; j++)
    cout << matrix[i][j];
    cout << endl;
    return 0;
    }
  10. (10%) Suppose you compile and run the following code on a 64-bit computer.  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).
    // Pointers to Array of Characters (P.155)
    #include <iostream>
    using std::endl;
    using std::cout;

    int main()
    {
        const char* s[] = { "NCNU", "~/-\\^" };
        char c = s[1][2];
        const char* p = &s[1][2];
        cout << sizeof (s) << endl;
        cout << sizeof (s[1][2]) << endl;
        cout << c << endl;
        cout << *(++p) << endl;
        cout << p << endl;
        return 0;
    }