12/05付試験のQ23〜Q38の問題について考察せよ。コードと実行結果を示し、各問について考察すること。 |
class test23{ public static void main(String args[]){ int x,y; x=100; x+=1; x--; y=200+x; System.out.println(y++);} } |
[nw0523:~] javac test23.java [nw0523:~] java test23 300 |
xは100+1-1だから100となり、yは200+100だから300となる。 そして、y++は表示された後に行われるので、表示は300。 |
class test24{ public static void main(String args[]){ int a=0, x=0; a = 5; a +=3; x=++a; System.out.println(x);} } |
[nw0523:~] javac test24.java [nw0523:~] java test24 9 |
a = 5+3 = 8でx = a+1 = 8+1 = 9である。 よって表示されるのは 9 |
class test25{ public static void main(String args[]){ int a=9, b=3; a/=b; System.out.println(a%=b); } } |
[nw0523:~] javac test25.java [nw0523:~] java test25 0 |
a = a/b = 9/3 = 3でa%b = 3%3 = 0となる。 よって表示されるのは 0 |
class test26{ public static void main(String args[]){ int i, j; for(i=0,j=0;i<3; i++) ++j; System.out.println(i*j); } } |
[nw0523:~] javac test26.java [nw0523:~] java test26 9 |
for(i=0,j=0; i<3; i++)++j;はi<3ならj=j+1をしてからi=i+1 だから、for文が終わった時点でi=3、j=3なので、i*j=9よって 9が表示される。 |
class test27{ public static void main(String args[]){ int i=2; while(i-->0) System.out.println(i); } } |
[nw0523:~] javac test27.java [nw0523:~] java test27 1 0 |
while(i-- > 0) System.out.println("q27=" + i);は i>0ならi = i-1をしてiを表示するという意味だから、 1回目のループで1を表示、2回目のループで0を表示、 3回目でループ終了。よって 1 0 が表示される。 |
class test28{ public static void main(String args[]){ int num=10000; for(int i = 0; i < 4; i++) num>>= i; System.out.println(num); } } |
[nw0523:~] javac test28.java [nw0523:~] java test28 156 |
(10000)10 =(1001110001000)2であり、for(i=0; i < 4; i++) num >>= i; はi<4ならnum>>iをし終わってからi=i+1であるから、計6回右シフトする。 すると、(1001100)2なので10進数になおすと156となる。 |
class test29{ 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); } } |
[nw0523:~] javac test29.java [nw0523:~] java test29 12 |
for文でiを1から10まで増やしていき、そしてif文で numを前置きインクリメントした値がiで割り切れる場合は numを後置きインクリメントで1増加させるのを繰り返させる。 for文ループを抜けた時点でnumは11。++numを出力するので 12が表示される。 |
class test30{ public static void main(String args[]){ int a = 9; if(a++ != 10 | a++ == 10) a++; System.out.println(a); } } |
[nw0523:~] javac test30.java [nw0523:~] java test30 12 |
このif文はa++が10でない、またはa++が10のときa++するのであり、 ここでa=9であり、10ではないのでa++する。するとaは10となるので a++する。するとaは11となりさらにa++するのでaは12となる。表示 されるのは12である。 |
class test31{ public static void main(String args[]){ for( int i=0; i<5; i++) System.out.println("i==" + i); System.out.println("Hello"); } } |
[nw0523:~] javac test31.java [nw0523:~] java test31 i==0 i==1 i==2 i==3 i==4 Hello |
System.out.println("Hello");の上の行までがfor文であるから System.out.println("Hello");はfor文の外なので、Helloが 表示されるのは1回。 |
class test32{ public static void main(String args[]){ int i; for(i=0;i<9;i+=3){} System.out.println(i); } } |
[nw0523:~] javac test32.java [nw0523:~] java test32 9 |
for(i = 0; i < 9; i += 3)はi<9ならi = i+3であり、for(i = 0; i < 9; i += 3){} よりSystem.out.println(i);はfor文の外にあるので、条件式の最後に計算された値 が表示される。最後に出る値は9よって表示されるのは9となる。 |
class test33{ public static void main(String args[]){ for(int i=0; i<8; i ++){ System.out.println(i); i += 3; } } } |
[nw0523:~] javac test33.java [nw0523:~] java test33 0 4 |
i<8 ならiを表示してi = i+3をし終わってからi = i+1をすればよい。 1回目のループでは0を表示、2回目のループは4を表示して、3回目の ループは条件から外れるため終了。よって0 4が表示される。 |
class test34{ public static void main(String args[]){ int i=0; for(sayHello(); i <= 6; i += 3){ sayHello(); } } static void sayHello(){ System.out.println("Hello"); } } |
[nw0523:~] javac test34.java [nw0523:~] java test34 Hello Hello Hello Hello |
for(sayHello(); i <= 6; i += 3)はi<=6の間はi=i+3してからsayHello()だから、 このループは4回繰り返されるので、4回Helloが表示される。 |
class test35{ 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; } |
[nw0523:~] javac test35.java [nw0523:~] java test35 3000 |
numにstaticがついているので、Playerを新しく呼び出しても値が保存されるから、 Player.num += p1.idは0+1000=1000そしてstaticによりPlayer.num += p2.idは 1000+2000=3000、したがって表示されるのは3000となる。 |
class test36{ 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; } |
[nw0523:~] javac test36.java [nw0523:~] java test36 p1.num==3000 p2.num==3000 |
numにstaticがついているので、Playerを新しく呼び出しても値が保存されるから、 Player.num += p1.idは0+1000=1000そしてstaticによりPlayer.num += p2.idは 1000+2000=3000、さらにstaticによって保存された値は3000だけp1.num==3000 p2.num==3000が表示される。 |
class test37{ public static void main(String args[]){ 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; } |
[nw0523:~] javac test37.java [nw0523:~] java test37 8000 |
Card c3 = c1という記述があるから、c1.depositとc3.depositは同じ値をとる。 したがってnumはc1=3000,c2=2000,c3=3000を足した値の8000となる。 |
class test38{ public static void main(String args[]){ 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); } } class Card{ int deposit; } class Bank{ public static void useCard(Card c ){ c.deposit -= 500; } } |
[nw0523:~] javac test38.java [nw0523:~] java test38 c1.deposit=1000 c2.deposit=1000 |
A行でCard c2 = c1という記述があるので、c2.deposit = 2000;だからc1.deposit = 2000となり、 deposit = deposit-500という記述から、Bank.useCard(c1)は2000-500=1500、また、 Bank.useCard(c2)は1500-500=1000となるから、c1.deposit = c2.depositが分かるが、 A行の時点でCard c2 = c1をCard c1 = c2とみても同じ結果が得られる。 |