Midterm Exam (3)
Introduction to Computer Science
NCNU CSIE

Date: December 29th, 2010
Time: 14:10-16:00
Open book; turn off computer & mobile phone

  1. (10%) What will be the output of the following program?
    #include <iostream>
    int main() // Understanding bitwise operators.
    {
    unsigned s = 1229;

    int i = (s >> 5) & ~(~0 << 4);
    std::cout << i << std::endl;
    }
  2. (10%) What will be the output of the following program?

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

    int main()
    { 
        int m=25, n=13;
        m -= n += 2;
        cout << m << ' ' << n << endl;
        return 0;
    }

  3. (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).
    #include <iostream>     // Reference to a variable
    using std::cout;
    using std::endl;

    int main()
    {
        int value = 12;
        int& rvalue = &value;
         value += 29;
        cout << value + rvalue << endl;
    }

  4. (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).
    // Checksum
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    { int cksum;
    char *p;
    char buf[8192] = "December";

    /*
    * Draft 8 POSIX 1003.2:
    *
    * s = sum of all bytes
    * r = s % 2^16 + (s % 2^32) / 2^16
    * cksum = (r % 2^16) + r / 2^16
    */

    cksum = 0;
    for (p = buf; *p; ++p)
    cksum += *p;

    cksum = (cksum & 0xffff) + (cksum >> 16);
    cksum = (cksum & 0xffff) + (cksum >> 16);
    cout << cksum << endl;
    return (0);
    }

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

    int main()
    {
        int value[10] = { 1, 3, 5, 7, 9};
        int sum = 0;

        for (int i=0; i<10; i += 2)
            value[i] = i;
        for (int i=0; i<10; i++)
            sum += value[i];
        cout << sum << endl;
        return 0;
    }
  6. (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).

    // break vs. continue
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    { 
        int sum=0, i = 1;
        for (i=1; i<10; i++)
            if (i++ == 5) break;
            sum += i++;
        cout << i << '\t' << sum << endl;
        return 0;
    }

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

    // String pointers vs. string arrays.
    #include <iostream>

    using std::cout;
    using std::endl;

    int main()
    {
    char str[10] = "December";
    char* p = str;
    cout << str << endl;
    cout << p++ << endl;
    for ( ; *p != '\0'; p++)
    *p ^= 0x20;
    cout << p << endl;
    cout << str << endl;
    }
  8. (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).
    #include <iostream>
    using std::cout;
    using std::endl;

    // Function to eliminate spaces from a string
    void eatspaces(char* str)
    {
    int i(0); // 'Copy to' index to string
    int j(0); // 'Copy from' index to string

    while((*(str + i) = *(str + j++)) != '\0') // Loop while character
    // copied is not \0
    if(*(str + i) != ' ') // Increment i as long as
    i++; // character is not a space
    return;
    }

    int main()
    {
    char expr[] = "2 + 38-5";
    eatspaces(expr);
    cout << expr[3] << expr[7] << endl;
    return 0;
    }

  9. Check whether the following program has any mistake. If yes, point out the mistakes and correct them; if no, predict the output of the program.
    #include <iostream>
    using std::cout;

    int sum(int arg1, int arg2, int arg3 = 30, int arg4 = 40);

    int main()
    {
    cout << sum(5, 5, , 5) << "\n";
    return 0;
    }

    int sum(int arg1, int arg2, int arg3, int arg4)
    {
    return arg1 + arg2 + arg3 + arg4;
    }
  10. Check whether the following program has any mistake. If yes, point out the mistakes and correct them; if no, predict the output of the program.
    #include <iostream>
    using std::cout;

    int sum(int a, int b);
    int diff(int a, int b);
    int product(int a, int b);

    int main()
    {
    int (*pfun[3])(int, int) = {sum, diff, product};
    cout << pfun[1]( (*pfun)(12, 2), (*(pfun+2))(9, 2) ) << std::endl;
    return 0;
    }

    int sum(int arg1, int arg2)
    { return arg1 + arg2; }

    int diff(int arg1, int arg2)
    { return arg1 - arg2; }

    int product(int arg1, int arg2)
    { return arg1 * arg2; }