- 实例分析
- (1) 在下面的例子里
- (2) 分析以下代码
- (3) SQLSERVER服务器中
- (4) 公司要求开发一个继承System.Windows.Forms.ListView类的组件
- (5) 写出一条Sql语句:
- (6) 一列数的规则如下: 1、1、2、3、5、8、13、21、34… 求第30位数是多少,用递归算法实现。
- (7) 求以下表达式的值,写出您想到的一种或几种实现方法:1-2+3-4+……+m
- (8) shorts1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
- (9) 需要实现对一个字符串的处理,首先将该字符串首尾的空格去掉,如果字符串中间还有连续空格的话,仅保留一个空格,即允许字符串中间有多个空格,但连续的空格数不可超过一个.
- (10) 某一密码仅使用K、L、M、N、O共5个字母,密码中的单词从左向右排列,密码单词必须遵循如下规则:
- (11) 写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
- (11) 给定以下XML文件,完成算法流程图。
- (12) 请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
- (13) 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
- (14) a=10,b=15,在不用第三方变量的前提下,把a,b的值互换
- (15) 还有变态要求,需要代码最短呢。有两个结果:
- 手写题
实例分析
(1) 在下面的例子里
using System;
class A
{
public A()
{
PrintFields();
}
public virtual void PrintFields(){}
}
class B:A
{
int x=1;
int y;
public B()
{
y=-1;
}
public override void PrintFields()
{
Console.WriteLine(“x={0},y={1}”,x,y);
}
当使用new B()创建B的实例时,产生什么输出?
答:X=1,Y=0;x= 1 y = -1
(2) 分析以下代码
stringstrTmp = “abcdefg某某某”;
inti= System.Text.Encoding.Default.GetBytes(strTmp).Length;
intj= strTmp.Length;
以上代码执行完后,i= j=
答:i=13,j=10
(3) SQLSERVER服务器中
给定表table1 中有两个字段 ID、LastUpdateDate,
ID表示更新的事务号,LastUpdateDate表示更新时的服务器时间,请使用一句
SQL语句获得最后更新的事务号
答:
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位数是多少,用递归算法实现。
public class MainClass
{
public static void Main()
{
Console.WriteLine(Foo(30));
}
public static int Foo(int i)
{
if (i <= 0)
return 0;
else if(i > 0 && i <= 2)
return 1;
else return Foo(i -1) + Foo(i - 2);
}
}
(7) 求以下表达式的值,写出您想到的一种或几种实现方法:1-2+3-4+……+m
int Num = this.TextBox1.Text.ToString() ;
int Sum = 0 ;
for (int i = 0 ; i < Num + 1 ; i++)
{
if((i%2) == 1)
{
Sum += i ;
}
else
{
Sum = Sum - I ;
}
}
System.Console.WriteLine(Sum.ToString());
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) 需要实现对一个字符串的处理,首先将该字符串首尾的空格去掉,如果字符串中间还有连续空格的话,仅保留一个空格,即允许字符串中间有多个空格,但连续的空格数不可超过一个.
string inputStr=" xx xx “;
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:
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:
select top 10 * from A where id > (select max(id) from (select top 30 id from A )as A)
解答3:
用ROW_NUMBER实现
(11) 给定以下XML文件,完成算法流程图。
<FileSystem>
< DriverC >
<Dir DirName=”MSDOS622”>
<File FileName =” Command.com”></File>
</Dir>
<File FileName =”MSDOS.SYS” ></File>
<File FileName =” IO.SYS”></File>
</DriverC>
</FileSystem>
请画出遍历所有文件名(FileName)的流程图(请使用递归算法)。
答:
//方法一
voidFindFile( Directory d )
{
FileOrFolders = d.GetFileOrFolders();
foreach( FileOrFolder fof in FileOrFolders)
{
if(fof is File)
You Found a file;
else if (fof is Directory)
FindFile( fof );
}
}
//方法二
Public void DomDepthFirst(XmlNode currentNode)
{
XmlNode node=currentNode.FirstChild;
while(node!=null)
{
if(node.Name=="File")
{
Console.Write(((XmlElement)node).GetAttribute("FileName")+"\r\n");
}
DomDepthFirst(node);
node=node.NextSibling;
}
}
(12) 请编程遍历页面上所有TextBox控件并给它赋值为string.Empty?
答:
foreach(System.Windows.Forms.Control control in this.Controls)
{
if (control is System.Windows.Forms.TextBox)
{
System.Windows.Forms.TextBox tb =(System.Windows.Forms.TextBox)control ;
tb.Text = String.Empty ;
}
}
(13) 根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)
public void test(int i)
{
lock(this)
{
if (i>10)
{
i--;
test(i);
}
}
}
(14) a=10,b=15,在不用第三方变量的前提下,把a,b的值互换
a=a+b;
b=a-b;
a=a-b;
(15) 还有变态要求,需要代码最短呢。有两个结果:
1) a^=b^(b^=a^b); // 13个字节 2) a=b+(b=a)*0; // 11个字节手写题
(1) 手写三层架构
(2) 手写冒泡排序
int [] array = new int [*] ;
int temp = 0 ;
for (int i = 0 ; i < array.Length - 1 ; i++)
{
for (int j = i + 1 ; j < array.Length ; j++)
{
if (array[j] < array[i])
{
temp = array[i] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
}
(3) 手写AJAX:XMLHttpRequest
(4) 手写增删改查、SQLHelper
(5) 编写一个单例(Singleton)类。
把构造函数设置为private,设置一个public、static的对象实例
public FileManager
{
private FileManager(){}
public readonly static FileManager Instance = new FileManager();
}
扩展:搜“C# Singleton”,有线程安全的更牛B的实现
(6) C#实现代码编程题:定义一个int数组,长度为100,并向其中随机插入1-100的数字,保证不重复?
int[] arr = new int[100];
List<int> list = new List<int> ();
for (int i = 0; i < arr.Length; i++) {
list.Add (i + 1);
}
Random rd = new Random ();
int Index = 0;
while (list.Count > 0) {
int num = rd.Next (0, list.Count);
int temp = list [num];
arr [Index] = temp;
Index++;
list.Remove (temp);
}
(7) C#实现代码编程题:编写一个冒泡排序的方法,参数传入一个int数组?
public static int[] Sort(int[] n1)
{
int temp;
for (int i = 0; i < n1.Length - 1; i++)
{
for (int j = 0; j < n1.Length - 1 - i; j++)
{
if (n1[j] > n1[j + 1])
{
temp = n1[j];
n1[j] = n1[j + 1];
n1[j + 1] = temp;
}
}
}
return n1;
}
(8) C#代码编程题:1 1 2 3 5 8 13 21 34 55用递归算法编写一个方法,传入n第几位,得出第几位上的数值?
public static int fn(int a)
{
if (a>2)
{
return fit(a - 1) + fit(a - 2);
}
else
{
return 1;
}
}
(9) C#实现代码编程题:
1-2+3-4+
……
+m(m为奇数)
输出结果?
public static int sum(int shu)
{int sum=0;
for (int i = 1; i <= shu; i++) {
if(i%2==0)
{
sum-=i
}else{
sum+=i
}
}}