report#6
ProgII/e045732
Report#1
Report#2
Report#3
Report#4
Report#5
Report#6
Report#7
Report#8
Belonging-----Information Engineering Section
Student ID---045732K
Name----------Shingo Tamaki
Date------------2005/12/26(MON)
1.課題の紹介
- 12/5に行った中間試験の問23〜問38について考察せよ。
2.コード&実行結果、及び考察
☆問23☆
- コード
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
- 考察
◎"y = 200 + x;"に入る'x'は、
x = 100 + 1 - 1 = 100
となり、"y = 300"となる。出力される値は"y++"なので、'300'が出力される。
Answer => 300
☆問24☆
- コード
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 + 3 = 8"ときて、" x = ++a; "で ' +1 ' されるので、出力時、" x
= 9 "となる。
Answer => 9
☆問25☆
- コード
public class Q25 {
public static void main(String[] args) {
int a=9, b=3;
a /= b;
System.out.println(a %= b);
}
}
- 実行結果
0
- 考察
◎" a /= b; " は " a / b = a "という意味なので、
a = 9 / 3 = 3
となり、" 3 / 3 " の剰余は0なので、0が出力される。
Answer => 0
☆問26☆
- コード
public class Q26 {
ublic 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 = 0のとき、j = 1
i = 1のとき、j = 2
i = 2のとき、j = 3
i = 3
=> ループ終了
のような形となり、" i = j = 3 " となるので、
i * j = 3 * 3 = 9
よって、9が出力される。
Answer => 9
☆問27☆
- コード
public class Q27 {
public static void main(String[] args) {
int i=2;
while(i-- > 0) System.out.print(i);
}
}
- 実行結果
10
- 考察
◎ループ開始 =>
i = 2 のとき、" i -- " より、1が出力
i = 1 のとき、" i -- " より、0が出力
i = 0
=> ループ終了
改行されないので、10と出力される。
Answer => 10
☆問28☆
- コード
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
- 考察
◎10000を2進数にすると、" 10011100010000 " となり、
for文で i = 0, 1, 2, 3 と変化していき、合計6ビット右にシフトします。
よって、値は " 00000010011100 " 、10進数に戻すと、156になります。
Answer => 156
☆問29☆
- コード
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
- 考察
◎num = 0, i = 1 の状態から、
ループ開始 =>
num = 0, i = 1 => 1 % 1 = 0 => num = 2
num = 2, i = 2 => 3 % 2 = 1 => num = 3
num = 3, i = 3 => 4 % 3 = 1 => num = 4
:
:
num = 10, i = 10 => 11 % 10 = 1 => num = 11
num = 11, i = 11
=> ループ終了
" ++ num " が出力されるので、12が出力される。
Answer => 12
☆問30☆
- コード
public class Q30 {
public static void main(String[] args) {
int a = 9;
if(a++ != 10 | a++ == 10) a++;
System.out.println(a);
}
}
- 実行結果
12
- 考察
◎" a ++ != 10 " では、" a = 9 " なので、" 9 != 10 " となり、真である。
" a ++ == 10 " では、" a = 10 " なので、" 10 == 10 " となり、これも真
である。
" a = 11 " の状態で、" a ++ " が実行されるので、12が出力される。
Answer => 12
☆問31☆
- コード
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文で実行するのは、" System.out.println("i == " + i); " の部分だけ
であり、
" System.out.println("Hello"); " はループを抜けた後の1回しか実行しな
い。
Answer => 1 回
☆問32☆
- コード
public class Q32 {
public static void main(String[] args) {
int i;
for(i = 0; i<9; i+=3) {}
System.out.println(i);
}
}
- 実行結果
9
- 考察
◎for文により、i に3ずつ加算されていき、9に達したときにループが終了し、
出力される。
Answer => 9
☆問33☆
- コード
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が出力されます。3が加算されて、
次は
4が出力されます。3が加算されると、8に達するので、ループを抜けます。
Answer => 0, 4
☆問34☆
- コード
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
- 考察
◎1回目 => for文の始めの部分
2回目 => i = 0 のときによるsayHello()の呼び出し
3回目 => i = 3 のときによるsayHello()の呼び出し
4回目 => i = 6 のときによるsayHello()の呼び出し
という感じで4回出力される。
Answer => 4 回
☆問35☆
- コード
public 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
- 考察
◎ " p1.num += p1.id; " より、" p1.num = 1000 " となり、
" p2.num += p2.id; " より、" p2.num = 2000 " となる。
numはstatic変数なので、" p1.num "と " p2.num " は値を共有する。
よって、" player.num = 1000 + 2000 = 3000 " となる。
Answer => 3000
☆問36☆
- コード
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
- 考察
◎問35と同様に、numはstatic変数であり、計算していくと、" player.num =
3000 "となり、
" p1.num "、" p2.num " どちらも3000となる。
Answer => p1.num == 3000
p2.num == 3000
☆問37☆
- コード
public class Q37 {
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;
}
- 実行結果
8000
- 考察
◎ " Card c3 = c1; " により、" c3.deposit = c1.deposit = 3000 " となる。
よって、
sum = 3000 + 2000 + 3000 = 8000
となり、8000が出力される。
Answer => 8000
☆問38☆
- コード
public class Q38 {
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);
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;
}
}
- 実行結果
p1.deposit == 1000
p2.deposit == 1000
- 考察
◎Card c2 = c1; で c2 = c1 と考える。c1.deposit = 1000; とあるが、次に、
c2.deposit = 2000;
があるので、c2 = c1 = 2000 。Bank.useCard(c1); と Bank.useCard(c2);で、
一回ずつ 500 を減算代入するから、
c1 = c2 = 1000 になる。よって c1.deposit と c2.deposit は等しい。
Answer => 等しい
3.感想
- 微妙な順位だったので、次は喜べるような点数を採りたいですね。
ProgII/e045732
Report#1
Report#2
Report#3
Report#4
Report#5
Report#6
Report#7
Report#8