สำหรับคนเขียน C# วันนี้มี Test เ้ล็ก ๆ น้อย ๆ มาให้ทำครับ
posted on 04 Jul 2005 11:36 by tidno1 in csharp-and-dotnetมี test เล็ก ๆ น้อย ๆ มาให้ทำกันเล่น ๆ ครับ บางข้อก็สำหรับคนที่หัดเขียนมาบ้าง บางข้อนี่สำหรับคนที่เขียนเป็น แล้วก็อาจจะตอบไม่ถูกได้ (ตอนคิดโจทย์บางข้อ ผมยังตอบผิดเลย)
โปรแกรมนี้ให้ค่าอะไร ถ้าอินพุทเป็น 30
float cel, fah;
cel = float.Parse(Console.ReadLine());
fah = 9 / 5 * cel + 32;
Console.WriteLine("{0} Celsius = {1} Fahrenheit", cel, fah);
cel = float.Parse(Console.ReadLine());
fah = 9 / 5 * cel + 32;
Console.WriteLine("{0} Celsius = {1} Fahrenheit", cel, fah);
code ด้านล่างข้อไหนที่สามารถคอมไพล์ผ่าน
string s;
s = "c:\windows\system32";
Console.WriteLine(s);
s = "c:\windows\system32";
Console.WriteLine(s);
string s;
s = "c:\temp";
Console.WriteLine(s);
s = "c:\temp";
Console.WriteLine(s);
string s;
s = "10";
s = s + 5; Console.WriteLine(s);
s = "10";
s = s + 5; Console.WriteLine(s);
โปรแกรมข้างล่างเหล่านี้จะคอมไพล์ผ่านหรือไม่
int x = 7++;
float f = 1.5f;
f++;
float f = 1.5f;
f++;
int x = 0, y = 0;
Console.WriteLine(x+++++y);
Console.WriteLine(x+++++y);
การประกาศ Main method ในข้อใดที่คอมไพล์ผ่านบ้าง
-
class MainClass {
private static void Main() {
}
} -
class MainClass {
public static int Main() {
return 0;
}
} -
class MainClass {
public static void Main() {
}
public static void Main(string[] args) {
}
} -
class MainClass {
public static void Main() {
}
public void Main() {
}
} -
class MainClass {
public static void Main() {
}
public static void Main(int n) {
}
} -
class MainClass {
public static void Main() {
}
}
class SecondClass {
public static void Main() {
}
}
-
class MainClass {
public static void Main() {
;return;
}
}
-
class MainClass {
public static void Main()
{ { } }
}
- $$$
- _
- @ten
- @at@
- @class
- @x-y
- Main
- args
class MainClass {
....
};
ถามว่าจะคอมไพล์ผ่านมั้ย
....
};
ข้อแรกนะงับ พฐ. คือ +- ทำก่อน */ดังนั้นจะได้ว่า
9 / 5 * (30 + 32)
1.8 * 62
111.6 (มั่ว ๆ 555)
ข้อ 2 นะงับ
การประกาศ string โดยมีเครื่องหมาย \ อยู่ใน string แบบเดี่ยวๆ ให้ใส่ @ ไว้หน้า string นะครับ ดังนั้น อันที่ 1 ไม่ผ่านจ้า แต่ในกรณีของ อันที่ 2 นะครับ "\t" แทน TAB character ดังนั้นจึง
ผ่านจ้า อันที่ 3 ระวังหน่อยเพราะ C# ไม่ auto casting ให้นะถ้า VB.Net ผ่านฉลุยจ้า
ข้อ 3 ต่อเลย อันแรกผ่านแน่นอนเพราะ operand ++ ไม่จำกัดว่าใช้ได้แต่ int ครับสามารถใช้งานกับ indexer หรือ storage location ได้ด้วย
แต่อันที่ 2 จนปัญญาตอบให้ครับ ถ้าเห็นน้องคนไหนเขียนผมคงสั่งให้แก้ไปแล้วหล่ะครับอ่านเข้าใจยาก 555
ข้อ 4 Main method ข้อจำกัดมีแค่
1. return type เป็น void หรือ int
2. parameter เป็น void หรือ string array
3. public static function เท่านั้นที่ถูกพิจารณา
ข้อที่ผ่านแน่นอนแบบไม่ต้องคิดมากคือ
2,3,4
ข้อ 6 สามารถทำได้นะครับแต่หมายถึงคุณจะมี entry point มากกว่า 1
ข้อ 7, 8 จะมี warning (แต่ข้อ 8 เหมือนไม่น่าจะผ่านนะ)
ข้อ 1 error เพราะหา entry point ไม่เจอ (ประกาศเป็น private) ดังนั้น error ครับ
ข้อ 5 งง งง
อันที่ 2 ถ้ามีอย่างอื่นต่อท้ายผ่านถ้ามาเดี่ยวๆ เดี้ยง
8 ผ่านแน่ๆ ช่างมัน
7 ไม่แน่ใจ ที่เหลือคิดว่าไม่ผ่าน
ข้อสุดท้ายน่าจะแค่ warning นะไม่แม่นแล้ว 555
#1 By Zeeeeed (58.8.51.179) on 2006-05-30 23:39