2009年2月3日 星期二

第3題SampleCode

Given:
1. public class Test{
2. public static void main(String[] args){
3. int x = 5;
4. boolean b1 = true;
5. boolean b2 = false;
6.
7. if((x==4) && !b2)
8. System.out.print("1 ");
9. System.out.print("2 ");
10. if((b2=true) && b1)
11. System.out.print("3 ");
12. }
13. }

What is the result?
A. 2
B. 3
C. 1 2
D. 2 3
E. 1 2 3
F. Compilation fails.
G. An exception is thrown at runtime.
答案:D
參考:4-1 條件控制

--------------------------------------------
public class SCJP6003 {
public static void main(String[] args) {
int x= 5;
boolean b1 = true;
boolean b2 = false;

if((x==4) && !b2)
System.out.print("1 ");
System.out.print("2 ");
if((b2=true) && b1)
System.out.print("3 ");
}
}

ANS:
D. 2 3

--------------------------------------------------------------------------------------
My Answers:
7. if((x==4) && !b2)
8. System.out.print("1 ");
will not run coz, x not equals 4
--------------------------------------------------------------------------------------

1 則留言:

  1. SCJP範例的執行結果雖然是D,可是正常的話應該是F?還是我程式的觀念有錯誤...

    回覆刪除