【C#-題庫】控制結構
1.底下的程式碼會印出?
int i = 0, j = 0; label: i++; j+=i; if (i < 10) { Console.Write(i +" "); goto label; }
A. 印出1 到 9
B. 印出0 到 8
C. 印出2 到8
D. 印出2 到9
E. 在標籤label處發生編譯錯誤
Answer: Option A
2.下面程式片段輸出為何?
int i = 20 ; for( ; ; ) { Console.Write(i + " "); if (i >= -10) i -= 4; else break; }
A. 20 16 12 84 0 -4 -8
B. 20 16 12 8 4 0
C. 20 16 12 8 4 0 -4 -8 -12
D. 16 12 8 4 0
E. 16 8 0 -8
Answer: Option C
3.底下程式片段輸出為何?
namespace IndiabixConsoleApplication { public enum color { red, green, blue }; class SampleProgram { static void Main (string[ ] args) { color c = color.blue; switch (c) { case color.red: Console.WriteLine(color.red); break; case color.green: Console.WriteLine(color.green); break; case color.blue: Console.WriteLine(color.blue); break; } } } }
A. red
B. blue
C. 0
D. 1
E. 2
Answer: Option B
4.那個程式片段可以得到和下列程式片段一樣的結果?
int i = 0; do { Console.WriteLine(i); i+ = 1; } while (i <= 10);
A.
int i = 0; do { Console.WriteLine(i); } until (i <= 10);
B.
int i; for (i = 0; i <= 10 ; i++) Console.WriteLine(i);
C.
int i = 0; while (i <= 11) { Console.WriteLine(i); i += 1; }
D.
int i = 0; do while ( i <= 10) { Console.WriteLine(i); i += 1; }
E.
int i = 0; do until (i <= 10) { Console.WriteLine(i); i+=1; }
Answer: Option B
5.底下程式片段輸出為格?
int val; for (val = -5; val <= 5; val++) { switch (val) { case 0: Console.Write ("India"); break; } if (val > 0) Console.Write ("B"); else if (val < 0) Console.Write ("X"); }
A. XXXXXIndia
B. IndiaBBBBB
C. XXXXXIndiaBBBBB
D. BBBBBIndiaXXXXX
E. Zero
Answer: Option C
6.底下程式片段輸出為何?
char ch = Convert.ToChar ('a' | 'b' | 'c'); switch (ch) { case 'A': case 'a': Console.WriteLine ("case A | case a"); break; case 'B': case 'b': Console.WriteLine ("case B | case b"); break; case 'C': case 'c': case 'D': case 'd': Console.WriteLine ("case D | case d"); break; }
A. case A | case a
B. case B | case b
C. case D | case d
D. Compile Error
E. No output
Answer: Option C
7.底下那個控制結構程式片段是錯的?
A.
if (Condition1) {// Some statement}
B.
if (Condition1) {// Some statement} else {// Some statement}
C.
if (Condition1) {// Some statement} else {// Some statement} else if ( Condition2){//Some statement}
D.
if ( Condition1 ) {// Some statement} else if ( Condition2 ) {// Some statement} else {// Some statement}
E.
if ( Condition1 ) {// Some statement} else if ( Condition2 ) {// Some statement} else if ( Condition3 ) {// Some statement} else {// Some statement}
Answer: Option C
8.底下那個程式片段可以正確地決定出ODD(奇數)或EVEN(偶數)
1.
int a; String res; if (a % 2 == 0) res = "Even"; else res = "Odd";
2.
int a; String res; if (a Mod 2 == 0) res = "Even"; else res = "Odd";
3.
int a; Console.WriteLine(a Mod 2 == 0 ? "Even": "Odd");
4.
int a; Console.WriteLine(a Mod 2 == 0 ? "Even": "Odd");
A. 1, 3
B. 1 Only
C. 2, 3
D. 4 Only
E. 以上皆非
Answer: Option B
10. 那一個敘述可以用來結束一個while迴圈,並且將控制權轉移到迴圈外部?
- exit while
- continue
- exit statement
- break
- goto
A. 1, 3
B. 2, 4
C. 3, 5
D. 4, 5
E. 以上皆非
Answer: Option D
11.底下的程式片段是用來產生何種性質的數值?
int i = 1, j = 1, val; while (i < 25) { Console.Write(j + " "); val = i + j; j = i; i = val; }
A. 質數 Prime
B. 費波拿契數列 Fibonacci
C. 迴文數 Palindrome
D. 奇數 Odd
E. 偶數 Even
Answer: Option B
12.底下那個敘述對下面的程式片段是正確的?
if (age > 18 && no < 11) a = 25;
1.只在 age > 18 條件評值為True時,條件式 no < 11 將才會被評估。
2.任一條件式為真(true) a = 25才會被執行。
3.只在 age > 18 條件評值為False時,條件式 no < 11 將才會被評估。
4.二個條件式皆為True時, a = 25才會被執行。
5.&& 為眾所週知的最短路徑運算子(short circuiting logical operator)。
A. 1, 3
B. 2, 5
C. 1, 4, 5
D. 3, 4, 5
E. 以上皆非
Answer: Option C
13.底下程式片段輸出為何?
int i = 2, j = i; if (Convert.ToBoolean((i | j & 5) & (j - 25 * 1))) Console.WriteLine(1); else Console.WriteLine(0);
A. 0
B. 1
C. 編譯錯誤 Compile Error
D. 執行時期錯誤 Run time Error
Answer: Option A
14.那個程式片段可以得到和下列程式片段一樣的結果?
int a = 1, b = 2, c = 0; if (a < b) c = a;
A.
int a = 1, b = 2, c = 0; c = a < b ? a : 0;
B.
int a = 1, b = 2, c = 0; a < b ? c = a : c = 0;
C.
int a = 1, b = 2, c = 0; a < b ? c = a : c = 0 ? 0 : 0;
D.
int a = 1, b = 2, c = 0; a < b ? return (c): return (0);
E.
int a = 1, b = 2,c = 0; c = a < b : a ? 0;
Answer: Option A
15.底下程式片段輸出為何?
int i; for(i = 0; i<=10; i++) { if(i == 4) { Console.Write(i + " "); continue; } else if (i != 4) Console.Write(i + " "); else break; }
A. 1 2 3 4 5 6 7 8 9 10
B. 1 2 3 4
C. 0 1 2 3 4 5 6 7 8 9 10
D. 4 5 6 7 8 9 10
E. 4
Answer: Option C
15.那個迴圈片段可以正確地印出底下陣列的元素?
char[ ] arr = new chart[ ] {'k', 'i','C', 'i','t'} ;
A.
do { Console.WriteLine((char) i); } while (int i = 0; i < arr; i++);
B.
foreach (int i in arr) { Console.WriteLine((char) i); }
C.
for (int i = 0; i < arr; i++) { Console.WriteLine((char) i); }
D.
while (int i = 0; i < arr; i++) { Console.WriteLine((char) i); }
E.
do { Console.WriteLine((char) i); } until (int i = 0; i < arr; i++);
Answer: Option B