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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2018.3.15
系所別:
年級:
學號:
姓名:
考試時間 08:10-08:25
  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).
    // True or False
    #include <iostream>

    int main()
    {
        int a = 0;
        int b = 10;
        if (b = a)
           std::cout << "True \n";
        else
           std::cout << "False \n";
        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).
    // Conditional Operator (P.103)
    #include <iostream>

    int main()
    {
        int i;
        for (i=1; i<=12; i++)
            std::cout << i << " - "
                 << (i<8?(i%2?31:30):(i%2?30:31))
                 << 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).
    // for-loop (P.110)
    #include <iostream>

    int main()
    {
        int i, sum;
        for (i=3, sum=0; i<=100; i+=2)
            sum += i;
        std::cout << i << '\t' << sum << std::endl;
        return 0;
    }