Midterm Exam (1)
C++ Programming
NCNU CSIE

Date: February 23rd, 2011
Time: 14:10-16: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).

    #include <iostream>
    using std::cout;
    using std::endl;

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

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


  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).
    // 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=20, j=11;
    swap1(i,j); print(i,j);
    swap2(i,j); print(i,j);
    swap3(i,j); print(i,j);
    }
  3. (10%) What will be the output of the following program?
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Node
    {
    char value;
    Node* left;
    Node* right;
    };

    void print(Node* r)
    {
    if (r->left != NULL) print(r->left);
    cout << r->value;
    if (r->right != NULL) print(r->right);
    return;
    }

    int main(void)
    {
    Node a = { 'A', 0, 0 };
    Node b = { 'B', 0, 0 };
    Node h = { 'H', &a, &b };
    Node c = { 'C', 0, 0 };
    Node d = { 'D', 0, 0 };
    Node g = { 'G', &c, &d };
    Node i = { 'I', &h, &g };
    Node f = { 'F', 0, 0 };
    Node e = { 'E', &i, &f };
    print(&e);
    cout << 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).
    #include <iostream>
    using std::cout;
    using std::endl;

    struct Node
    {
    char value;
    Node* left;
    Node* right;
    };

    void print(Node* r)
    {
    if (r->left != NULL) print(r->left);
    if (r->right != NULL) print(r->right);
    cout << r->value;
    return;
    }

    int main(void)
    {
    Node a = { 'A', 0, 0 };
    Node b = { 'B', 0, 0 };
    Node h = { 'H', &a, &b };
    Node c = { 'C', 0, 0 };
    Node d = { 'D', 0, 0 };
    Node g = { 'G', &c, &d };
    Node i = { 'I', &h, &g };
    Node f = { 'F', 0, 0 };
    Node e = { 'E', &i, &f };
    print(&i);
    cout << endl;
    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).
    // Importance of the "break" statement in the "switch" statement
    #include <iostream>
    using std::cout;
    using std::endl;

    int main(void)
    {
    char str[] = "TAIWAN";
    char *p = str;
    int vowel = 0;
    int consonant = 0;
    for ( ; *p != '\0'; p++)
    switch (*p)
    {
    case 'A':
    case 'E':
    case 'I':
    case 'O':
    case 'U':
    vowel++;
    default:
    consonant++;
    }
    cout << vowel << '\t' << consonant << 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).

    // Matrix Multiplication
    #include <iostream>
    using std::cout;
    using std::endl;

    int main(void)
    {
        int A[][5] = {    { 0, 0, 0, 0, 1},
                        { 0, 0, 0, 1, 0 },
                        { 0, 0, 1, 0, 0 },
                        { 0, 1, 0, 0, 0 },
                        { 1, 0, 0, 0, 0 } };
        int B[5][5] = { 0 };
        int i, j, k;
        for (i=0; i<5; i++)
            for (j=0; j<5; j++)
                for (k=0; k<5; k++)
                    B[i][j] += A[i][k] * A[k][j];
        for (i=0; i<5; i++)
        {
            for (j=0; j<5; j++)
                cout << B[i][j];
            cout << endl;
        }
      return 0;
    }



  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;
    using std::endl;

    int main()
    {
    char str[10] = "February";
    char* p = str;
    cout << str << endl;
    cout << *(++p) << endl;
    for ( ; *p != '\0'; p++)
    *p ^= 0x20;
    cout << sizeof(*p) << endl;
    cout << str << endl;
    }


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

    int main(void)
    {
    int c1 = 'A', c2 = 'a';
    c1 ^= c2; cout << hex << c1 << c2 << endl;
    c2 ^= c1; cout << hex << c1 << c2 << endl;
    c1 ^= c2; cout << hex << c1 << c2 << endl;
    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).
    // Linked-List
    #include <iostream>
    using std::cout;
    using std::endl;

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

    Node* insert(Node* head, int n)
    {
    Node* p = new Node;
    p->value = n;
    p->next = head;
    return p;
    }

    void print_list(Node* p)
    {
    int n = 0;
    while (p)
    {
    cout << "Line " << ++n << ":" << p->value << endl;
    p = p->next;
    }
    return;
    }

    int main(void)
    {
    int A[] = { 1, 3, 5, 7, 2, 4, 6 };
    int n = sizeof(A) / sizeof(A[0]);
    Node* head = NULL;
    for (int i=0; i<n; i++)
    head = insert(head, A[i]);
    print_list(head);
    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).

    // Structure array
    #include <iostream>
    using std::cout;

    struct POINT
    {
        int x;
        int y;
    }

    int Area(POINT p1, POINT p2)
    { return (p2.x - p1.x) * (p2.y - p1.y);
    }

    int main()
    {
        POINT p[2] = { { 10, 40 } , { 50, 60 } };   
        cout << Area(p[0], p[1]) << "\n";
        return 0;
    }


  11. (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;
    using std::endl;

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

    Node* insert(Node* head, int n)
    {
        Node* p = new Node;
        p->value = n; p->next = NULL;
        Node* p2 = head;
        Node* p1 = p2;
       
        if (p2 == NULL)
            return p;

        while (p2->value < p->value && p2->next != NULL)
        {
            p1 = p2;       
            p2 = p2->next;
        }

        if (p2->value < p->value)
        {
            p->next = p2->next;
            p2->next = p;
            return head;
        }
        else   
            if (p1==p2)        // n is less than the first element
            {
                p->next = p1;
                return p;
            }
            else
            {
                p->next = p2;
                p1->next = p;
                return head;
            }   
    }

    void print_list(Node* p)
    {
        int n = 0;
        while (p)
        {
            cout << p->value << endl;
            p = p->next;
        }
        return;
    }

    int main(void)
    {
        int A[] = { 1, 3, 5, 7, 2, 4, 6 };
        int n = sizeof(A) / sizeof(A[0]);
        Node* head = NULL;
        for (int i=0; i<n; i++)
            head = insert(head, A[i]);
        print_list(head);
        return 0;
    }