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.
其實這題我種是卡在if判斷是中的運算元裡面,
&&是短路, 表示當前敘述不成立, 就表示後敘述仍然會執行,
但是這是一個AND的邏輯運算, 所以false && true => false.
所以這裡的第8行並不會執行.
在來第10行的b2 = true, 這裡放了一個 = 的指派運算子,
只要編譯正確就會給予預設的true値,
所以第10行, true && true => true
沒有留言:
張貼留言