1.課題
12/05付試験のQ23~Q38のついて考察せよ。
コードと実行結果を示し、各問について考察すること。

2.解答及び考察
(Q23) Q23.java
ソースコード
public class Q23 { public static void main(String[] args) { int x,y; x = 100; x +=1; x--; y = 200 + x; System.out.println(y++); } }

実行結果
300

考察

・x = 100;
  x +=1;  // x = x + 1 であるから x = 101
  x--;    // x = x - 1 であるから x = 100
  y = 200 + x;  // y = 200 + 100 で y = 300
  System.out.println(y++);  //後置きインクリメントなので、出力するときはまだインクリメントされない。よって300

(Q24) Q24.java
ソースコード
public class Q24 { public static void main(String[] args) { int a=0, x=0; a=5; a +=3; x = ++a; System.out.println(x); } }

実行結果
9

考察

・int a=0, x=0;
  a=5;
  a +=3;  // a = a + 3 であるから a = 8
  x = ++a;  // x = a + 1 であるから x = 9
  System.out.println(x);  // xの値(9)を出力する
TOPへ

(Q25)Q25.java
ソースコード
public class Q25 { public static void main(String[] args) { int a=9, b=3; a /= b; System.out.println(a %= b); } }

実行結果
0

考察

・int a=9, b=3;
  a /= b;  // a = a / b よって a = 3
  System.out.println(a %= b);  // a = a % b = 3 % 3の値(0)を出力

(Q26) Q26.java
ソースコード
public class Q26 { public static void main(String[] args) { int i, j; for(i=0, j=0; i<3; i++) ++j; System.out.println(i * j); } }

実行結果
9

考察

・for(i=0, j=0; i<3; i++) ++j;  // i<3の時iとjをインクリメントするからループを抜けるときはiもjも3
  System.out.println(i * j);  // i * j の値(9)を出力
  
・このループ内での処理の順番は、++j , i++である。
TOPへ

(Q27) Q27.java
ソースコード
public class Q27 { public static void main(String[] args) { int i=2; while(i-- > 0) System.out.print(i); } }

実行結果
10

考察

・int i=2;
  while(i-- > 0) System.out.print(i);
  
  一回目のループでは i = 2 (>0) その後デクリメントされ i = 1 となり 1 が出力。但し改行されない。
  二回目のループでは i = 1 (>0) その後デクリメントされ i = 0 となり 0 が出力。
  次のループで i = 0 であるからループから抜ける。
  したがって実行すると10(10ではなく1と0)となる

(Q28) Q28.java
ソースコード
public class Q28 { public static void main(String[] args) { int num=10000; for(int i = 0; i < 4; i++) num >>= i; System.out.println(num); } }

実行結果
156

考察

・int num=10000;
  for(int i = 0; i < 4; i++) num >>= i;
  System.out.println(num);
  
  一回目のループでは、i = 0 (<4) であるから 10000 >>= 0 。num = 10000
  二回目のループでは、i = 1 (<4) であるから 10000 >>= 1 。num =  5000
  三回目のループでは、i = 2 (<4) であるから  5000 >>= 2 。num =  1250
  四回目のループでは、i = 3 (<4) であるから  1250 >>= 3 。num =   156
  次は i = 4 となるからループから抜ける。
  よって実行すると156が表示される。
  
・このループ内での処理の順番は num >>= i , i++ である。 
TOPへ

(Q29) Q29.java
ソースコード
public class Q29 { public static void main(String[] args) { int num =0; for(int i = 1; i <= 10; i++){ if(++num % i==0) num++; } System.out.println(++num); } }

実行結果
12

考察

for文中での動作を表で表すと次のようになる
i++numで変化したnumの値num++で変化したnumの値(条件式が偽になれば影響を受けない)
112
233
344
455
566
677
788
899
91010
101111
出力されるときにインクリメントされるからnum = 12 となる。

(Q30) Q30.java
ソースコード
public class Q30 { public static void main(String[] args) { int a = 9; if(a++ != 10 | a++ == 10) a++; System.out.println(a); } }

実行結果
12

考察

・if(a++ != 10 | a++ == 10) a++;
  後置きインクリメントであるから
  9 != 10 → インクリメント(a=10)→ 10 == 10 → インクリメント(a=11)
  となる。これは真であるから、さらにインクリメント(a=12)。
  よって出力時のaの値は12
TOPへ

(Q31) Q31.java
ソースコード
public class Q31 { public static void main(String[] args) { for( int i = 0; i< 5; i ++) System.out.println("i=="+ i ); System.out.println("Hello"); } }

実行結果
i==0
i==1
i==2
i==3
i==4
Hello

考察

・{}を使わないfor文、while文などではセミコロンまでが影響を受ける
  したがってSystem.out.println("Hello");はfor文の影響を受けていない。
  よってHelloが出力されるのは一回だけ。

(Q32) Q32.java
ソースコード
public class Q32 { public static void main(String[] args) { int i; for( i= 0; i < 9; i +=3){} System.out.println(i); } }

実行結果
9

考察

・i = 9 となりループから抜けるので答えは9となる。
TOPへ

(Q33) Q33.java
ソースコード
public class Q33 { public static void main(String[] args) { for( int i = 0; i < 8; i ++){ System.out.println(i); i += 3; } } }

実行結果
0
4

考察

・for( int i = 0; i < 8; i ++){
			System.out.println(i);
			i += 3;
		}
  このfor文の動作は次のようになる
  i = 0 (<8)→ System.out.println(i);→ i += 3 (i=3)→ i++ (i=4)→ i = 4 (<8)→ System.out.println(i);
  → i += 3 (i=7)→ i++ (i=8)→ ループから抜ける

(Q34) Q34.java
ソースコード
public class Q34 { public static void main(String[] args) { int i = 0; for( sayHello(); i <= 6; i += 3){ sayHello(); } } static void sayHello(){ System.out.println("Hello"); } }

実行結果
Hello
Hello
Hello
Hello

考察

・ まずfor( sayHello(); i <= 6; i += 3)のsayHello()で一回表示
   for文は三回繰り返されるので三回表示したがって、4回表示される。
TOPへ

(Q35) Q35.java
ソースコード
class Q35 { public static void main(String[] args) { 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); } } class Player{ int id = 0; static int num = 0; }

実行結果
3000

考察

・numはstatic変数なので値を共有。
  p1.id = 1000;
  p2.id = 2000;
  p1.num += p1.id;  //p1.num = 1000
  p2.num += p2.id;  //p2.num = 1000 + 2000 = 3000
  よってPlayer.numは3000

(Q36) Q36.java
ソースコード
public class Q36 { public static void main(String args[]) { 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); } } class Player{ int id = 0; static int num = 0; }

実行結果
p1.num == 3000
p2.num == 3000

考察

・numはstatic変数なので値を共有。
  p1.id = 1000;
  p2.id = 2000;
  Player.num += p1.id;  //Player.num = 1000
  Player.num += p2.id;  //Player.num = 1000 + 2000 = 3000
  よってPlayer.numは3000であるから、p1.numもp2.numも3000となる。
TOPへ

(Q37) Q37.java
ソースコード
public class Q37 { public static void main(String argv[]){ 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); } } class Card{ int deposit; }

実行結果
8000

考察

・c1.deposit = 1000;
  c2.deposit = 2000;
  c3.deposit = 3000;  //c3 = c1であるからこの時点でc1.deposit = 3000
  int sum = c1.deposit + c2.deposit + c3.deposit; // int sum = 3000 + 2000 + 3000 = 8000
  したがってsumの値は8000

(Q38) Q38.java
ソースコード
public class Q38 { public static void main(String argv[]) { Card c1 = new Card(); Card c2 = c1; c1.deposit = 1000; c2.deposit = 2000; Bank.useCard(c1); Bank.useCard(c2); System.out.println(c1.deposit); System.out.println(c2.deposit); } } class Card{ int deposit; } class Bank{ public static void useCard(Card c){ c.deposit -= 500; } }

実行結果
1000
1000

考察

・c1.deposit = 1000;
  c2.deposit = 2000;  //c2 = c1であるから、この時点でc1.deposit = 2000
  Bank.useCard(c1);   //c1.deposit = 2000 - 500 = 1500  この時点でc2.depositも1500となる。
  Bank.useCard(c2);   //c2.deposit = 1500 - 500 = 1000  この時点でc1.depositも1000となる。
  したがってc1.deposit = c2.deposit = 1000
  すなわちc1.depositとc2.depositは等しい
TOPへ

3.感想
今回の課題でいろいろなことを学べた。次のテストにもこの知識を生かしていきたい。


戻る