2009年2月11日 星期三

第111題

Given:
15. public class Pass2{
16. public void static main(String[] args){
17. int x = 6;
18. Pass2 p = new Pass2();
19. p.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

答案:C
參考:4-1 條件控制、8-2 方法種類與呼叫方式
-----------------------------------------------------------
17. int x = 6;
19. p.doStuff(x); //傳值6
23. void doStuff(int x){ //接到6
24. System.out.print(" doStuff x = " + x++); //先列印doStuff x = 6後x再+1
25. }//結束}後回到原呼叫式的後面20. System.out.print(" main x = " + x); //main x = 6

沒有留言:

張貼留言