問題


Q23

ソースプログラム
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。

Q24

ソースプログラム
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

Q25

ソースプログラム
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

Q26

ソースプログラム
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が表示される。

Q27

ソースプログラム
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 が表示される。

Q28

ソースプログラム
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となる。

Q29

ソースプログラム
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が表示される。

Q30

ソースプログラム
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である。

Q31

ソースプログラム
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回。

Q32

ソースプログラム
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となる。

Q33

ソースプログラム
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が表示される。

Q34

ソースプログラム
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が表示される。

Q35

ソースプログラム
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となる。

Q36

ソースプログラム
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が表示される。

Q37

ソースプログラム
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となる。

Q38

ソースプログラム
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とみても同じ結果が得られる。

感想


参考文献




もどる