國立暨南國際大學 106 學年度第二學期小考試卷

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2018.4.11
系所別:
年級:
學號:
姓名:
考試時間 10:45-11:00
  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).
    // Pointer to Character
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        char word[] = "Tsai Ing-wen";
        char* p = word;
        std::cout << word[5] << std::endl;
        std::cout << p+5 << std::endl;
        std::cout << *(p+5) << std::endl;
        return 0;
    }

  2. (10%) Assume this is a 64-bit computer. 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).
    // Sizeof Operator
    #include <iostream>

    int main(){
        const int MAX_LEN = 80;
        char msg[MAX_LEN] = "ALICE";
        char* p = msg;
        char msg2[] = "BOB";
        std::cout << sizeof(msg) << '\t' << sizeof(msg2) << std::endl;
        std::cout << sizeof(msg[0]) << '\t' << sizeof(p) << std::endl;
        return 0;
    }

  3. (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).
    // Pointer Operation
    #include <iostream>

    int main(){
       int n[] = {2, 0, 1, 8};
       int* p = &n[0];
       int* p1 = p;

       std::cout << *p << (*p)++ << std::endl;
       std::cout << *(++p);
       std::cout << *(p++) << std::endl;
       std::cout << ++(*p) << *(p1++) << *p << std::endl;
       return 0;
    }