國立暨南國際大學 101 學年度第一學期小考試卷

科目名稱:計算機概論 開課系所:資訊工程 學系 考試日期 2012.11.2
系所別:
年級:
學號:
姓名:
考試時間 08:10-08:20
  1. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // binary to decimal
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char b[] = "01000100";
        int i;
        for (i=0; i<8; i++)
            b[i] -= '0';    // ASCII code of '0' is 48.
        for (i=0; i<=6; i++)
            b[i+1] = b[i]*2 + b[i+1];
        cout << b[7] << endl;
        return 0;
    }


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    // binary to decimal
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char b[] = "01000100";
        int i;
        for (i=0; i<8; i++)
            b[i] -= '0';    // ASCII code of '0' is 48.
        for (i=0; i<=6; i++)
            b[i+1] = b[i]*2 + b[i+1];
        cout << static_cast<short>(b[7]) << endl;
        return 0;
    }