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

Date: December 7th 2012
Time: 09:00-11:00
Open book; turn off computer & mobile phone

  1. (10%) What will be the output of the following program?

    // printf()
    #include <stdio.h>  // include this for printf()

    int main()
    {
        int x = 0x127f;
        float y = 12.7f;   
        printf("%+07d\n%07.2f\n", x, -y);   
        return 0;
    }


  2. (10%) What will be the output of the following program?

    // strcpy()
    #include <iostream>
    #include <cstring>

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

    int main()
    {
        char str1[] = "Hello- ";
        char str2[ sizeof(str1) ];
        // Can we replace "sizeof" with "strlen"?
        for (int i=0; i<4; i++)
        {
            str1[6] = '0' + i;
            strcpy(str2, str1);
            cout << str2 << endl;
        }
        return 0;
    }


  3. (10%) What will be the output of the following program?

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

    int main()
    {
        int i, sum = 0;
        for (i=1; i<10; i++)
        {
            if (i % 2) continue;    // P.146
            sum += i;
        }
        cout << sum << endl;
        return 0;
    }





  4. (10%) What will be the output of the following program?

    // Recursive function
    #include <iostream>

    void f(int n)
    {
        if (n >= 8)
            f(n/8);
        std::cout << n % 8;
        return;
    }

    int main()
    {
        f(4735);
        std::cout << std::endl;
        return 0;
    }



  5. (10%) What will be the output of the following program?

    // fopen()
    #include <stdio.h>

    int main()
    {
        FILE* fd1;
        FILE* fd2;  
        int n;
      
        for (int i=0; i<10; i++)
            if (i % 2)
            {
                fd1 = fopen("odd.txt", "w");
                fprintf(fd1, "%d\n", i);
                fclose(fd1);
            }
            else
            {
                fd2 = fopen("even.txt", "w");
                fprintf(fd2, "%d\n", i);
                fclose(fd2);
            }
      
        fd1 = fopen("odd.txt", "r");
        while ( fscanf(fd1, "%d", &n) != EOF )
            printf("%d\n", n);
        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).

    // Pointer to int
    #include <iostream>

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

    int main()
    {
        int number1 = 12;
        int number2 = 04;
        int* pnumber = &number2;
        cout << (*pnumber*=2) << endl;   // Is this a valid expression?
        cout << number1 << number2 << 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).

    // Size of a char array
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char str[][18] = { "George Washington",
                        "John Adams",
                        "Thomas Jefferson",
                        "James Madison",
                        "James Monroe",
                        "John Quincy Adams"
                       };
        cout << sizeof str[2] << endl;    
        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).
    // Pointer arithmetic
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
    int data[2][4] = { {1, 3, 5, 7}, {2, 4, 6, 8} };
    int* pnum = &data[0][2];
    cout << *(pnum + 2) - *(pnum - 1) << 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).
    // Math function
    #include <iostream>
    #include <cmath>

    int rnd(float x)
    { return static_cast<int>( floor(x + 0.5) ); }

    int main()
    {
    std::cout << rnd(4.5) << std::endl;
    return 0;
    }
  10. (10%) What will be the output of the following program?
    // Pass-by-reference
    #include <stdio.h>

    int strlen(char* &s)
    {
    char* p = s;
    while (*p != '\0')
    ++p;
    return p - s;
    }

    int main()
    {
    char* str = "Apple";
    int n = strlen(str);
    printf("The length of \"%s\" is %d.\n",
    str, n);
    return 0;
    }