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

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

  1. Check whether the following program has any syntax 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 i, j;
    for (i=0; i<6; i++)
    {
    for (j=0; j<6; j++);
    cout << "A";
    cout << endl;
    }
    return 0;
    }

  2. Check whether the following program has any syntax mistake. If yes, point out the mistakes and correct them; if no, predict the output of the
    program.

    #include <iostream>

    using namespace std;

    int main( void )
    { int *ptrA, *ptrB, tmp, A=5, B=6;

    ptrA = &A;
    ptrB = &B;

    tmp = ptrA;
    ptrA = ptrB;
    ptrB = tmp;

    cout << "A = " << *ptrA << ", B = " << *ptrB << endl;

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

    int i = (s >> 3) ^ ~(~0 << 2);
    std::cout << i << std::endl;
    }
  4. What will be the output of the following program?
    #include <iostream>
    using namespace std;

    int main( void )
    { int i=0, nums[10]={ 0 };
    while( i<10 )
    {
    if( i==5 )
    {
    ++i;
    continue;
    }
    nums[i++]=i;
    }
    for (i=0;i<10;)
    cout << nums[i++] << " ";
    cout << endl;
    return 0;
    }
  5. Please correct the following code so that the output will be
    1 1
    9 3
    6 6
    3 9
    // bubble_sort.c
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    int i, j, *temp;
    int a[] = { 1, 9, 6, 3}; // data
    const int K = sizeof(a) / sizeof(a[0]);
    int *b[K]; // pdata

    // Initialize pdata
    for(i=0;i<K;i++)
    b[i] = &a[i];

    // Sort pdata by value which is pointed by pdata
    for(i=0;i<K;i++)
    for(j=K-1;j>i;j--)
    if (b[j-1] > b[j])
    {
    temp = *(b[j-1]);
    *(b[j-1]) = *(b[j]);
    *(b[j]) = temp;
    }

    // print values of data array & values pointed by pdata
    for(i=0;i<K;i++)
    cout << a[i] << " " << *(b[i]) << 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[] = "1100111100";
    int n=0, i=0;
    while (b[i])
    n = (n<<1) + b[i++] - 48;
    cout << n << endl;
    return 0;
    }
  7. Check whether the following program has any syntax mistake. If yes, point out the mistakes and correct them; if no, predict the output of the
    program.
    #include <iostream>

    int main()
    {
    for (int i = 6; i < 10; i--)
    {
    if (i==3)
    break;
    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.
    #include <iostream>
    using std::cout; using std::endl;

    int main()
    {
    static int MAX = 5;
    int i,j,k;
    for(k=0; k<MAX; k++) ;
    for(i=1; i<=MAX; i++)
    {
    for(j=MAX; j>0; j--)
    if(i==j)
    cout << "O";
    else
    cout << "X";
    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::endl;
    using std::cout;

    int main()
    {
        char a[]="Hello, world!";
        const int k = sizeof(a);
        int sum = 0;
        for(int i=0;i<k;i++)
            sum += a[i];
        cout << "k= " << k << endl;
        cout << "sum= " << sum << endl;

        return 0;
    }

  10. Consider the following C++ program:
    #include <iostream>
    using std::cout; using std::cin; using std::endl;

    int main()
    {
    int i,j, N;
    cout << "N = ? ";
    cin >> N;
    for(i=1; i<=N; i++)
    {
    for(j=1; j<=N; j++)
    if(i== (1) || i== (2) )
    cout << "O";
    else
    cout << "X";
    cout << endl;
    }
    return 0;
    }



    If we want to obtain the following results, what expressions should be filled into the three blanks, respectively?
    N = ? 5
    OXXXO
    XOXOX
    XXOXX
    XOXOX
    OXXXO
    N = ? 6
    OXXXXO
    XOXXOX
    XXOOXX
    XXOOXX
    XOXXOX
    OXXXXO