实例分析

(1) 在下面的例子里

  1. using System;
  2. class A
  3. {
  4. public A()
  5. {
  6. PrintFields();
  7. }
  8. public virtual void PrintFields(){}
  9. }
  10. class B:A
  11. {
  12. int x=1;
  13. int y;
  14. public B()
  15. {
  16. y=-1;
  17. }
  18. public override void PrintFields()
  19. {
  20. Console.WriteLine(“x={0},y={1}”,x,y);
  21. }

当使用new B()创建B的实例时,产生什么输出?

答:X=1,Y=0;x= 1 y = -1

(2) 分析以下代码

  1. stringstrTmp = abcdefg某某某”;
  2. inti= System.Text.Encoding.Default.GetBytes(strTmp).Length;
  3. intj= strTmp.Length;

以上代码执行完后,i= j=

答:i=13,j=10

(3) SQLSERVER服务器中

给定表table1 中有两个字段 ID、LastUpdateDate,

ID表示更新的事务号,LastUpdateDate表示更新时的服务器时间,请使用一句

SQL语句获得最后更新的事务号

答:

  1. Select ID FROM table1 Where LastUpdateDate = (Select MAX(LastUpdateDate)FROM table1)

(4) 公司要求开发一个继承System.Windows.Forms.ListView类的组件

要求达到以下的特殊功能:点击ListView各列列头时,能按照点击列的每行值进行重排视图中的所有行 (排序的方式如DataGrid相似)。根据您的知识,请简要谈一下您的思路

答:根据点击的列头,包该列的ID取出,按照该ID排序后,在给绑定到ListView中。

(5) 写出一条Sql语句:

取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的)。

答:解1: select top 10 * from A where id not in (select top 30 id fromA)

(6) 一列数的规则如下: 1、1、2、3、5、8、13、21、34… 求第30位数是多少,用递归算法实现。

  1. public class MainClass
  2. {
  3. public static void Main()
  4. {
  5. Console.WriteLine(Foo(30));
  6. }
  7. public static int Foo(int i)
  8. {
  9. if (i <= 0)
  10. return 0;
  11. else if(i > 0 && i <= 2)
  12. return 1;
  13. else return Foo(i -1) + Foo(i - 2);
  14. }
  15. }

(7) 求以下表达式的值,写出您想到的一种或几种实现方法:1-2+3-4+……+m

  1. int Num = this.TextBox1.Text.ToString() ;
  2. int Sum = 0 ;
  3. for (int i = 0 ; i < Num + 1 ; i++)
  4. {
  5. if((i%2) == 1)
  6. {
  7. Sum += i ;
  8. }
  9. else
  10. {
  11. Sum = Sum - I ;
  12. }
  13. }
  14. System.Console.WriteLine(Sum.ToString());
  15. System.Console.ReadLine() ;

(8) shorts1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?

short s1 = 1; s1 = s1 + 1;有错,s1是short型,s1+1是int型,不能显式转化为short型。

可修改为s1 =(short)(s1 + 1) 。short s1 = 1; s1 += 1正确。

(9) 需要实现对一个字符串的处理,首先将该字符串首尾的空格去掉,如果字符串中间还有连续空格的话,仅保留一个空格,即允许字符串中间有多个空格,但连续的空格数不可超过一个.

  1. string inputStr=" xx xx “;
  2. inputStr=Regex.Replace(inputStr.Trim(),”*"," ");

(10) 某一密码仅使用K、L、M、N、O共5个字母,密码中的单词从左向右排列,密码单词必须遵循如下规则:

(1) 密码单词的最小长度是两个字母,可以相同,也可以不同

(2) K不可能是单词的第一个字母

(3) 如果L出现,则出现次数不止一次

(4) M不能使最后一个也不能是倒数第二个字母

(5) K出现,则N就一定出现

(6) O如果是最后一个字母,则L一定出现

问题一:下列哪一个字母可以放在LO中的O后面,形成一个3个字母的密码单词?

A)K B)L C) M D) N

答案:B

问题二:如果能得到的字母是K、L、M,那么能够形成的两个字母长的密码单词的

总数是多少?

A)1个 B)3个 C)6个 D)9个

答案:A

问题三:下列哪一个是单词密码?

A)KLLN B) LOML C) MLLO D)NMKO

答案:C

(11) 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。

解1:
  1. select top 10 * from A where id not in (select top 30 id from A)

演变步骤:

1)select top 30 id from T_FilterWords—取前条

2)select * from T_FilterWords

where id not in (select top 30 id from T_FilterWords)—取id不等于前三十条的

—也就是把前条排除在外

3)select top 10 * from T_FilterWords

where id not in (select top 30 id from T_FilterWords)

—取把前条排除在外的前条,也就是-40条

解2:
  1. select top 10 * from A where id > (select max(id) from (select top 30 id from A )as A)
解答3:

用ROW_NUMBER实现

(11) 给定以下XML文件,完成算法流程图。

  1. <FileSystem>
  2. < DriverC >
  3. <Dir DirName=”MSDOS622”>
  4. <File FileName = Command.com></File>
  5. </Dir>
  6. <File FileName =”MSDOS.SYS” ></File>
  7. <File FileName = IO.SYS></File>
  8. </DriverC>
  9. </FileSystem>

请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。

答:
  1. //方法一
  2. voidFindFile( Directory d )
  3. {
  4. FileOrFolders = d.GetFileOrFolders();
  5. foreach( FileOrFolder fof in FileOrFolders)
  6. {
  7. if(fof is File)
  8. You Found a file;
  9. else if (fof is Directory)
  10. FindFile( fof );
  11. }
  12. }
  13. //方法二
  14. Public void DomDepthFirst(XmlNode currentNode)
  15. {
  16. XmlNode node=currentNode.FirstChild;
  17. while(node!=null)
  18. {
  19. if(node.Name=="File")
  20. {
  21. Console.Write(((XmlElement)node).GetAttribute("FileName")+"\r\n");
  22. }
  23. DomDepthFirst(node);
  24. node=node.NextSibling;
  25. }
  26. }

(12) 请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?

答:
  1. foreach(System.Windows.Forms.Control control in this.Controls)
  2. {
  3. if (control is System.Windows.Forms.TextBox)
  4. {
  5. System.Windows.Forms.TextBox tb =(System.Windows.Forms.TextBox)control ;
  6. tb.Text = String.Empty ;
  7. }
  8. }

(13) 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。

  1. public void test(int i)
  2. {
  3. lock(this)
  4. {
  5. if (i>10)
  6. {
  7. i--;
  8. test(i);
  9. }
  10. }
  11. }
答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)

(14) a=10,b=15,在不用第三方变量的前提下,把a,b的值互换

  1. a=a+b;
  2. b=a-b;
  3. a=a-b;

(15) 还有变态要求,需要代码最短呢。有两个结果:

1) a^=b^(b^=a^b); // 13个字节 2) a=b+(b=a)*0; // 11个字节

手写题

(1) 手写三层架构

(2) 手写冒泡排序

  1. int [] array = new int [*] ;
  2. int temp = 0 ;
  3. for (int i = 0 ; i < array.Length - 1 ; i++)
  4. {
  5. for (int j = i + 1 ; j < array.Length ; j++)
  6. {
  7. if (array[j] < array[i])
  8. {
  9. temp = array[i] ;
  10. array[i] = array[j] ;
  11. array[j] = temp ;
  12. }
  13. }
  14. }

(3) 手写AJAX:XMLHttpRequest

(4) 手写增删改查、SQLHelper

(5) 编写一个单例(Singleton)类。

把构造函数设置为private,设置一个public、static的对象实例

  1. public FileManager
  2. {
  3. private FileManager(){}
  4. public readonly static FileManager Instance = new FileManager();
  5. }

扩展:搜“C# Singleton”,有线程安全的更牛B的实现

(6) C#实现代码编程题:定义一个int数组,长度为100,并向其中随机插入1-100的数字,保证不重复?

  1. int[] arr = new int[100];
  2. List<int> list = new List<int> ();
  3. for (int i = 0; i < arr.Length; i++) {
  4. list.Add (i + 1);
  5. }
  6. Random rd = new Random ();
  7. int Index = 0;
  8. while (list.Count > 0) {
  9. int num = rd.Next (0, list.Count);
  10. int temp = list [num];
  11. arr [Index] = temp;
  12. Index++;
  13. list.Remove (temp);
  14. }

(7) C#实现代码编程题:编写一个冒泡排序的方法,参数传入一个int数组?

  1. public static int[] Sort(int[] n1)
  2. {
  3. int temp;
  4. for (int i = 0; i < n1.Length - 1; i++)
  5. {
  6. for (int j = 0; j < n1.Length - 1 - i; j++)
  7. {
  8. if (n1[j] > n1[j + 1])
  9. {
  10. temp = n1[j];
  11. n1[j] = n1[j + 1];
  12. n1[j + 1] = temp;
  13. }
  14. }
  15. }
  16. return n1;
  17. }

(8) C#代码编程题:1 1 2 3 5 8 13 21 34 55用递归算法编写一个方法,传入n第几位,得出第几位上的数值?

  1. public static int fn(int a)
  2. {
  3. if (a>2)
  4. {
  5. return fit(a - 1) + fit(a - 2);
  6. }
  7. else
  8. {
  9. return 1;
  10. }
  11. }

(9) C#实现代码编程题:

1-2+3-4+

……

+m(m为奇数)

输出结果?

  1. public static int sum(int shu)
  2. {int sum=0;
  3. for (int i = 1; i <= shu; i++) {
  4. if(i%2==0)
  5. {
  6. sum-=i
  7. }else{
  8. sum+=i
  9. }
  10. }}