Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quiz #3. 6.Show the final output of the program execution given in Fig.1. public class A{ int x=1;  實體變數 public int m01(int y){ int x = 3;  區域變數 return.

Similar presentations


Presentation on theme: "Quiz #3. 6.Show the final output of the program execution given in Fig.1. public class A{ int x=1;  實體變數 public int m01(int y){ int x = 3;  區域變數 return."— Presentation transcript:

1 Quiz #3

2 6.Show the final output of the program execution given in Fig.1. public class A{ int x=1;  實體變數 public int m01(int y){ int x = 3;  區域變數 return x+y;  此 X 使用區域變數 } public int m02(int y){ return x+y;  此 X 使用實體變數 } public void main(){ int y; int z; y=x+m01(3);  y=1+m01(3)=1+(3+3)=7 x=10-m01(9);  此 X 使用實體變數∴改變的值為一開始宣告的 x 的值  x=10-m01(9)=10-(3+9)=10-12=-2 z=y+m02(22);  z=7+m02(22)=7+(-2+22)=27 println(x, y, z); } Answer : -2 7 27

3 7.Show the final output of the program execution given in Fig.2. public class A{ int x=1; public int m01(int y){ if (y%2==0)  如果 y 是偶數的話, return x+y {return x+y} else  如果 y 是奇數的話, return y*2 {return y*2} } public void main(){ int i, s, t; s=0; i=1; while (i =-10){  當 -10≤i≤25 跑 while 裡的指令 s=s+m01(i);  當 i 是偶數 return x+i  當 i 是奇數 return i*2 t=s%3;  依 t mod 3 的結果選擇執下列其中之一 if (t==0){i=i-1}; if (t==1){i=i+2}; if (t==2){i=i+3}; } println(s); println(i); } Answer : 433 26

4 8.How many multiplication (*) and addition (+) are performed respectively when executing the program given in Fig.2. public class A{ int x=1; public int m01(int y){ if (y%2==0)  如果 y 是偶數的話,這裡會使用一次 ”+” {return x+y} else  如果 y 是奇數的話,這裡會使用一次 ”*” {return y*2} } public void main(){ int i, s, t; s=0; i=1; while (i =-10){ s=s+m01(i);  這裡會使用一次 ”+”  m01 裡則依奇 / 偶數,使用 ”*” 或 ”+” t=s%3; if (t==0){i=i-1}; if (t==1){i=i+2};  這裡會使用一次 ”+” if (t==2){i=i+3};  這裡會使用一次 ”+” } println(s); println(i); } Answer : 44 次 ”+” 9 次 ”*”

5 9.Show the final output of the program execution given in Fig.3 public class A{ int x=1; public void main(){ int s,t; int i,j,k; s=0; i=1; K=0; while (i<=10){  當 i≤10 跑 while 裡的指令 for (j=1;j<=i;j++)  初始 j=1 ;當 j ≤i 跑 while 裡的指令 { while (k<=(i+j))  當 k ≤ (i+j) 跑 while 裡的指令 { s=s+i+j; k=k+1; } i=i+3;  當 for 結束後,更新 i (i=i+3) } println(s); } Answer : 216

6 10.How many addition (+) are performed when executing the program given in Fig.3. public class A{ int x=1; public void main(){ int s,t; int i,j,k; s=0; i=1; K=0; while (i<=10){ for (j=1;j<=i;j++)  這裡會執行一次 ”+” { while (k<=(i+j))  這裡會執行一次 ”+” { s=s+i+j;  這裡會執行二次 ”+” k=k+1;  這裡會執行一次 ”+” } i=i+3;  這裡會執行一次 ”+” } println(s); } Answer : 110

7 11.Show the activity diagram for the main( ) method given in Fig. 3.

8 Quiz #4

9 4 、 Assume in a main(), two variables are instantiated as class B and C objects. Show the output after executing method “ m01 ” respectively. public class A{ private int x=1; public int m01(int y){ return x+y; }}… public class B{ public x=3; public int m01(int y){ A x = new A(); println(x.x); return y }}… public class C{ public x=5; public int m01(int x){ A z = new A(); println(z.m01(10)); println(this.x); return y }}… 私有成員 “this” 表示「目前這個物件」


Download ppt "Quiz #3. 6.Show the final output of the program execution given in Fig.1. public class A{ int x=1;  實體變數 public int m01(int y){ int x = 3;  區域變數 return."

Similar presentations


Ads by Google