Final Exam
Introduction to Computer Science
NCNU CSIE

Date: January 13th, 2009
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 << 3 << 3 << 3 << endl;
    cout << (3 << 3 << 3) << endl;
    cout << 3 << (3 << 3) << endl;
    cout << (3 << (3 << 3)) << endl;
    }

  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 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;
    }
  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 namespace std;

    int main( void )
    { int i=0, nums[10]={ 1, 8, 9, 5 };
    while( i<10 )
    {
    if( i%2 )
    {
    ++i;
    continue;
    }
    nums[i++]=i;
    }
    for (i=0;i<10;)
    cout << nums[i++] << " ";
    cout << endl;
    return 0;
    }
  4. Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // pointer to function
    #include <iostream>
    using std::cout;
    using std::endl;

    int sum(int a, int b)
    { return a+b;
    }

    int product(int a, int b)
    { return a*b;
    }

    int main()
    {
    int *pfun(int, int) = sum;
    cout << pfun(4, 8) << endl;
    pfun = product;
    cout << pfun(4, 8) << endl;
    }
  5. Show the output of the following program.

    #include <iostream>
    #include <windows.h>

    int main()
    {
    RECT aRect = { 0, 0, 100, 100 };
    RECT* pRect = &aRect;

    pRect.top += 10;

    std::cout << pRect.top;
    }
  6. What will be the output of the following program?
    // bin2dec.c
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    char b[] = "11100110000";
    int n=0, i=0;
    while (b[i])
    n = (n<<1) + b[i++] - '0';
    cout << n << endl;
    return 0;
    }
  7. What will be the output of the following 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;
    }
  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>
    #include <windows.h>

    struct ListElement
    {
    int value;
    ListElement* pNext;
    }

    int main()
    {
    ListElement L1 = { 1 };
    L1.pNext = NULL;
    std::cout << L1.value << std::endl;
    }
  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.

    // Pass-by-value
    #include <iostream>
    using std::cout;
    using std::endl;

    void test(char p[])
    { cout << p ;
    }

    int main()
    {
    char *name = "####";
    while (*name)
    test(name++);
    cout << endl;
    }



  10. Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // Pass-by-reference
    #include <iostream>
    using std::cout;
    using std::endl;

    void print(int i, int j)
    {
    cout << "i = " << i << "\t j = " << j << endl;
    }

    void swap1(int a, int b)
    { int temp;
    temp = a; a = b; b = temp;
    }
    void swap2(int &a, int &b)
    { int temp;
    temp = a; a = b; b = temp;
    }
    void swap3(int &a, int b)
    { int temp;
    temp = a; a = b; b = temp;
    }

    int main()
    {
    int i=2, j=1;
    swap3(i,j); print(i,j);
    swap2(i,j); print(i,j);
    swap1(i,j); print(i,j);
    }