Mid-Term Exam (2)
Introduction to Computer Science
NCNU CSIE

Date: December 10th, 2008
Time: 14:10-17:00
Open book; turn off computer & mobile phone

  1. What will be the output of the following program?
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    cout << 2 << 2 << 2 << endl;
    cout << (2 << 2 << 2) << endl;
    cout << 2 << (2 << 2) << endl;
    cout << (2 << (2 << 2)) << endl;
    }

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

    int i = (s >> 3) & ~(~0 << 3);
    std::cout << i << std::endl;
    }
  3. 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;
    using std::endl;

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

    /*
    * 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);
    }
  4. What will be the output of the following program?
    #include <iostream>

    int main()
    {
    char c1 = 'A';
    std::cout << static_cast<char>(c1 | 0x20) << std::endl;

    char c2 = 'g';
    std::cout << static_cast<char>(c2 ^ 0x20) << std::endl;

    c1 ^= c2;
    c2 ^= c1;
    c1 ^= c2;
    std::cout << static_cast<char>(c1) << " , "
    << static_cast<char>(c2) << std::endl;

    (c2 >>= 6) <<= 1;
    c2 |= c2 << (1 << 2);
    std::cout << static_cast<char>(c2 ^ c1) << std::endl;
    }
  5. Show the output of the following program.

    #include <iostream>
    int main()
    {
    const int MAX = 100;
    int i = 1, sum = 0;
    for ( ; ++i <= MAX; sum += i++)
    ;
    std::cout << std::endl << "sum = " << sum
    << std::endl << "i = " << i
    << std::endl;
    return 0;
    }
  6. What will be the output of the following program?
    // bin2dec.c
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    char b[] = "11110101011";
    int n=0, i=0;
    while (b[i])
    n = (n<<1) + b[i++] - 48;
    cout << n << endl;
    return 0;
    }
  7. What will be the output of the following program?
    #include <iostream>

    int main()
    {
    for (int i = 1; i < 6; i++)
    {
    if (i==3)
    continue;
    std::cout << i << std::endl;
    }
    }
  8. 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.
    // dec2bin.c
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    int i=0;
    short a[16] = 1963;
    int k=sizeof(a) / sizeof(a[0]);
    for (i=0; a[i]>1; i++)
    {
    a[i+1] = a[i] / 2;
    a[i] %= 2;
    }
    cout << "i=" << i << endl;
    for (int j=i; j>=0; j--)
    cout << a[j];
    cout << 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;
    using std::endl;

    int main()
    {
        int a=1963, b=828;
        int* pa = &a;
        int* pb = &b;
        int* ptemp = NULL;

        cout <<   a << "\t" <<   b << endl;
        cout << *pa << "\t" << *pb << endl;

        ptemp = pa; pa = pb; pb = ptemp;

        cout <<   a << "\t" <<   b << endl;
        cout << *pa << "\t" << *pb << endl;

        return 0;
    }
  10. Consider the following C++ program:
    #include <iostream>
    #include <iomanip>

    using std::cout;
    using std::cin;
    using std::endl;
    using std::setw;

    int main()
    {
    int N = 0;
    int i, j;
    do
    {
    cout << "N = ? ";
    cin >> N;
    for (i=1; i<=N; i++)
    {
    int w = (1) ;
    for (j=N-i; j>=0; j--)
    cout << "*";
    cout << setw(w);
    for (j=N-i; j>=0; j--)
    cout << "*";
    cout << endl;
    }

    for (i= (2) ; i>0; i--)
    {
    int w = (3) ;
    for (j=N-i; j>=0; j--)
    cout << "*";
    cout << setw(w);
    for (j=N-i; j>=0; j--)
    cout << "*";
    cout << endl;
    }
    } while (N > 0);
    return 0;
    }

    If we want to obatin the following results, what expressions should be filled into the three blanks, respectively?
    N = ? 5
    ***** *****
    **** ****
    *** ***
    ** **
    * *
    ** **
    *** ***
    **** ****
    ***** *****
    N = ? 6
    ****** ******
    ***** *****
    **** ****
    *** ***
    ** **
    * *
    ** **
    *** ***
    **** ****
    ***** *****
    ****** ******