




已阅读5页,还剩46页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#习题集(2015 年) 1、选择题 注:答案中“(ch3.1 ) ”指示了题目来源的章节,对本题而言,来自于教材 3.1 节 1、在.Net 中,关于 CLR 和 IL,以处描述正确的是() 。 A.应用程序在 CLR 环境被编译成 IL,IL 将能够被任何计算机指令性集成 B.应用程序被编译两次,第一次生成 IL,IL 在运行时候被 CLR 快速编译 C.应用程序被编译两次,但是第二次 CLR 编辑比第一次慢 D.借取 CLR,IL 被编译成机器代码并且能够在不同的计算机指令集下被执行 答案:B(ch1.2 ) 2、CLR 为.NET 提供以下方面的功能或者服务除了() 。 A.无用存储单元收集 B.代码验证和类型安全 C.代码访问安全 D.自动消除程序中的逻辑错误 答案:D(ch1.2) 3、CLS 是一种() A程序设计语言 B. 运行环境 C.开发环境 D.API 编程接口 答案:B(ch1.2) 4、C#中导入某一命名空间的关键字是() Ausing B.use C.import D. include 5、C#中程序的入口方法名是() A.Main B.main C.Begin D.using 6、下列选项中,( )是引用类型。 A.enum 类型 B.struct 类型 C.string 类型 D.int 类型 答案:C (ch2.1) 7、C# 的数据类型有( ) A.值类型和调用类型; B.值类型和引用类型; C.引用类型和关系类型; D.关系类型和调用类型; 答案:B (ch2.1) 8、当整数 a 赋值给一个 object 对象时,整数 a 将会被(C )。 A)拆箱 B)丢失 C) 装箱 D)出错 答案:C(ch2.1) 9、装箱、拆箱操作发生在: ( ) A.类与对象之间 B.对象与对象之间 C.引用类型与值类型之间 D.引用类型与引用类型之间 正确答案:C(ch2.1) 10、在 C#中,下列代码运行后,变量 Max 的值是(C) (选择一项)Int a=5,b=10,c=15,Max=0; Max = ab?a:b; Max = c和=和 max) max = ai; if (ai y) tmp = x; x = y; y = tmp; if (x z) tmp = x; x = z; z = tmp; if (y z) tmp = y; y = z; z = tmp; class Program static void Main(string args) Myclass m = new Myclass(); int a, b, c; a = 30; b = 20; c = 10; m.Sort(ref a, ref b, ref c); Console.WriteLine(“a=0, b=1, c=2“, a, b, c); Console.Read(); 答:a=10,b=20,c=30 (ch3.3) 2、下面程序的运行结果是: 。 using System; class Myclass public void Sort(int x, int y, int z) int tmp; / tmp 是方法 Sort 的局部变量 / 将 x, y, z 按从小到大排序 if (x y) tmp = x; x = y; y = tmp; if (x z) tmp = x; x = z; z = tmp; if (y z) tmp = y; y = z; z = tmp; class Test static void Main(string args) Myclass m = new Myclass(); int a, b, c; a = 30; b = 20; c = 10; m.Sort(a, b, c); Console.WriteLine(“a=0, b=1, c=2“, a, b, c); Console.Read(); 答:a=30,b=20,c=10 (ch3.3) 3、下面程序的运行结果是: 。 using System; class Myclass public void MaxMin(out int max, out int min, params int a) if (a.Length = 0) / 如果可变参数为零个,可以取一个约定值或产生异常 max = min = -1; return; max = min = a0; for (int i = 1; i max) max = ai; if (ai 0) string str = (string)s.Pop(); Console.WriteLine(“Poping: 0“, str); Console.WriteLine(“Done“); 答:(ch4.5)-注意该题与下面题本质是一样,区别是进出栈的数据类型不一样 Poping: How Poping: Is Poping: This Poping: So Done 14、下面程序的运行结果是: 。 using System; using System.Collections; class StackExample static void Main() Stack s = new Stack(); s.Push(90); s.Push(80); s.Push(70); s.Push(60); while (s.Count 0) int a = (int)s.Pop(); Console.WriteLine(“Poping: 0“, a); Console.WriteLine(“Done“); 答:(ch4.5) Poping: 60 Poping: 70 Poping: 80 Poping: 90 Done 15、下面程序的运行结果是: 。 using System; class MyClass private string data = new string4; public string thisint index get return dataindex; set dataindex = value; class MyClient public static void Main() MyClass mc = new MyClass(); mc0 = “ab“; mc1 = “cd“; mc2 = “ef“; mc3 = “gh“; Console.WriteLine(“0, 1, 2, 3“, mc0, mc1, mc2, mc3); 答:(ch4.5) ab, cd, ef, gh 16、下面程序的运行结果是: 。 using System; class WithFinally public static void Main() try int x = 5; int y = 0; int z = x / y; Console.WriteLine(z); catch (DivideByZeroException) Console.WriteLine(“Error occurred, unable to compute“); finally Console.WriteLine(“Thank you for using the program“); 答:(ch4.6) Error occurred, unable to compute Thank you for using the program 17、下面程序的运行结果是: 。 using System; class ThrowExample public void Div() try int x = 5; int y = 0; int z = x / y; Console.WriteLine(z); catch (DivideByZeroException e) throw new ArithmeticException(“被除数为零“, e); public static void Main() try ThrowExample ThrowException = new ThrowExample(); ThrowException.Div(); catch (Exception e) Console.WriteLine(“Exception: 0“, e.Message); 答:(ch4.6) Exception: 被除数为零 18、下面程序的运行结果是: 。 using System; class ThrowExample public void Div() try int x = 5; int y = 0; int z = x / y; Console.WriteLine(z); catch (DivideByZeroException) throw; public static void Main() try ThrowExample throwexample = new ThrowExample(); throwexample.Div(); catch (DivideByZeroException e) Console.WriteLine(“Exception: 0“, e.Message); 答:(ch4.6) Exception: 尝试除以零 19、下面程序的运行结果是: 。 using System; class SimpleClass public class WorkerClass public int IM(int nID, string sName) int retval = 0; retval = nID * sName.Length; Console.WriteLine(“调用 IM 方法“); return retval; static public int SM(int nID, string sName) int retval = 0; retval = nID * sName.Length; Console.WriteLine(“调用 SM 方法“); return retval; public delegate int SomeDelegate(int nID, string sName); static void Main(string args) WorkerClass wr = new WorkerClass(); SomeDelegate d1 = new SomeDelegate(wr.IM); Console.WriteLine(“d1=0“, d1(5, “ab“); SomeDelegate d2 = new SomeDelegate(WorkerClass.SM); Console.WriteLine(“d2=0“, d2(5, “abc“); Console.Read(); 答:(ch4.7) 调用 IM 方法 d1=10 调用 SM 方法 d2=15 20、下面程序的运行结果是: 。 using System; class SimpleClass public class WorkerClass public int IM(int nID, string sName) int retval = 0; retval = nID * sName.Length; Console.WriteLine(“调用 IM 方法“); return retval; static public int SM(int nID, string sName) int retval = 0; retval = nID * sName.Length; Console.WriteLine(“调用 SM 方法“); return retval; public delegate int SomeDelegate(int nID, string sName); static void Main(string args) WorkerClass wr = new WorkerClass(); SomeDelegate d1 = new SomeDelegate(wr.IM); SomeDelegate d2 = new SomeDelegate(WorkerClass.SM); SomeDelegate d3 = d1 + d2; Console.WriteLine(“d3=0 “, d3(5, “abcd“); Console.Read(); 答:(ch4.7) 调用 IM 方法 调用 SM 方法 d3=20 21、下面程序的运行结果是: 。 #define DEBUG using System; public class MyClass public static void Main() #if (DEBUG) Console.WriteLine(“ DEBUG is defined“); #else Console.WriteLine(“ DEBUG is not defined“); #endif 答:(ch4.8) DEBUG is defined 22、下面程序的运行结果是: 。 #define A #undef B using System; public class MyClass public static void Main() #if (A #elif (!A #elif (A #else Console.WriteLine(“A and B are not defined“); #endif 答:(ch4.8) A is defined 23、下面程序的运行结果是: 。 using System; class Point public double x, y; public Point() x = 0; y = 0; public Point(double x, double y) this.x = x; / 当 this 在实例构造函数中使用时, this.y = y; / 它的值就是对该构造的对象的引用。 class Test static void Main(string args) Point a = new Point(); Point b = new Point(3, 4); / 用构造函数初始化对象 Console.WriteLine(“a.x=0, a.y=1“, a.x, a.y); Console.WriteLine(“b.x=0, b.y=1“, b.x, b.y); Console.Read(); 答: a.x=0,a.y=0 b.x=3,b.y=4 24、写出下列程序段的运行结果。 class Program delegate void DualFunction(double x, double y); static void Main() DualFunction fun1 = null; DualFunction fun2 = new DualFunction(Sub); DualFunction fun3 = Add + fun2; fun3(2.5, 2); fun3 -= fun2; Console.WriteLine(fun1 = fun3); static void Add(double x, double y) Console.WriteLine(“0 + 1 = 2“, x, y, x + y); static void Sub(double x, double y) Console.WriteLine(“0 - 1 = 2“, x, y, x - y); 答案: 五、简答题 1、如何看待 C# 、CLR 和.NET 之间的关系? C#是一种编程语言,他是运行在 CLR 上的,CLR 提供了他的编译环境。 CLR,即公共语言运行时,CLR 提供了所有类型的应用程序都要使用的编程模型。包括文 件加载器,垃圾收集器,安全系统,线程池等。CLR 还提供了一个面向对象的编程模型, 定义了类型和对象是什么,以及他们的行为方式。 .NET 是个框架,即.NET Framework,它由 CLR 和 Framework 类库组成。其次,.NET 是一 个完整的开发平台。 2、简述.NET 应用程序的编译过程。 在.NET Framework 之上,先将高级语言(如 C#、VB)编译成为中间语言(IL),这些中间 语言是.NET 框架中所有的语言编译后的结果。 ,IL 经过再次编译形成机器码,完成 IL 到 机器码编译任务的是 JIT(Just In Time)编译器。 3、简述构建在 Windows 操作系统之上的是 CLS 的作用。 构建在 Windows 操作系统之上的是公共语言运行时,其作用是负责执行程序,提供内 存管理、线程管理、安全管理、异常处理、通用类型系统与生命周期监控等核心服务。 4、简述.NET FrameWork 的体系结构。 .NET FrameWork 的体系结构包括 5 大部分,分别为: 1) 程序设计语言及公共语言规范(CLS) 2) 应用程序平台(ASP.NET 及 Windows 应用程序等) 3) ADO.NET 及类库 4) 公共语言运行时(CLR) 5) 程序开发环境(Visual Studio) 5、接口和抽象类的区别是什么? 答:接口用于规范,抽象类用于共性。接口中只能声明方法,属性,事件,索引器,不能 包含字段、构造函数、析构函数、静态成员或常量。 而抽象类中可以有方法的实现,也可以定义非静态的类变量。 抽象类是类,所以只能被单继承,但是接口却可以一次实现多个。 抽象类的实例是它的子类给出的。接口的实例是实现接口的类给出的。 接口成员被定义为公共的,但抽象类的成员也可以是私有的、受保护的。 6、简述公有、私有、保护修饰符的访问限制规则。 公有:允许外部对象访问该成员。 私有:不允许外部对象访问该成员。 保护:只允许当前类及其派生类对象访问该成员,不允许其他对象共访问。 7、简述通过委托来调用对象方法的基本过程。 (1)定义委托原型,其签名应与要封装的方法保持一致。 (2)定义委托类型的变量。 (3)使用 new 关键字创建委托对象,并将要封装的方法名作为参数传递给构造函数。 (4)通过委托变量来调用方法。 5、简述派生类对象创建和销毁时所调用的构造函数、析构函数及次序。 派生类对象创建时将自顶向下调用各级基类的构造函数,最后调用自身的构造函数。 销毁时首先调用自身的析构函数,再自底向上调用各级基类的析构函数。 8、简述属性声明中包含哪些特殊方法,以及这些方法的参数和返回值情况。 属性中可以包含 get 和 set 方法。 get 方法无参数,默认返回值类型就是属性的类型。 set 默认返回类型为 void,隐含与属性类型相同的参数 value,表示要传递给属性的值。 9、简述值类型和引用类型的不同之处。 第一:值类型的变量直接包含自身的所有数据,引用类型的变量只存储对目标数据的引用。 第二:作为方法参数时,值类型变量传递的是数值,引用类型变量传递的是地址。 10、C#中的委托是什么?事件是不是一种委托? 答 : 委托可以把一个方法作为参数代入另一个方法。 委托可以理解为指向一个函数的引用。是一种特殊的委托 11、override 与重载的区别 答 :重载是方法的名称相同。参数或参数类型不同,进行多次重载以适应不同的需要 Override 是进行基类中函数的重写。为了适应需要。 六、编程题 1、利 用 委 托 编 写 一 个 类 , 类 中 有 MyShow()方 法 , 要 求 实 际 执 行 的 是 System.MessageBox.Show()方 法。 using System; using System.Windows.Forms; public delegate DialogResult MyShowDelegate(string s); public class MainClass public static void Main() /System.Windows.Forms.MessageBox.Show(“This is a program.“); MyShowDelegate msd = new MyShowDelegate(MessageBox.Show); MyClass mc = new MyClass(); mc.MyShow(msd); public class MyClass public DialogResult MyShow(MyShowDelegate msd) msd(“This is a program.“); return DialogResult.OK; 2、已知下面接口,试编写一个类实现该接口,并通过该接口进行编程,实现简单功能。 interface IShow int Data get; set; void Show(); 参考答案: using System; interface IShow int Data get; set; void Show(); public class MyClass:IShow int data; public int Data get return this.data; set this.data = value; public void Show() Console.WriteLine(“保存的数据为:0“,this.Data); public class MainClass public static void Main() IShow sh = new MyClass(); sh.Data = 30; sh.Show(); 3、定义有公有访问权限的 Dog 类,为该类添加下列字段和方法。 公有定段sex,代表雌雄,数据类型为bool。 公有字段age ,代表年龄,数据类型为int。 构造方法Dog(bool,int) ,设置为雌性,2岁。 无返回值方法Speak(),用于介绍动物: 当sex的值为true,方法返回一个字符串 “I am a male Dog!”; 当sex的值为false,方法返回一个字符串 “I am a female Dog!”; 另外再定义一个类 Test,包含有 Main()方法,Main()方法中定义 Animal 对象,执行 Speak() 方法。 参考答案: using System; public class Test public static void Main() Dog animal = new Dog(true,2); animal.Speak(); public class Dog public bool sex; public int age; public Dog(bool sex,int age) this.sex = sex; this.age = age; public void Speak() if(this.sex) Console.WriteLine(“I am a male Dog!“); else Console.WriteLine(“I am a femal Dog!“); 4、已知下面接口,试编写一个实现IIn接口的Square(正方形)类和一个实现IOut接口的 Circle(圆)类;初始化圆类对象,通过接口将圆类对象的半径输出,作为正方形的边长, 并通过接口传给正方形对象。 interface IIn void In(int size); void Show(); interface IOut int Out(); void Show(); 参考答案: using System; interface IIn void In(int size); void Show(); interface IOut int Out(); void Show(); public class Square:IIn int size; public Square() public Square(int size) this.size = size; public void In(int size) this.size = size; public void Show() Console.WriteLine(“正方形的边长为: 0“,this .size); public class Circle:IOut int radius; public Circle() public Circle(int radius) this.radius = radius; public int Out() return this.radius; public void Show() Console.WriteLine(“圆的半径为: 0“,this .radius); public class MainClass public static void Main() Circle ci = new Circle(8); ci.Show(); Square sq = new Square(); sq.Show(); sq.In(ci.Out(); sq.Show(); 5、已知下面接口,试编写一个实现了该接口的 Student 类,使该类只能通过接口编程。 interface IStudent string Name get; set; string age get; set; void Learn(); void Show(); 参考答案: 程序: using System; interface IStudent string Name get; set; string Age get; set; void Learn(); void Show(); public class Student:IStudent #region IStudent 成员 string name; string age; string IStudent.Name get return ; set = value; string IStudent.Age get return this.age; set this.age = value; void IStudent.Learn() Console.WriteLine(“0 study chinese.“,); void IStudent.Show() Console.WriteLine(“0 is 1 years old.“,,this.age); #endregion public class MainClass public static void Main() IStudent istu = new Student(); istu.Age = “20“; istu.Name = “Mike“; istu.Learn(); istu.Show(); /Student st = new Student(); /以下均出错,不能通过st访问 /st.Age = “18“; /st.Name = “Lucy“; /st.Learn(); /st.Show(); 6、定义一抽象类 Pet(宠物)类,作为基类。类中定义两个私有字段毛色和年龄,定义抽 象方法完成宠物的自我介绍功能;定义两个派生类 Cat 和 Dog,再覆写基类抽象方法;定 义包含主方法的类 MainClass,通过抽象类对象调用派生类的覆写方法。 参考答案: using System; public class MainClass public static void Main() Pet p = new Petnew Cat(“Tarme“,2,“black“,3),new Dog(“LittleTiger“,1,“Yellow“,5); for(int i=0;i(T array, T e) for (int i = 0; i (a, 2); index2 = Ts.find(b, 3.6); Console.WriteLine(“index1=0,index2=1“, index1, index2); Console.Read(); 11、设计一个 WinForm 应用程序,可以调节窗体的透明度。从工具箱中拖拽 2“Button”控 件到窗体上。设置窗体和控件的属性如下表 3 所示。 类 别 名 称 属 性 设 置 值 Form FormOpacity text 可调节透明度的窗体 Button BtnAdd text 增加透明度 BtnSub text 降低透明度 运行程序,调节窗体透明度前后如下图所示。 表中窗体与控件的名称是指窗体与控件的 name 属性值,在窗体设计器中分别双击两个按 钮,在代码编辑窗口中添加代码,请完成相应代码: private void BtnAdd_Click(object sender, EventArgs e) this.Opacity += 0.1; private void BtnSub_Click(object sender, EventArgs e) if (this.Opacity 0.2) this.Opacity -= 0.1; else this.Close(); /关闭程序 12、新建 WinForm 项目,从工具箱中拖拽 1 个“PictureBox”、1 个“Timer” 和 2 个“Button” 控 件到窗体上。设置窗体和控件的属性如下表所示。 类 别 名 称 属 性 设 置 值 PictureBox pictureBox1 Image 选择一张图片 SizeMode StretchImage Timer timer1 Interval 10 Button button1 Text 开始移动 button2 Text 暂停移动 运行程序,运行前后如下图所示。 请写出相应的事件代码: private void button1_Click(object sender, EventArgs e) timer1.Start(); / 开始移动 private void button2_Click(object sender, EventArgs e) timer1.Stop(); / 停止移动 private void timer1_Tick(object sender, EventArgs e) if(pictureBox1.Left=this.Width) pictureBox1.Left = -pictureBox1.Width; pictureBox1.Left += 1; 13、编程实现矩形面积的计算。操作界面如下图所示,其中有四个标签按钮 Label1、Label2、Label3 和 Label4;三个文本框 textBox1、textBox2 和 textBox3;一个按钮 (Name 属性为 btnArea) 。textBox1 文本框输入矩形的长, textBox2 文本框输入矩形的宽, textBox3 文本框显示运算结果。请分别实现 Rectangular 类中面积计算的代码和 btnArea 按 钮的单击事件代码。 /定义基类 Shape public class Shape public virtual double GetArea() return 0.0; / 派生类 Rectangular,从 Shape 类派生
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 环境友好加工技术分析报告
- 铁路客运企业竞争力战略研究分析报告
- 智能建筑仪表用户体验分析报告
- 2025年食品冷链物流温控技术在冷链食品冷链物流过程中的物流协同化报告
- 金融机构数字化转型中的信用风险监控与预警体系构建报告
- 工业互联网平台量子密钥分发技术产业生态构建2025年研究报告
- 冷链物流温控技术与冷链食品冷链物流标准化研究报告
- 电商直播行业2025年主播与品牌合作模式创新趋势及风险预警报告
- 城市污水处理厂智能化升级改造2025年大数据分析与决策支持报告
- 中医考试题目及答案
- 2025年道路运输两类人员安全员考核分享题库及答案
- 2025临时工合同协议书模板
- 中考英语688高频词大纲词频表
- 神的《全备之救》
- GB/T 19355.3-2016锌覆盖层钢铁结构防腐蚀的指南和建议第3部分:粉末渗锌
- GA 38-2021银行安全防范要求
- 年产1000吨速冻蔬菜工厂设计
- 【医院管理】-科研创新助推学科建设课件
- 新课标高考英语词汇表3500
- DBJ50T-065-2020 民用建筑外门窗应用技术标准
- 工资现金发放证明书
评论
0/150
提交评论