Q23
1. public class Q23{
2. public static void main(String[] argv){
3. int x,y;
4. x = 100; x +=1; x--; y = 200 + x;
5. System.out.println(y++);
6. }
7. }
実行結果
300
3行目で、int型の変数xとyを定義している。
4行目のx = 100で、xを100に初期化し、x +=1で、x=101にする。x--で、再びx=100にして、y = 200 + xで、y=300とな
る。
5行目は、y=300のままで出力し、その後にインクリメントする処理である。
Q24
1. public class Q24{
2. public static void main(String[] argv){
3. int a=0, x=0;
4. a = 5; a +=3; x = ++a;
5. System.out.println(x);
6. }
7. }
実行結果
9
3行目で、int型の変数a,xを定義している。
4行目のa = 5で、aを5に初期化し、a +=3で、aを8にしている。また、x = ++aで、aをインクリメントしたものをxの値にして
いる。
Q25
1. public class Q25{
2. public static void main(String[] argv){
3. int a=9, b=3;
4. a /= b;
5. System.out.println(a %= b);
6. }
7. }
実行結果
0
3行目で、int型の変数a,bを定義し、初期値をそれぞれ9と3にしている。
4行目で、aをbで割った値をaに代入している。
5行目のa %= bで、aをbで割った余りをaに代入する。
Q26
1. public class Q26{
2. public static void main(String[] argv){
3. int i, j;
4. for(i=0,j=0; i<3; i++) ++j;
5. System.out.println(i * j);
6. }
7. }
実行結果
9
3行目で、int型の変数i,jを定義している。
4行目と5行目は、0<=i<3の間jをインクリイメントし、i=3になったら、i*jを演算して出力する。
Q27
1. public class Q27{
2. public static void main(String[] argv){
3. int i=2;
4. while(i-- > 0) System.out.println(i);
5. }
6. }
実行結果
1
0
3行目でint型の変数iを定義し、初期値を2にしている。
4行目は、まずiと0を比べた後、iをデクリメントする。iを0と比べる時にi>0であれば、iの値を出力する。そして、iを0と比べ
る時にi=0になったら、処理を終える。
Q28
1. public class Q28{
2. public static void main(String[] argv){
3. int num=10000;
4. for(int i = 0; i < 4; i++) num >>= i;
5. System.out.println(num);
6. }
7. }
実行結果
156
3行目で、int型の変数numを定義し、初期値を10000にしている。
4行目で、iが4になるまでインクリメントしながら、numをiビット右シフトさせる。
Q29
1. public class Q29{
2. public static void main(String[] argv){
3. int num = 0;
4. for(int i = 1; i <= 10; i++){
5. if(++num % i ==0) num++;
6. }
7. System.out.println(++num);
8. }
9. }
実行結果
12
3行目で、int型の変数numを定義する。
4~6行目は、iを1から10までインクリメントし続けて、その間++num % i ==0ならば、num++をする。
7行目は、num+1の値をnumに代入してから、出力する。
Q30
1. public class Q30{
2. public static void main(String[] argv){
3. int a = 9;
4. if(a++ != 10 | a++ ==10) a++;
5. System.out.println(a);
6. }
7. }
実行結果
12
3行目で、int型の変数aを定義し、初期値を9にしている。
4~5行目で、aをインクリメントしたものが、10か10でない値ならば、さらにインクリメントして、その結果を出力する。
Q31
1. public class Q31{
2. public static void main(String[] argv){
3. for( int i = 0; i < 5; i++)
4. System.out.println("i == " + i);
5. System.out.println("Hello");
6. }
7. }
実行結果
i == 0
i == 1
i == 2
i == 3
i == 4
Hello
int型の変数iを定義し、初期値を0にする。そして、iをインクリメントし続けて、i<5の間は、"i == "とiの値を出力し、i=5
になったら、"Hello"を出力する。
Q32
1. public class Q32{
2. public static void main(String[] argv){
3. int i;
4. for( i = 0; i < 9; i += 3 ) {}
5. System.out.println(i);
6. }
7. }
実行結果
9
3行目で、int型の変数iを定義している。
4~5行目で、iが0~9の間+3し続けて、i=9になったら、そのiの値を出力する。
Q33
1. public class Q33{
2. public static void main(String[] argv){
3. for( int i = 0; i < 8; i++ ){
4. System.out.println(i);
5. i += 3;
6. }
7. }
8. }
実行結果
0
4
int型の変数iの初期値を0にして、インクリメントし続ける。i<8の間、iの値を出力して、iにi+3値を代入する。そして、i=8に
なった時に、処理を終える。
Q34
01. public class Q34{
02. public static void main(String[] argv){
03. int i=0;
04. for( sayHello(); i <= 6; i +=3){
05. sayHello();
06. }
07. }
08. static void sayHello(){
09. System.out.println("Hello");
10. }
11. }
実行結果
Hello
Hello
Hello
Hello
3行目で、int型の変数iを定義し、初期値を0にする。
4~5行目は、まずメソッドsayHello()を実行する。そして、i +=3し続けて、i<=6の間は
sayHello()を実行する。i=6になったら処理を終わる。
Q35
01. class Q35{
02. public static void main(String[] argv){
03. Player p1 = new Player();
04. Player p2 = new Player();
05. p1. id = 1000;
06. p2. id = 2000;
07. p1. num += p1. id;
08. p2. num += p2. id;
09. System.out.println(Player.num);
10. }
11. }
12. class Player{
13. int id = 0;
14. static int num = 0;
15. }
実行結果
3000
3~4行目で、クラスPlayerのオブジェクトp1、p2をそれぞれ生成する。
5~6行目で、オブジェクトp1のidを1000に初期化し、p2のidを2000に初期化する。
7~8行目で、numにidを加えた値をnumに代入する処理を、p1とp2で行う。
13行目で、int型の変数idを定義する。
14行目で、int型の静的変数numを定義する。
Q36
01. class Q36{
02. public static void main(String[] argv){
03. Player p1 = new Player();
04. Player p2 = new Player();
05. p1. id = 1000;
06. p2. id = 2000;
07. Player. num += p1. id;
08. Player. num += p2. id;
09. System.out.println("p1. num == " + p1. num);
10. System.out.println("p2. num == " + p2. num);
11. }
12. }
13. class Player{
14. int id = 0;
15. static int num = 0;
16. }
実行結果
p1. num == 3000
p2. num == 3000
3~4行目で、クラスPlayerのオブジェクトp1、p2をそれぞれ生成する。
5~6行目で、オブジェクトp1のidを1000に初期化し、p2のidを2000に初期化する。
7~8行目で、クラスPlayerの静的変数numにp1とp2のint型変数idを加える。
Q37
01. class Q37{
02. public static void main(String[] argv){
03. Card c1 = new Card();
04. Card c2 = new Card();
05. Card c3 = c1;
06. c1. deposit = 1000;
07. c2. deposit = 2000;
08. c3. deposit = 3000;
09. int sum = c1. deposit + c2. deposit + c3. deposit;
10. System.out.println(sum);
11. }
12. }
13. class Card{
14. int deposit;
15. }
実行結果
8000
3~4行目で、クラスCardのオブジェクトc1、c2をそれぞれ生成する。
5行目で、オブジェクトc3がc1と同じであるとしている。
6~7行目で、オブジェクトc1のdepositを1000に初期化し、c2のdepositを2000に初期化している。8行目で、c3のdepositを
3000に初期化している。ここで、c3=c1なので、c1のdepositも3000になる。
Q38
01. class Q38{
02. public static void main(String[] argv){
03. Card c1 = new Card();
04. Card c2 = c1;
05. c1. deposit = 1000;
06. c2. deposit = 2000;
07. Bank. useCard(c1);
08. Bank. useCard(c2);
09. System.out.println("c1. deposit == " + c1. deposit);
10. System.out.println("c2. deposit == " + c2. deposit);
11. }
12. }
13. class Card{ int deposit; }
14. class Bank{
15. public static void useCard( Card c ){c. deposit -= 500; }
16. }
実行結果
c1. deposit == 1000
c2. deposit == 1000
テスト問題では、09行目と10行目はなく、08行目まで終えたときのc1. depositとc2. deposiの関係を示すものであったが、
c1. depositとc2. depositの値を出力すればわかりやすくなると思い、09行目と10行目を付け足した。
03行目で、Card()のオブジェクトc1を生成し、04行目で、オブジェクトc2がc1と同じであるとしている。
05~06行目で、c1のdepositを1000に初期化し、c2のdepositを2000に初期化している。しかし、c2=c1なので、c1の
dipositも2000になる。
クラスBankは、c. deposit から500を引いた値を、c. deposit に代入する処理を行う。よって、07行目は、c1. deposit
から500を引いた値を、c1. deposit に代入する処理であり、08行目は、c2. deposit から500を引いた値を、c2. deposit
に代入する処理である。
c1とc2は同じであることがわかっているので、08行目まで終えた時のc1. depositとc2. depositの関係は、
c1. deposit=c2. depositである