Midterm Exam (1)
C++ Programming
NCNU CSIE

Date: February 22nd, 2013
Time: 08:10-10:00
Open book; turn off computer & mobile phone

  1. (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).

    // Bitwise Shift Operator (pp.86-88)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        signed int n = 1;
        int i = 2;
        for (i=0; i<31; i++)
            n <<= 1;
        for (i=0; i<31; i++)
            n >>= 1;
        cout << n << endl;
        return 0;
    }


  2. (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).
    // Arrays of Pointers to Char
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    char* pstr[] = { "Arkansas", "Oklahoma", "Kentucky" };
    cout << **pstr << endl;
    cout << *(pstr + 1) << endl;
    return 0;
    }

  3. (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).
    // Precedence (P.77)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    char* pstr[] = { "Arkansas", "Oklahoma", "Kentucky" };
    char** p = pstr; // array name as a pointer
    cout << **p++ << endl;
    cout << *(p + 1) << endl;
    return 0;
    }


  4. (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).
    // ASCII code of a character
    #include <iostream>

    void bar(short n)
    {
    for (short i=0; i<n; i++)
    printf("%c", 77); // 'A' == 65
    printf("\n");
    return;
    }

    int main()
    {
    short b[] = { 2, 0, 3, 2, 4, 3 };
    int i;
    for (i=0; i<= 4; i++)
    {
    printf("%2d ", b[i]);
    bar(b[i]);
    }
    return 0;
    }


  5. (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).
    // nested for-loop
    #include <iostream>
    using std::cout;
    using std::endl;

    int main ()
    {
    int space;
    int rows;
    int star;

    for (rows = 0; rows <9; rows ++)
    { for (space = 0; space < 8 - rows; space ++)
    cout << " ";
    for (star = 0; star < 8 + rows * 2; star ++)
    cout << "*";
    cout << endl;
    }
    return 0;
    }
  6. (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).
    // Member function of a class
    #include <iostream>
    using std::cout;
    using std::endl;

    class CCircle
    {
    public:
    int x;
    int y;
    int r;

    void MoveCircle(int x0, int y);
    };

    int main()
    {
    CCircle hut1, hut2;
    hut1.x = 10; hut1.y = 10; hut1.r = 10;
    hut2 = hut1;
    hut2.MoveCircle(50, 70);
    cout << hut2.y << endl;
    return 0;
    }

    // Do you need to pass by reference?
    void CCircle::MoveCircle(int x0, int y)
    {
    x = x0;
    this->y = y;
    }

  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).
    #include <iostream>
    using std::cout;

    void swap(int& i, int &j)
    {
    j ^= i; i ^= j; j ^= i; // eXclusive OR
    }

    int main()
    {
    int data[] = { 3, 1, 4, 1, 5, 9, 2, 6 };
    int i, j;
    for (i=0; i<6; i++)
    for (j=i+1; j<7; j++)
    if (data[i] > data[j])
    swap(data[i], data[j]);
    for (i=0; i<7; i++)
    cout << data[i];
    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).
    // Linked-list
    #include <iostream>
    using std::cout;

    struct Node
    {
    int value;
    Node* next;
    };

    int main()
    {
    int a[] = { 3, 1, 5, 4, 0, 6};
    Node* p;
    Node* current;
    Node* previous;
    Node* head = new Node;
    head->value = a[0]; head->next = NULL;
    int i, n;
    n = sizeof(a) / sizeof(a[0]);
    for (i=1; i < n; i++)
    {
    p = new Node;
    p->value = a[i];

    if (head->value < a[i])
    {
    p->next = head;
    head = p;
    continue;
    }

    current = previous = head;
    while (current != NULL && current->value > a[i])
    {
    previous = current;
    current = current->next;
    }
    if (current == NULL)
    {
    previous->next = p;
    p->next = NULL;
    }
    else
    {
    previous->next = p;
    p->next = current;
    }
    }

    p = head;
    while (p)
    {
    cout << p->value;
    p = p->next;
    }
    cout << '\n';
    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).
    // switch case
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::cin;

    int main()
    {
    int n = 21;
    int odd = 0;
    int even = 0;

    while (n > 0)
    {
    switch (n % 2)
    {
    case 0:
    even++;
    break;
    default:
    odd++;
    }
    n >>= 1;
    }
    cout << "even=" << even << endl;
    cout << "odd=" << odd << endl;
    return 0;
    }
  10. (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).
    // 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=21;

    swap2(i,j); print(i,j);
    swap3(i,j); print(i,j);
    }