Midterm Exam (1)
C++ Programming
NCNU CSIE

Date: March 4th, 2009
Time: 08:30-10:30
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;
    break;
    }
    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. 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 sum(int x1=1, int x2=2, int x3=3, int x4=4);
    // Supply default values of parameters
    int main(void)
    {
    cout << sum(300, 15) << endl;
    }
    int sum(int x1, int x2, int x3, int x4)
    {
    return x1 + x2 + x3 + x4;
    }
  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>
    #define N 10
    using std::cout;

    void dout(int n)
    {
    if(n<0)
    cout << "(" << n << ")";
    else
    cout << n;
    }

    int main()
    {
    int M[N], D[N], Q[N], R[N], A[N], B[N];
    int i = 0;
    M[0] = 1610; D[0] = 590;
    do
    {
    Q[i] = M[i] / D[i];
    R[i] = M[i] - Q[i]*D[i];
    M[i+1] = D[i];
    D[i+1] = R[i];
    } while (R[i++] > 0);

    i-=2; A[i] = 1; B[i] = -Q[i];
    cout << R[i];
    while (1)
    {
    cout << "\t=\t"; dout(A[i]);
    cout << "*"; dout(M[i]);
    cout << "+"; dout(B[i]);
    cout << "*"; dout(D[i]);
    cout << "\n";

    if (i==0) break;
    A[i-1] = B[i];
    B[i-1] = A[i] - B[i]*Q[i-1];
    i--;
    }
    }
  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).
    #include <iostream>
    using std::cout;

    void eatspaces(char* str)
    {
    int i = 0, j = 0;
    while ((*(str+i) = *(str + j++)) != '\0')
    if (*(str + i) != ' ')
    i++;
    return;
    }

    int main()
    {
    int i = 0;
    char msg[] = "National Chi Nan University";
    eatspaces(msg);
    for (i=0; msg[i] != '\0'; i++);
    cout << i << '\n';
    }