Report#4
オフライン試験のQ23〜Q38のついて考察せよ。{〜12/25(Mon)}
* コードと実行結果を示し、各問について考察すること。
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++);
}
}
|
実行結果
考察
x = 100;
x +=1; // x = x + 1 なので x = 101
x--; // x = x - 1 なので x = 100
y = 200 + x; // y = 200 + 100 で y = 300
System.out.println(y++); //後置きインクリメントなので、出力するとき
はまだインクリメントされない。よって実行結果は 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);
}
}
|
実行結果
考察
int a=0, x=0;
a=5;
a +=3; // a = a + 3 であるから a = 8
x = ++a; // x = a + 1 であるから x = 9
System.out.println(x); // xの値(9)を出力する
Q25
ソースプログラム
public class q25{
public static void main(String args[]){
int a=9, b=3;
a /= b; System.out.println(a %= b);
}
}
|
実行結果
考察
int a=9, b=3;
a /= b; // a = a / b よって a = 3
System.out.println(a %= b); // a = a % b = 3 % 3の値(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);
}
}
|
実行結果
考察
for(i=0, j=0; i<3; i++) ++j;//i<3の時iとjをインクリメントするから
ループを抜けるときはiもjも3。
System.out.println(i * j); // i * j の値(9)を出力
・このループ内での処理は、インクリメント野市関係から++j , i++の順番に
行われる。
Q27
ソースプログラム
public class q27{
public static void main(String args[]){
int i=2;
while(i-- > 0) System.out.print(i);
|
実行結果
考察
ループ1周目では i = 2 (>0) その後デクリメントされ i = 1 となり 1が
出力。但し改行されない。
ループ2周目では i = 1 (>0) その後デクリメントされ i = 0 となり 0が
出力。
ループ3周目では i = 0 であるからループから抜ける。
したがって実行すると10(1と0)が出力される。
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);
}
}
|
実行結果
考察
ループの1周目では、i = 0 (<4) であるから 10000 >>= 0 → num =
10000
ループの2周目では、i = 1 (<4) であるから 10000 >>= 1 → num =
5000
ループの3周目では、i = 2 (<4) であるから 5000 >>= 2 → num =
1250
ループの4周目では、i = 3 (<4) であるから 1250 >>= 3 → num =
156
ループの5周目では、i = 4 となるからループから抜ける。
よって実行すると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);
}
}
|
実行結果
考察
if文は最初i=1の時は実行されるが、2回目からは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);
}
}
|
実行結果
考察
a++なので条件に当てはめた後に+1をすればいい。
a++ != 10では、aは9(条件に当てはめた後に+1する)。a++ == 10では、aは
10(条件に当てはめた後に+1する)。
この時点でのaの値は11になっていて、最後にまたa++があるので、+1して、
出力結果は12になる。
Q31
ソースプログラム
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文、while文ではセミコロンまでが処理内容である。
よって、System.out.println("Hello");はfor文の影響を受けていない。
よってHelloが出力されるのは一回だけ。
Q32
ソースプログラム
public class Q32{
public static void main(String args[]){
int i;
for(i = 0; i < 9;i += 3){}
System.out.println(i)
}
}
|
実行結果
考察
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;
}
}
}
|
実行結果
考察
for文中の処理は次のようになる
i = 0 (<8)→ System.out.println(i);→ i += 3 (i=3)→ i++ (i=4)→
→ i = 4 (<8)→ System.out.println(i); → i += 3 (i=7)→ i++ (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");
}
}
|
実行結果
考察
for文のfor( sayHello(); i <= 6; i += 3)の条件の部分で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 += p2.id;
System.out.println(Player.num);
}
}
class Player{
int id = 0;
static int num = 0;
}
|
実行結果
考察
p1には1000、p2には2000が入っている。また、numはstatic変数なので値を共
有している。
p1.num += p1.idで、num=1000となり、p2.num += p2.idで、num=1000+2000、
よって3000が出力。
Q36
ソースプログラム
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
|
考察
ここでもnumはstatic変数で定義されているので共有されている。
さっきと同じようにしいていくと、numには3000が入る。
p1.numとp2.numが出力されるようになっているが、共有ということなので値
が同じ。よって、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;
}
|
実行結果
考察
Card c3 = c1とあるので、c3とc1は同じものとして考えられる。
c1に1000入れた後にc3に3000入れているので、c1の1000が3000に置き換えら
れ、c1、c3とも3000が入る。
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 );
/* 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;
}
}
|
実行結果
c1.deposit==1000
c2.deposit==1000
|
考察
Card c2 = c1とあるので、c1=c2と考える。
c1.deposit =1000でc1,c2とも1000が入る。
c2.deposit =2000でc1,c2とも2000が入る。
Bank.useCard( c1 )で500引かれるが、c1とc2は同じなので、次の
Bank.useCard( c2 )でも
500引かれることになり、合計で1000引かれる。
よって、Aの時点では、c1.depositに1000、c2.depositに1000となっているの
で、
c1.deposit=c2.depositという関係。
感想