首页 > 代码库 > 北航软院2012级C#期末考试部分考题解答
北航软院2012级C#期末考试部分考题解答
博主注:本渣渣水平有限,文中若有不对的地方敬请指出,谢谢。
一、选择题(2*15=30)
1.In C# what is base class of all reference types?
System.Type B. System.Base C.System.Object D.System.Root
解答:所有引用类型的基类为System.Object
引用老师PPT:
(枚举是值类型,放在这里只是作为扩展)
2.Wrong statement?
A.double a = 123E; B. long b = 0xFeel; C. string c = “C#”; D.int d = 2014;
解答:英文字体换成Times New Roman就分得清1和l还有I了(可能是题目问题不纠结,这里的A错误太明显了吧)
3.Choose the output of the code below:
double a = 10, b = 10;
++a;
Console.WriteLine(a>b?(2>>1):(1<<2));
A.Conpile error B. 1 C. 2 D. 4
解答:a=11>b=10,2>>1=1
7.Which of the following keyword is used when a virtual is refined by a derived class?
A: base B:virtual C:overload D:override
解答:子类中的方法与父类中的方法完全一致,叫做重写(override);同一个类中方法名相同参数个数不同or类型不同or次序不同,叫做重载(overload)。
8.Which of the following are correct ways to pass a parameter to a attribute?
1) By value 2)by reference 3)by position 4)by name
A 1 ,2 B 3,4 C 1,2,3,4 D1,2,3
解答:老师的PPT上写的,有点看不懂。
9 if a method is marked as protected who can access it?
A classes in the declaring assembly
B only methods that are in the same class
C classed derived from the declaring class
D anywhere the application has a reference to an object of that base class
解答:仔细看PPT:)。
10.If you want to select all the numbers which are greeter than 1000 in an int array, which of the following can you use?
A.LINQ To Objects
B.LINQ To XML
C.LINQ To SQL
D.None of above
解答:LINQ查询:LINQ to Object, LINQ to XML, LINQ to ADO.NET 包括两种独立的技术: LINQ to DataSet 和 LINQ to SQL
对于LINQ to Object可以查询数组等里面的数据;
对于LINQ to SQL可以查询SQL能够查询的表中的数据;
对于LINQ to XML可以查询XML文档的标签等;
对于LINQ to DataSet可以查询DataSet中的数据;
11.The statement f = delegate(int x){return x + 1;}; is equivalent to which of the following statements?
A.f(x) =>x + 1;
B.f(x) = x + 1;
C.f => x + 1;
D.f = x => x + 1;
解答:考的是委托的形式(老师PPT的搬运工)
这里是一段测试代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Lambda{ class Program { static void Main(string[] args) { int sum = 0; Func<int, int> f1 = x => 2 * x + 1; Func<int, int, bool> f2 = (x, y) => x > y; Func<string, int, string> f3 = (x, y) => x.Substring(y); Func<int, int> f4 = (x) => sum += x; Action a1 = () => { System.Console.WriteLine("HelloWorld"); }; Action<int> a2 = (x) => { System.Console.WriteLine(x); }; Action<bool> a3 = (x) => { System.Console.WriteLine(x); }; Action<string> a4 = (x) => { System.Console.WriteLine(x); }; for (int i = 1; i <= 10; i++) { f4(i); } a2(sum); a1(); a2(f1(1)); a3(f2(3, 5)); a4(f3("AlvinZH", 5)); Console.ReadKey(); } }}
运行结果:
12.In C#, we can use the following statement to convert a string s to an integer num
1)int num = Convert.ToInt32(s);
2)int nym = Int32.Parse(s);
3)int num = s.ToInt();
4)int num = int.Parse(s);
A.1,2,3,4 B.1,2 C.1,3,4 D.2,3,4
解答:本题没有答案好像。
二、判断题(2*10=20)
1.In a switch statement, every statement sequence in a case clause must be terminated with break(or return, goto, throw).
解答:(T)(搬运工又来了)
2.Property 可声明在 class, struct, interface里。
解答:(T)
简单测试一下,反正就是可以有:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Fraction{ public struct StructTest { private int x; public int X { get { return x; } set { x = value; } } public StructTest(int _x) { x = _x; } } class Program { static void Main(string[] args) { StructTest str = new StructTest(); str.X = 1; Console.WriteLine(str.X); string s = "123"; int num1 = Convert.ToInt32(s); int num2 = Int32.Parse(s); //int num3 = s.ToInt(); int num4 = int.Parse(s); Console.WriteLine("num1="+num1); Console.WriteLine("num2="+num2); //Console.WriteLine(num3); Console.WriteLine("num4="+num4); Console.ReadKey(); } }}
运行结果:
PS:使用属性的好处:允许只读或者只写字段;可以在访问时验证字段;接口和实现的数据可以不同;替换接口中的数据。
5.if a constructor was declared, no default constructor is generated
解答:这个问题要看对象是谁了,对于类:只有在你没有构造器的时候编译器才会给你一个默认的构造器,你可以自定义一个无参构造器,当你自己写了一个构造器(无参或有参),编译器就不管你了;对于结构体:它总是会有一个默认的构造器,你也不能声明一个无参的构造器。
6.nested types cannot be interfaces and delegates
解答:(F)内嵌类型可以是类、结构、结构体、枚举、委托
7.Only the class that declares the event can fire it.
解答:(T)PPT上好详细啊,大家认真看喽!
8.In C#, we can only throw an exception by an invalid operation.
解答:(F)可以是非法操作,也可以是人为抛出异常。
三、填空题(2*7 + 3*6 = 32)
1.A constructor may call another constructor with the keyword__this___.(这个不用讲了吧)
2.In C#, if you want to change a thread’s state from suspended into running, you should call method__Resume()____.(详见PPT207页)
3.A(n)____static__ variable represents classwide information that is shared by all the objects of the class.(静态变量)
5.In C#,__indexer__is used to retrieve specific element in an enumerable object directly.(索引器检索)
6.In C#,__assembly is a set of types that are compiled together and it is the smallest unit for development and dynamic loading.(PPT181页)
7.The following class is often called___Generic____ Class.(通用类,PPT254页)
Class Buffer<Element>{
private Element[]data;
public Buffer(int size){…}
public void Put(Element x){…}
public Element Get(){…}
}
8.Suppose you have a double array called numbers ,please sum all the elements of the array using foreach statement
double sum = 0;foreach( double x in numbers){ sum = sum + x;}
11.What is the output of the following program?
using System;class Plant{ public virtual void ShowName() { Console.WriteLine("Food"); }}class Fruit : Plant{ public override void ShowName() { Console.WriteLine("Fruit"); }}class Apple : Fruit{ public new virtual void ShowName() { Console.WriteLine("Apple"); }}class LittleApple : Apple{ public override void ShowName() { Console.WriteLine("LittleApple"); }}class Test{ public static void Main() { Plant plant = new LittleApple(); plant.ShowName(); Apple apple = new LittleApple(); apple.ShowName(); Console.ReadKey(); }}
解答:代码里面的Plant.ShowName();应该是plant.ShowName();,Apple.ShowName();应该是apple.ShowName();
代码考的是virtual与new的用法。我的理解是new会把你的一路继承生生打断,重新开始,就酱:)
运行结果:
12.What is the output of the following program?
using System;class Fraction{ int x, y; public Fraction(int x, int y) { this.x = x; this.y = y; } public override string ToString() { return x + "/" +y; } public override int GetHashCode() { return x ^ y; } public override bool Equals(Object o) { Fraction f = (Fraction)o; return f.x == x && f.y == y; } public Fraction ShallowCopy() { return (Fraction)MemberwiseClone(); } class Client { static void Main() { Fraction a = new Fraction(1, 2); Fraction b = new Fraction(1, 2); Fraction c = new Fraction(3, 4); Console.WriteLine(a.Equals(b)); Console.WriteLine(a == b); a = c.ShallowCopy(); Console.WriteLine(a); Console.ReadKey(); } }}
解答:这道题考的是equals函数和==的区别,这里给大家贴一个讲得很好的帖子:http://www.cnblogs.com/chen0720/p/3209398.html
运行结果:
13.Please fill in the blanks.
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)) { //Please write code to call OnNew events if (OnNew != null) OnNew(); } } }}
解答:这道题什么个鬼意思呢?不太懂,大概是考一下event的用法吧,不仔细看会发现你读不懂这段代码:)
event介绍在PPT164~166页,大家仔细看哦~
四、简单题
1.What is the difference between Overloading and Overriding?
解答:(截图妙不可言qwq)
3. Give brief description about the differences and connections between classes and interfaces。
Class | Interface |
|
|
|
|
|
|
|
|
|
|
|
|
解答:继续老师PPT放送!!!咦,老师PPT里面没有这个对比。。。(Interface介绍在PPT152~157页)
大家加油呀,祝明天考试顺利~
作者: AlvinZH
出处: http://www.cnblogs.com/AlvinZH/
本人Github:https://github.com/Pacsiy/JobDu
本文版权归作者AlvinZH和博客园所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
北航软院2012级C#期末考试部分考题解答