01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class exam1{ public static void main(String args[]){ System.out.print("Q23's Ans ----> "); q23(); System.out.print("Q24's Ans ----> "); q24(); System.out.print("Q25's Ans ----> "); q25(); System.out.print("Q26's Ans ----> "); q26(); System.out.print("Q27's Ans ----> "); q27(); System.out.println(); System.out.print("Q28's Ans ----> "); q28(); System.out.print("Q29's Ans ----> "); q29(); System.out.print("Q30's Ans ----> "); q30(); System.out.println(); System.out.print("Q31's Ans ----> "); q31(); System.out.println("---------------"); System.out.println(); System.out.print("Q32's Ans ----> "); q32(); System.out.println(); System.out.print("Q33's Ans ----> "); q33(); System.out.println("---------------"); System.out.println(); System.out.print("Q34's Ans ----> "); q34(); System.out.println("---------------"); System.out.println(); System.out.print("Q35's Ans ----> "); q35(); System.out.println(); System.out.print("Q36's Ans ----> "); q36(); System.out.println("---------------"); System.out.println(); System.out.print("Q37's Ans ----> "); q37(); System.out.println(); System.out.print("Q38's Ans ----> "); q38(); } /* */ /* (A) q23 ~ q27 スレッドが入るが、ここでは省略 */ /* */ }
% javac exam1.java % java exam1 Q23's Ans ----> 300 Q24's Ans ----> 9 Q25's Ans ----> 0 Q26's Ans ----> 9 Q27's Ans ----> 10 Q28's Ans ----> 156 Q29's Ans ----> 12 Q30's Ans ----> 12 Q31's Ans ----> i == 0 i == 1 i == 2 i == 3 i == 4 Hello --------------- Q32's Ans ----> 9 Q33's Ans ----> 0 4 --------------- Q34's Ans ----> Hello Hello Hello Hello --------------- Q35's Ans ----> 3000 Q36's Ans ----> p1.num == 3000 p2.num == 3000 --------------- Q37's Ans ----> 8000 Q38's Ans ----> c1.deposit == 1000 c2.deposit == 1000
01
02
03
04
05
public static void q23(){ int x,y; x = 100; x +=1; x--; y = 200 + x; System.out.println(y++); }
01
02
03
04
05
public static void q24(){ int a = 0, x = 0 ; a = 5; a +=3; x = ++a; System.out.println(x); }
01
02
03
04
05
public static void q25(){ int a = 9, b = 3; a /= b; System.out.println(a %= b); }
01
02
03
04
05
public static void q26(){ int i, j; for(i=0, j=0; i < 3; i++) ++j; System.out.println(i * j); }
01
02
03
04
public static void q27(){ int i=2; while (i-- > 0) System.out.print(i); }
01
02
03
04
05
public static void q28(){ int num = 1000; for (int i = 0; i < 4; i++) num >>= i; System.out.println(num); }
01
02
03
04
05
06
07
public static void q29(){ int num = 0; for (int i = 1; i <= 10; i++) { if (++num % i ==0) num++; } System.out.println(++num); }
01
02
03
04
05
public static void q30(){ int a = 9; if (a++ != 10 | a++ == 10) a++; System.out.println(a); }
01
02
03
04
05
public static void q31(){ for (int i = 0; i < 5; i++) System.out.println("i == " + i); System.out.println("Hello"); }
01
02
03
04
05
public static void q32(){ int i; for ( i = 0; i < 9; i += 3) {} System.out.println(i); }
01
02
03
04
05
public static void q33(){ for (int i = 0; i < 8; i++) { System.out.println(i); i += 3; }
01
02
03
04
05
06
07
08
09
10
public static void q34(){ int i = 0; for(sayHello(); i <= 6; i += 3) { sayHello(); } } static void sayHello() { System.out.println("Hello"); }
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
public static void q35(){ Player p1 = new Player(); Player p2 = new Player(); p1.id = 1000; p2.id = 2000; p1.num += p1.id; p2.num += p2.id; System.out.println(Player.num); } /* Player ************** class Player{ int id = 0; static int num = 0; } ************************/
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
public static void q36(){ Player p1 = new Player(); Player p2 = new Player(); p1.id = 1000; p2.id = 2000; Player.num += p1.id; Player.num += p2.id; System.out.println("p1.num == "+p1.num); System.out.println("p2.num == "+p2.num); } /* Player ************** class Player{ int id = 0; static int num = 0; } ************************/
q35と同じく、staticで共有されているので、p1.num = p2.num であり、共に3000が出力される。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
public static void q37(){ Card c1 = new Card(); Card c2 = new Card(); Card c3 = c1; c1.deposit = 1000; c2.deposit = 2000; c3.deposit = 3000; int sum = c1.deposit + c2.deposit + c3.deposit; System.out.println(sum); } /* Card *************** class Card{ int deposit; } **********************/
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void q38(){ Card c1 = new Card(); Card c2 = c1; c1.deposit = 1000; c2.deposit = 2000; Bank.useCard(c1); Bank.useCard(c2); /* A */ System.out.println("c1.deposit == " + c1.deposit); System.out.println("c2.deposit == " + c2.deposit); } /* Card **************************************************************** class Card{ int deposit; } ** Bank **************************************************************** class Bank{ public static void useCard( Card c ) {c.deposit -= 500; } } ***********************************************************************/
今回は考察する量が多く、すごく疲れました。
参考文献