民國115年 普通考試 電子工程 計算機概要
第31題
C++程式片段如下:class Student {private:int score;public:void setScore(int n) {if(n>=90…
題目
C++程式片段如下:class Student {private:int score;public:void setScore(int n) {if(n>=90)score=100;else if(n>=60 && n<90)score=n+5;elsescore=n+3;}int getScore () {return score;}void showScore() {cout << getScore() << endl;}};若執行以下敘述會得到什麼結果?Student stu;stu.setScore(80);stu.showScore(); A. 80 B. 83 C. 85 D. 100
官方答案
- 答案:C. 85
解析
追蹤 setScore(80) 的判斷流程:第一個條件 n>=90 不成立(80 未達 90),接著判斷 n>=60 && n<90,因 80 同時大於等於 60 且小於 90,條件成立,執行 score = n+5 = 80+5 = 85。只有輸入 90 以上才會被設為 100,輸入小於 60 才走 score = n+3 的分支。之後 showScore() 呼叫 getScore() 取出 score 並輸出,因此結果為 85。故選 C。
AI 參考解析(推得答案已與官方答案比對一致),非官方公告內容,僅供備考參考。
官方資料
答案以考選部公告之測驗式試題標準答案 PDF 為準。