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

科目名稱:程式設計 開課系所:資訊工程 學系 考試日期 2014.3.11
系所別:
年級:
學號:
姓名:
考試時間 14:10-14:20
  1. (10%) What would the following code show on the screen?
    // Ncurses Library
    #include <curses.h>

    int main()
    {
        const int HEIGHT = 3;
        int y;

        initscr();
        for (y=0; y<=HEIGHT; y++)
        {
            move(y, y); addch('*');
            move(y, 2*HEIGHT - y); addch('*');
        }
        refresh();
        endwin();
        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).
    // switch (P.135)
    #include <iostream>

    int main()
    {
        int i(0), a(0), b(0);
        for (i=1; i<=6; i++)
            switch(i % 2)
            {
                case 0:
                    a += 2;
                    break;
                case 1:
                    b++;
                    break;
            }

        std::cout << a << b << 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).
    // continue (P.146) vs. break (P.136)
    #include <iostream>
    using std::cout;
    using std::endl;

    int main()
    {
        int i, j, k, m;
        m=0;
        for (i=1; i<6; i++)
        {
            if (i==3)
                continue;
            cout << i << endl;
        }
        cout << "After the loop, i = " << i << endl;
        return 0;
    }