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

Date: December 30th, 2009
Time: 14:10-16:00
Open book; turn off computer & mobile phone

  1. What will be the output of the following program?
    // Range of an unsigned short integer
    #include <iostream>
    using std::cout;

    int main()
    {
    unsigned short f=1;
    unsigned short i=1;
    for (i=1; i<14; i++)
    {
    f *= i;
    cout << f << "\n";
    }
    return 0;
    }
  2. Wha will be the output of the following program?

    // break from a loop
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
            int i;
            for (i=1; i<10; i++)
                    if (i==3) break;
                    cout << i << endl;
            return 0;
    }


  3. 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>   
    // Swap without a temporary variable
    int main()
    {
            char a = 'T';
            char b = 'W';

            printf("%c %c\n", a, b);

            a ^= b; printf("%x\n", a);
            b ^= a; printf("%x\n", b);
            a ^= b; printf("%x\n", a);

            printf("%c %c\n", a, b);

            return 0;
    }

  4. 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;

    int sum(int arg1 = 10, int arg2 = 20, int arg3 = 30, int arg4 = 40);

    int main()
    {
    cout << sum(5, 5) << "\n";
    return 0;
    }

    int sum(int arg1, int arg2, int arg3, int arg4)
    {
    return arg1 + arg2 + arg3 + arg4;
    }

  5. What will be the output of the following program?
    // Ex6_01a.cpp
    #include <iostream>
    using std::cout;
    using std::endl;

    long sum(long, long);
    long product(long, long);

    int main(void)
    {
    long (*pdo_it) (long, long);

    pdo_it = product;
    cout << pdo_it(3,5) << endl;
    pdo_it = sum;
    cout << pdo_it(3,5) << endl;
    return 0;
    }

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

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

  6. 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.

    // The conditional operator
    #include <iostream>
    using std::cout;

    int odd(int i)
    {
    int j = i % 2;
    return (i=1)?1:0;
    }

    int main(void)
    {
    for (int k=1; k<=2; k++)
    if (odd(k))
    cout << "Odd\n";
    else
    cout << "Even\n";
    return 0;
    }


  7. What will be the output of the following program?
    // Comparing two strings
    #include <iostream>
    using std::cout;

    int main()
    {
    char* p1="Q";
    char* p2 = "QUIT";

    bool flag = true;
    while (flag && *p1 != '\0')
    {
    if (*p1 != *p2)
    flag = false;
    p1++; p2++;
    }

    if (flag)
    cout << "The two strings are the same.\n";
    else
    cout << "The two strings are different.\n";
    }

  8. What will be the output of the following program?
    // List_program
    #include <iostream>
    using std::cout;

    int main()
    {
    const int MAX_LINE=10;
    const int STATEMENT_WIDTH=80;
    int numbers[MAX_LINE]={ 0 }; // Line number
    int next[MAX_LINE] = { 0 }; // Index of the next line
    char statements[MAX_LINE][STATEMENT_WIDTH];
    int nLines = 0; // how many lines are kept in memory

    strcpy(statements[nLines], "A=10");
    numbers[nLines++] = 10;
    strcpy(statements[nLines], "B=20");
    numbers[nLines++] = 20;
    strcpy(statements[nLines], "C=A+B");
    numbers[nLines++] = 30;

    // List_program

    for (int i=0; i<nLines; i++)
    {
    cout << numbers[i] << " ";
    cout << statements[i] << "\n";
    }

    cout << sizeof statements / sizeof statements[0] << "\n";
    return 0;
    }


  9. The following code is a little different from Ex6_09, so you can expect that its behavior is also different.Predict its output.
    // A program to implement a calculator
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    using std::cin;
    using std::cout;
    using std::endl;

    void eatspaces(char* str);
    int expr(char* str);
    int number(char* str, int& index);

    int main()
    {
    char buffer[] = "45 - 5 -4";
    eatspaces(buffer);
    cout << strlen(buffer) << endl;
    cout << buffer << " = " << expr(buffer) << endl;
    }

    // Function to eliminate spaces from a string
    void eatspaces(char* str)
    {
    int i = 0;
    int j = 0;

    while ((*(str + i) = *(str + j++)) != '\0')
    if (*(str+i) != ' ')
    i++;
    return;
    }

    // Function to evaluate an arithmetic expression
    int expr(char* str)
    {
    int value = 0;
    int index = 0;

    value = number(str, ++index);

    for (;;)
    {
    switch (*(str + index++))
    {
    case '\0':
    return value;
    case '+':
    value += number(str, ++index);
    break;
    case '-':
    value -= number(str, ++index);
    break;
    default:
    cout << endl
    << "Arrrgh!*#!! There's an error"
    << endl;
    exit(1);
    }
    }
    }

    // Function to recognize a number in a string
    int number(char* str, int& index)
    {
    int value = 0;
    while(isdigit(*(str + index)))
    value = 10*value + (*(str + index++) - '0');
    return value;
    }
  10. The following code is slightly different from the above one.  Predict its output.

    // A program to implement a calculator
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    using std::cin;
    using std::cout;
    using std::endl;

    void eatspaces(char* str);
    int expr(char* str);
    int number(char* str, int& index);

    int main()
    {
        char buffer[] = "45 - 5 -4";
        eatspaces(buffer);
        cout << strlen(buffer) << endl;
        cout << buffer << " = " << expr(buffer) << endl;
    }

    // Function to eliminate spaces from a string
    void eatspaces(char* str)
    {
        int i = 0;
        int j = 0;

        while ((*(str + i) = *(str + j++)) != '\0')
            if (*(str+i) != ' ')
                i++;
        return;
    }

    // Function to evaluate an arithmetic expression
    int expr(char* str)
    {
        int value = 0;
        int index = 0;

        value = number(str, index);

        for (;;)
        {
            switch (*(str + index++))
            {
                case '\0':
                    return value;
                case '+':
                    value += number(str, index);
                    break;
                case '-':
                    value -= number(str, index);
                    break;
                default:
                    cout << endl
                         << "Arrrgh!*#!! There's an error"
                         << endl;
                    exit(1);
            }
        }
    }

    // Function to recognize a number in a string
    int number(char* str, int& index)
    {
        int value = 0;
        while(isdigit(*(str + index)))
            value = 10*value + (*(str + index++) - '0');
        return value;
    }