SCJP 1.6版考題 111

出自 陳富國維基館
前往: 導覽搜尋
Given:
15. public class Pass2{
16.  public void static main(String[] args){
17.     int x = 6;
18.     Pass2 p = new Pass2();
19.     .doStuff(x);
20.     System.out.print(" main x = " + x);
21.  }
22.
23.   void doStuff(int x){
24.     System.out.print(" doStuff x = " + x++);
25.  } 
26. }


And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
  A. Compilation fails.
  B. An exception is thrown at runtime.
  C. doStuff x = 6 main x = 6
  D. doStuff x = 6 main x = 7
  E. doStuff x = 7 main x = 6
  F. doStuff x = 7 main x = 7

解答


Ans: C

解說:

此題是考call by value的觀念
19行將x=6傳進doStuff方法,6被複製給doStuff.x,印出後,doStuff.x++後結束doStuff方法
回到main方法之後,x還是為6…