1.課題
12/5のプログラミング試験のQ23〜Q38について考察せよ
 Q23
ソースプログラム
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に100が入る。  x==100
x += 1 でxに1足される。  x==101
x-- でxから1引く。  x==100
y = 200 + x でyに200+xを入れる。  y==300
System.out.println(y++)で yを出力した後に1足す。
よって出力されるのは 300
Q24
ソースプログラム
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
考察
a = 5 でaに5を代入。
a += 3 でaに3足す。
x = ++a でaに1足したものをxに代入。
よって出力結果は 9
Q25
ソースプログラム
public class q25{
public static void main(String args[]){
int a=9, b=3;
a /= b;
System.out.println(a %= b);
}
}
実行結果
0
考察
a=9,b=3で値をそれぞれに代入。
a /= b でa/bをaに代入。  a==3
a %= b でa/bの余りを出力。
よって出力されるのは 0
Q26
ソースプログラム
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文に{}がついていない場合は次の「;」までを{}内と考える。
なので、このfor文は3回繰り返され i==3,j==3 となる。
よって出力結果は i*j==3*3 であるから 9
Q27
ソースプログラム
public class q27{
public static void main(String args[]){
int i=2;
while(i-- > 0) System.out.print(i);
実行結果
10
考察
while文の場合も{}がない時は「;」までを{}内とする。
while(i-- >0)でi--となっているので後から1引く。
i(==2)は0より大きいので1引いて出力する。  1
i(==1)は0より大きいので1引いて出力する。  0
i(==0)は0より大きくないので終了。
よって出力結果は 10
Q28
ソースプログラム
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
考察
num==10000 を二進数で表すと、10011100010000 となる。
これを右シフトさせるのであるが、for文よりシフトさせるので0、1、2、3
合計6ビットシフトさせる。
よって 00000010011100 これを十進数で出力するので 156
Q29
ソースプログラム
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
考察
if文は最初 i=1 の時だけ実行され、後はnumの方が1多くて実行されない。
iは最後は10になることからnumは11になる。
出力する前に++numで1足すので出力結果は 12
Q30
ソースプログラム
public class q30{
public static void main(String args[]){
int a = 9;
if(a++ != 10 | a++ == 10) a++;
System.out.println(a);
}
}
実行結果
12
考察
a++なので条件に当てはめた後に1足す。
a(9)!=10 | a(10) ==10 となり条件を満たすので a++→a(9+1+1)+1
よって出力結果は 12
Q31 Helloは何回表示されるか述べよ。
ソースプログラム
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文に{}がない場合は次の「;」までを{}内とするので、Helloは{}外となる。
よって1回しか表示されない。
Q32
ソースプログラム
public class q32{
public static void main(String args[]){
int i;
for(i = 0; i < 9;i += 3){}
  System.out.println(i)
}
}
実行結果
9
考察
iには3足されていき、iが9以上になったときに終了するので、
最終的にはiは9となる。よって9が表示される。
Q33
ソースプログラム
public class q33{
public static void main(String args[]){
for( int i=0; i < 8; i ++){
System.out.println(i);
i += 3;
}
}
実行結果
0
4
考察
最初i=0のとき出力されるので0が表示される。
i += 3 で i=3 になり、その後i++とり1足すことになり i=4
そのまま出力されて、
0
4
そして3と1足されて i==8 となり終了
Q34
ソースプログラム
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()で1回表示。
for文は3回繰り返されるので3回表示。
合わせて4回表示される。
Q35
ソースプログラム
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 += p1.id;
System.out.println(Player.num);
}
}
class Player{
int id =0;
static int num =0;
}
実行結果
3000
考察
p1には1000、p2には2000が代入されている。
p1.numとp2.numはnumがstatic変数なので値を共有する。
p1.num += p1.id で num==1000
p2.num += p2.id で num==1000+2000=3000
よってPlayer.num==3000
Q36
ソースプログラム
class Test{
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
考察
クラスPlayerのint numはstatic変数で定義されている。
であるからp1.num p2.num Player.numは共有された状態にある。
Player.numに1000足して2000足しているので、Player.num==3000である。
p1.numもp2.numもPlayer.numと同じなので出力結果は
p1.num == 3000
p2.num == 3000
と表示される。
Q37
ソースプログラム
class Test{
public static void main(String args[]){
Card c1 = new Card();
Card c2 = cew 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
考察
Card c3 = c1; と置いているのでc1とc3は同じものと考える。
c1に1000を代入した後にc3に3000代入しているのでc1、c3==3000となる。
よって sum=3000+2000+3000=8000 となる。
Q38
ソースプログラム
class Test2{
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 );
/*c1.depositとc2.depositの関係は?*/
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;
}
}
実行結果
c1.deposit==1000
c2.deposit==1000
考察
Card c2 = c1 よりc2とc1は同じものと考える。
c1.deposit = 1000; でc2.depositも1000になり、
c2.deposit = 2000; でc1.depositも2000になる。
Bank.useCard(c1);
Bank.useCard(c2);
で2回500引かれるので、c1.deposit、c2.depositは1000になる。
よって出力結果は
c1.deposit==1000
c2.deposit==1000
となり、c1.deposit==c2.deposit という関係になる。