Mid-term Exam (1)
Introduction to Computer Science
NCNU CSIE

Date: October 23rd, 2012
Time: 14:10-16:00
Open book; turn off computer & mobile phone

  1. (10%) Consider the following binary representation of a signed short integer (stored in one's complement).  Convert it to a decimal number.
    11000000 11000000
  2. (10%) Represent 336.1875 in IEEE single-precision floating-point format.


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

    // for-loop
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        const int max(4);
        int i, j, k;
        for (i=1; i<=max; i++)
            for (j=0; j<=i; j++)
            {
                for (k=0; k<max-j; k++)
                    cout << ' ';
                for (k=0; k<2*j+1; k++)
                    cout << '*';
                cout << endl;
            }
        for (i=1; i<max; i++)
        {
            for (j=1; j<max; j++)
                cout << ' ';
            cout << "***" << endl;
        }
        return 0;
    }



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

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

    int main()
    {
        int a=1, b=2, c=3;
        int i;
        for (i=1; i<3; i++)
        {
            static int a=10, c=30;    // P.96
            a = b = c++;
            cout << a << ' ' << b << ' ' << c << endl;
        }
        cout << a << ' ' << b << ' ' << c << endl;
        return 0;
    }


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

    // overflow
    #include <iostream>
    int main()
    {
        signed char a(3);
        for (int i=0; i<4; ++i,
    a = 4*a + 3)
            ;
        std::cout << static_cast<signed int>(a) << std::endl;
        return 0;
    }


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

    // static-cast
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char c1, c2 = 'H'; // ASCII code of 'H' is 72;
        cout << (c1 = c2++) << endl;
        cout << static_cast<short>(c1, c2) << 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).

    // increment and decrement
    #include <iostream>
    using std::endl;
    using std::cout;

    int main()
    {
        unsigned short a = 100, b = 200, c = 300;
        cout << (a++, b = --c) << endl;
        cout << a << ' ' << b << ' ' << c << endl;
        return 0;
    }
  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).
    // Conditional operator
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    unsigned short n = 23;
    do {
    cout << n << ' ';
    if (n == 1)
    break;
    } while ( n = n % 2 ? 3 * n + 1 : n / 2 );
    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).
    // break vs. continue
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    int i;
    for (i=1; i<10; i++)
    {
    if (i>2)
    if (i==5)
    break;
    else
    continue;
    cout << i << endl;
    }
    cout << i << endl;
    return 0;
    }
  10. (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.
    // exchange_sort.cpp 
    #include <iostream>
    #include <iomanip>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::setw;

    int main()
    {
    const int MAX = 4;
    int data[MAX] = { 2, 3, 1, 0};
    int i, j, temp;

    for (j=0; j<=MAX-2; j++)
    {
    for (i=j+1; i<=MAX-1; i++)
    {
    if (data[j] > data[i])
    {
    temp = data[j]; data[j]=data[i]; data[i]=temp;
    }
    }

    cout << endl << "Round " << j+1 << ":\t";
    for (i=0; i<MAX; i++)
    cout << data[i] << '\t';

    }
    return 0;
    }
  11. (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.
    // Array
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int y = 2012;
        int day = ( y-1 + y/4 - y/100 + y/400) % 7;
        int month_days[13] = { 0,
            31, 28, 31, 30, 31, 30,
            31, 31, 30, 31, 30, 31 };
        char weekdays[][10] = { "Sunday", "Monday",
            "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday" };
        int m = 10, d = 23;
        int i;

        if ( y % 4 == 0 ) month_days[2] = 29;    // leap year
        if ( y % 100 == 0 && y % 400 != 0 ) month_days[2] = 28;

        cout << "day = " << day << endl;
        for (i=1; i<m; i++)
            day += month_days[i];
        day += d -1;

        cout << "Today is " << weekdays[ day % 7 ] << endl;
        cout << "day = " << day << endl;
        return 0;
    }