☆課題4☆


試験問題考察





Q23
class Q23{
    public static void main(String argv[]){
        int x,y;
        x = 100;
        x += 1;
        x--;
        y = 200 + x;
        System.out.println(y++);
    }
}


実行結果
  300  


〜考察〜

x = 100 ;x = x + 1;」であるためこの段階でx = 101。 次に「x--;」なのでx =100となる。
次の「y = 200 + x;」でy = 200 + 100 = 300となるため、実行結果は300となる。



Q24
class Q24{
    public static void main(String argv[]){
        int a=0, x=0;
        a = 5;
        a += 3;
        x = ++a;
        System.out.println(x);
    }
}
    


実行結果
   9   


〜考察〜

「a = 5; a += 3;」なので、この段階でa = a + 3 = 5 + 3 = 8となる。
次の「x = ++a;」でx = 8 + 1 = 9となるので、実行結果は9となる。



Q25
class Q25{
    public static void main(String argv[]){
        int a=9, b=3;
        a /= b;
        System.out.println(a %= b);
    }
}


実行結果
   0   


〜考察〜

「a /= b;」なのでa = a / b = 9 / 3 = 3となる。
「a %= b」とあるのでa = 3 % 3 = 0となるので、実行結果は0となる。



Q26
class Q26{
    public static void main(String argv[]){
        int i, j;
        for(i=0,j=0; i<3; i++) ++i;
        System.out.println(i * j);
   }
}


実行結果
   0   


〜考察〜

for文でi<3の時iとjをインクリメントする。つまり、ループを抜けるときiとjは3となる。
i * j = 3 * 3 = 9となるので実行結果は9となる。



Q27
class Q27{
    public static void main(String argv[]){
        int i=2;
        while(i-- > 0)
            System.out.println(i);
    }
}


実行結果
   1  
   0   


〜考察〜

整数型の変数i(2で初期化)を定めて、while文の条件がtrueのままの2回だけループが起こる。
よって、答えは0と1である。



Q28
class Q28{
    public static void main(String argv[]){
        int num=10000;
        for(int i = 0; i < 4; i++) num >>=i;
        System.out.println(num);
    }
}


実行結果
  156 


〜考察〜

10000という数を2進数であらわすと、「10011100010000」である。
問題では、その数をiの値分右にシフトさせているから、1回目のループでunmの値は
「1001110001000」となり、2回目のループで「10011100010」となり、
3回目のループで「10011100」となる。
ループは3回で崩れるから、あとは2進数の「10011100」という値が残り、
この数を10進数で直した値が出力されている。 よって、答えは156である。



Q29
class Q29{
    public static void main(String argv[]){
        int num = 0;
        for(int i = 1; i <= 10; i++){
            if(++num % i ==0) num++;
    }
        System.out.println(++num);
    }
}


実行結果
  12  


〜考察〜

for文中のif文のループにおいて、整数型の変数numに1加えた値が整数型の変数i(1で初期化)
の値で割り切れるかどうかを判断し、そうであればnumを更に1を加える。
numの値は11でループを抜けるのだが、最後に出力される際に1を加えている。
よって、答えは12である。



Q30
class Q30{
    public static void main(String argv[]){
        int a = 9;
        if(a++ != 10 | a++ ==10) a++;
        System.out.println(a);
    }
}


実行結果
  12  


〜考察〜

a++なので条件に当てはめた後に+1する。
a(=9)!=10 | a(==10) ==10となり条件満たすのでa++{a(==11)+1}
よって出力結果は 12



Q31
class Q31{
    public static void main(String argv[]){
        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は{}外になる。
よって一回しか表示されない。



Q32
class Q32{
    public static void main(String argv[]){
        int i;
        for(i = 0; i < 9; i += 3){}
        System.out.println(i);
    }
}


実行結果
   9   


〜考察〜

iには3たされていき、iが9以上になった時に終了するので最終的にiは9になる。
よって9が表示される。



Q33
class Q33{
    public static void main(String argv[]){
        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 argv[]){
        int i = 0;
        for(sayHello(); i <= 6; i += 3){
            sayHello();
    }
}

    static void sayHello(){
        System.out.println("Hello");
    }
}


実行結果
 Hello
 Hello
 Hello
 Hello  


〜考察〜

まず最初にfor(sayHello();...のsayHello()でsayHelloメソッドにとぶ。
i<=6に条件を満たすときsayHello();を実行するとうい事だから、
i==0で2回目のsayHello();を実行する。
i+=3でi==3になり次のループにうつり3回目のsayHello();を実行,
またi+=3でi==6となり4回目のsayHello()を実行、その後i==9となるため条件から外れる。
よってHelloは4回出力されることになる。



Q35
class Q35{
    public static void main(String argv[]){
        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 


〜考察〜

staticがついてるのでPlayerを新しく呼び出しても値が保存される。
よって
p1.num += p1.id → num = 0+1000 = 1000
p2.num += p2.id → num = 1000+2000 = 3000 なので
表示されるのは 3000



Q36
class Q36{
    public static void main(String argv[]){
        Player1 p1 = new Player1();
        Player1 p2 = new Player1();
        p1.id = 1000;
        p2.id = 2000;
        Player1.num += p1.id;
        Player1.num += p2.id;
        System.out.println("p1.num =="+p1.num);
        System.out.println("p2.num =="+p2.num);
    }
}

class Player1{
    int id = 0;
    static int num = 0;
}


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


〜考察〜

Playerクラスで宣言されている整数型の変数numは「static」なので、
オブジェクトのp1、p2で共有されている。変数numの値は「Player.num += 1000」と
「Player.num += 2000」という処理で3000となる。



Q37
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 = 3000となる。「int sum = c1.deposit + c2.deposit + c3.deposit;」は
int sum = 3000 + 2000 + 3000= 8000となる。よって実行結果は8000となる。



Q38
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 = " + 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;」となっているので、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となっている。
よって実行結果は両方とも1000になる。



〜感想〜

今回のテストのこの課題の部分はナカナカできたので良かった。