2011最新asp.net面试题选择题和上机2

根据委托(delegate)的知识,请完成以下用户控件中代码片段的填写:
namespace test

{

    public delegate void OnDBOperate();

    public class UserControlBase : System.Windows.Forms.UserControl

    {

        public event OnDBOperate OnNew;

        private void toolBar_ButtonClick(object sender, System.Windows.Forms.

                                                        ToolBarButtonClickEventArgs e)

        {

            if (e.Button.Equals(BtnNew))

            {

                //请在以下补齐代码用来调用OnDBOperate委托签名的OnNew事件。

            }

        }

    }

}

答:if( OnNew != null ) 

    OnNew( this, e );

分析以下代码,计算运行结果
string strTmp = "abcdefg某某某";

int i= System.Text.Encoding.Default.GetBytes(strTmp).Length;

int j= strTmp.Length;

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

分析以下代码,计算运行结果
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,new B()时,首先运行父类的构造方法,构造方法里运行PrintFields()方法,此方法被B类重新,按B类的方法来运行。

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

class MainClass

{

    static void Main(string[] args)

    {

        Console.WriteLine("请输入数字:");

        int Num = int.Parse(Console.ReadLine().ToString());

        int Sum = 0;

        for (int i = 0; i < Num + 1; i++)

        {

            if ((i % 2) == 1)

            {

                Sum += i;

            }

            else

            {

                Sum -= i;

            }

        }

        System.Console.WriteLine(Sum.ToString());

        System.Console.ReadLine();

    }

}

结果:

请输入数字:

5

3

产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
using System;

using System.Collections;

 

class MainClass

{

    static void Main(string[] args)

    {

        int[] intArr = new int[100];

        ArrayList myList = new ArrayList();

        Random rd = new Random();

        int x = 0;

        while (myList.Count < 100)

        {

            x++;

            int num = rd.Next(1, 101);

            if (!myList.Contains(num))

            {

                myList.Add(num);

            }

        }

        for (int i = 0; i < 100; i++)

        {

            intArr[i] = (int)myList[i];

            Console.Write(intArr[i] + " ");

        }

        Console.WriteLine();

    }

}

13 11 2 52 74 7 56 42 69 20 58 84 15 19 87 78 53 36 5 16 34 54 82 22 55 76 61 79

 23 8 98 71 44 45 41 26 100 83 86 65 10 1 60 30 81 46 50 43 17 48 62 91 64 35 72

 21 6 75 14 9 92 12 96 49 80 33 37 39 93 18 88 63 38 70 28 27 40 95 3 24 97 25 8

5 29 66 68 73 31 59 90 4 57 99 94 89 51 67 77 47 32

请按任意键继续. . .

程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。
       要求:1.要有联动性,老鼠和主人的行为是被动的。 2.考虑可扩展性,猫的叫声可能引起其他联动效应。

using System;

public sealed class Cat

{

    //猫叫时引发的事件

    public event EventHandler Calling;

 

    public void Call()

    {

        Console.WriteLine("猫叫了...");

        if (Calling != null) //检查是否有事件注册

            Calling(this, EventArgs.Empty); //调用事件注册的方法.

    }

}

//老鼠,提供一个方法表示逃跑

public sealed class Mouse

{

    public void Escape(object sender, EventArgs e)

    {

        Console.WriteLine("老鼠逃跑了...");

    }

}

 

//主人,发生猫叫的时候惊醒

public sealed class Master

{

    public void Wakened(object sender, EventArgs e)

    {

        Console.WriteLine("主人惊醒了...");

    }

}

//用于测试的执行方法

//程序入口点

public class Program

{

    public static void Main(string[] args)

    {

        //建立猫

        Cat cat = new Cat();

        //建立老鼠

        Mouse mouse = new Mouse();

        //建立主人

        Master master = new Master();

 

        //注册事件

        cat.Calling += new EventHandler(mouse.Escape);

        cat.Calling += new EventHandler(master.Wakened);

 

        //猫开始叫

        cat.Call();

    }

}

结果:

猫叫了...

老鼠逃跑了...

主人惊醒了...

请按任意键继续. . .

 

以下采用观察者模式

//要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法.2. 对老鼠和主人进行抽象

//评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行(2分)

//<2>从Mouse和Master中提取抽象(5分)

//<3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒.(3分)

using System;

using System.Collections;

public interface Observer

{

    //观察者的响应,如是老鼠见到猫的反映

    void Response();

}

public interface Subject

{

    //针对哪些观察者,这里指猫的要扑捉的对象---老鼠

    void AimAt(Observer obs);

}

public class Mouse : Observer

{

    private string name;

    public Mouse(string name, Subject subj)

    {

        this.name = name;

        subj.AimAt(this);

    }

 

    public void Response()

    {

        Console.WriteLine(name + " attempt to escape!");

    }

}

public class Master : Observer

{

    public Master(Subject subj)

    {

        subj.AimAt(this);

    }

 

    public void Response()

    {

        Console.WriteLine("Host waken!");

    }

}

 

public class Cat : Subject

{

    private ArrayList observers;

    public Cat()

    {

        this.observers = new ArrayList();

    }

    public void AimAt(Observer obs)

    {

        this.observers.Add(obs);

    }

    public void Cry()

    {

        Console.WriteLine("Cat cryed!");

        foreach (Observer obs in this.observers)

        {

            obs.Response();

        }

    }

}

class MainClass

{

    static void Main(string[] args)

    {

        Cat cat = new Cat();

        Mouse mouse1 = new Mouse("mouse1", cat);

        Mouse mouse2 = new Mouse("mouse2", cat);

        Master master = new Master(cat);

        cat.Cry();

    }

}

Cat cryed!

mouse1 attempt to escape!

mouse2 attempt to escape!

Host waken!

请按任意键继续. . .

写出程序的输出结果
using System;

class Program

{

    private string str = "Class1.str";

    private int i = 0;

    static void StringConvert(string str)

    {

        str = "A string being converted.";

    }

    static void StringConvert(Program c)

    {

        c.str = "B string being converted.";

    }

    static void Add(int i)

    {

        i++;

    }

    static void AddWithRef(ref int i)

    {

        i++;

    }

    static void Main()

    {

        int i1 = 10;

        int i2 = 20;

        string str = "str";

        Program c = new Program();

        Add(i1);

        AddWithRef(ref i2);

        Add(c.i);

        StringConvert(str); //string无法被改变           

        StringConvert(c);   //传递对象string可以被改变

 

        Console.WriteLine(i1);

        Console.WriteLine(i2);

        Console.WriteLine(c.i);

        Console.WriteLine(str);

        Console.WriteLine(c.str);

    }

}

运行结果:

10

21

0

str

B string being converted.

请按任意键继续. . .

写出程序的输出结果
using System;

public abstract class A

{

    public A()

    {

        Console.WriteLine('A');

    }

    public virtual void Fun()

    {

        Console.WriteLine("A.Fun()");

    }

}

public class B : A

{

    public B()

    {

        Console.WriteLine('B');

    }

    public new void Fun()

    {

        Console.WriteLine("B.Fun()");

    }

    public static void Main()

    {

        A a = new B();

        a.Fun();

    }

}

 

运行结果:

A

B

A.Fun()

请按任意键继续. . .

有继承关系,在构造子类的时候会先构造父类;Fun函数在子类是被new了一下,只是显示隐藏,并没有重写,所以输出还是A.Fun()。如果子类是被override,则最后输出B.Fun

写出程序的输出结果
using System;

public class A

{

    public virtual void Fun1(int i)

    {

        Console.WriteLine(i);

    }

    public void Fun2(A a)

    {

        a.Fun1(1);  //以传递进来的对象选择哪个Fun1

        Fun1(5);    //以调用此方法的对象选择哪个Fun1

    }

}

public class B : A

{

    public override void Fun1(int i)

    {

        base.Fun1(i + 1);

    }

    public static void Main()

    {

        B b = new B();

        A a = new A();

        a.Fun2(b);

        b.Fun2(a);

    }

}

输出结果:

2

5

1

6

请按任意键继续. . .

冒泡排序
using System;

public class MainClass

{

    public static void ArraySort()

    {

        int[] Arr = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

        int t = 0;

        //从小到大排序

        for (int i = 0; i < Arr.Length - 1; i++)            //总共需要N-1趟

        {

            for (int j = 0; j < Arr.Length - 1 - i; j++)    //每趟总共需要(N-1-趟数)次比较

            {

                if (Arr[j] > Arr[j + 1])

                {

                    t = Arr[j];

                    Arr[j] = Arr[j + 1];

                    Arr[j + 1] = t;

 

                }

            }

        }

        foreach (int i in Arr)

        {

            Console.Write(i + ",");

        }

    }

    public static void Main()

    {

        ArraySort();

    }

}

输出:1,2,3,4,5,6,7,8,9,请按任意键继续. . .

写出程序的输出结果
using System;

class A

{

    public static int X;

    static A()

    {

        X = B.Y + 1;

    }

}

class B

{

    public static int Y = A.X + 1;

    static B() { }

    static void Main()

    {

        Console.WriteLine("X={0},Y={1}", A.X, B.Y);

    }

}

输出结果:

X=1,Y=2

请按任意键继续. . .

在.net(C# or vb.net)中如何用户自定义消息,并在窗体中处理这些消息。
在form中重载DefWndProc函数来处理消息:

protected override void DefWndProc(ref System.WinForms.Message m)

{

    switch (m.msg)

    {

        case WM_Lbutton:

            ///string与MFC中的CString的Format函数的使用方法有所不同

            string message = string.Format("收到消息!参数为:{0},{1}", m.wParam, m.lParam);

            MessageBox.Show(message);///显示一个消息框

            break;

        case USER:

        //处理的代码

        default:

            base.DefWndProc(ref m);///调用基类函数处理非自定义消息。

            break;

    }

}

请说明如下代码的结果

using System;

class Father

{

    public virtual void show()

    {

        Console.WriteLine("Father");

    }

    static void Main(string[] args)

    {

        Father obj = new Child();

        obj.show();

    }

}

class Child : Father

{

    public override void show()

    {

        Console.WriteLine("Child");

    }

}

输出:Child请按任意键继续. . .如果把virtual和override去掉,输出结果为Father

posted @ 2012-02-22 20:40  冰灵的风儿  阅读(876)  评论(0编辑  收藏  举报