Examly題庫立即開始練習
身心障礙人員考試身障四等-資訊處理類科計算機概要11430單選題

下列 Java 語言程式的執行結果為何? public class Exam { int x = 6, y = 13; public static void main (String[] args) { int sum = 0; for (int i = y; i >= x; i--) sum += i; System.out.println(sum); } }

A63
B70
C76
D編譯錯誤,無法執行正確答案
答案與詳解
D
正確答案
main 是 static 方法,不能直接存取非 static 的實例變數 x、y,導致編譯錯誤。

為什麼答案是 D

x、y 宣告為 instance variable(非 static),而 main 是 static 方法,無法直接存取實例成員,Java 編譯器會報 non-static variable cannot be referenced from a static context。

考點:加總陷阱考點:計算誘答考點:表面正解陷阱考點:static 存取規則
載入中…

計算機概要 相關題目

想練更多計算機概要考古題?

Examly 收錄 38 萬+ 道歷屆題目,每題都有像這樣的精選詳解。免費下載,立即開練。

Download on theApp Store即將推出Google Play
黑皮