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

Date: December 3rd, 2010
Time: 8:10-10:00
Open book; turn off computer & mobile phone

  1. What will be the output of the following program?
    #include <iostream>
    int main() // Understanding bitwise operators.
    {
    unsigned s = 2010;

    int i = (s >> 5) & ~(~0 << 4);
    std::cout << i << std::endl;
    }
  2. What will be the output of the following program?

    #include <iostream>

    using std::cout;
    using std::endl;

    int main()
    {
            int total, count = 6;
            total = --count + count++;
            cout << total << ' ' << count << endl;
            return 0;
    }


  3.  What will be the output of the following program, if the input string is "NCNU"?

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

    int main()
    {
            const int MAX(3);
            char buffer[MAX];
            cin.getline(buffer, MAX, '\n');
            cout << buffer << endl;
            return 0;
    }

  4. Consider the following BASIC program:
    10 INPUT A, B
    20 WHILE NOT (A=0 AND B=0)
    30 PRINT "MIN("; A; ","; B; ")=";
    40 PRINT __________
    50 INPUT A, B
    60 WEND
    70 END
    If we want to get the minimum of two input numbers, what expressions should be filled into the blank in line 40?

  5. Consider the following binary representation of a short integer (stored in two's complement).  Convert it to a decimal number.
    10000000 00000001
  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).

    // break vs. continue
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    { 
        int sum=0, i = 1;
        do {
            if (i++ == 5) continue;
            sum += i++;
        } while (i<10);
        cout << i << '\t' << sum << endl;
        return 0;
    }

  7. (10%) What will be the output of the following bwBASIC program?
    10 LET N = 5
    20 LET A$ = STRING$(N-1, " ") + STRING$(2*N-1, "*")
    30 FOR I=1 TO N
    40 PRINT MID$(A$, I, I+N-1)
    50 NEXT I
    60 END
  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).
    #include <iostream>

    int main() // break in a nested loop
    {
    for (int i = 1; i < 6; i++)
    for (int j = 1; j < 6; j++)
    {
    if (j==3)
    break;
    std::cout << i << 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.
    #include <iostream>

    using std::cout;
    using std::endl;

    int main()
    {
    int i, sum;
    for (i=10, sum=0; i>5; sum+= i--)
    cout << "i = " << i << endl; cout << "sum = " << sum << endl;
    }
  10. 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 main()

        char name[] = "tpgm";
        int i = 0;
        while (name[i] != '\0')    
            name[i++] = name[i++] ^ 0x20;
        cout << i << name << endl;
        return 0;
    }