Monday, November 13, 2006

(ZT)C# Interview Basic Question

http://dflying.dflying.net/1/archive/104_c_basic_interview_questions.html

 

  1. What is the implicit name of the parameter passed into a property’s ’set’ method?
    ‘value’. And it’s data type depends on whatever variable we’re changing.
  2. How do you inherit from a class in C#? Place a colon and then the name of the base class.
  3. Does C# support multiple inheritances? No, use interfaces instead.
  4. When you inherit a protected class-level variable, who is it available to? Only classes that ultimately inherit from the class with a protected member can see that member.
  5. Are private class-level variables inherited? Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But they are.
  6. Describe the accessibility modifier protected internal. It’s available to derived classes and classes within the same Assembly (and naturally from the base class it’s declared in).
  7. C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write? Two. Once you write at least one constructor, C# cancels the freebie constructor, and now you have to write one yourself, even if there’s no implementation in it.
  8. What’s the top .NET class that everything is derived from? System.Object.
  9. How’s method overriding different from overloading? When overriding, you change the method behavior for a derived class. Overloading simply involves having a method with the same name within the class.
  10. What does the keyword virtual mean in the method definition? The method can be over-ridden.
  11. Can you declare the override method static while the original method is non-static? No, you can’t, the signature of the virtual method must remain the same, only the keyword virtual is changed to keyword override.
  12. Can you override private virtual methods? No, moreover, you cannot access private methods in inherited classes, have to be protected in the base class to allow any sort of access.
  13. Can you prevent your class from being inherited and becoming a base class for some other classes? Yes, that’s what keyword sealed in the class definition is for. The developer trying to derive from your class will get a message: cannot inherit from Sealed class WhateverBaseClassName. It’s the same concept as final class in Java.
  14. Can you allow class to be inherited, but prevent the method from being over-ridden? Yes, just leave the class public and make the method sealed.
  15. What’s an abstract class? A class that cannot be instantiated. A concept in C++ known as pure virtual method. A class that must be inherited and have the methods over-ridden. Although an abstract class does not require implementations (its methods can be abstract) it can also offer implementations of methods (either virtual or not) which can be called in implementing classes.
  16. When do you absolutely have to declare a class as abstract (as opposed to free-willed educated choice or decision based on UML diagram)? When at least one of the methods in the class is abstract. When the class itself is inherited from an abstract class, but not all base abstract methods have been over-ridden.
  17. What’s an interface class? It’s an abstract class with public abstract methods all of which must be implemented in the inherited classes.
  18. Why can’t you specify the accessibility modifier for methods inside the interface? They all must be public. Therefore, to prevent you from getting the false impression that you have any freedom of choice, you are not allowed to specify any accessibility, it’s public by default.
  19. Can you inherit multiple interfaces? Yes, why not.
  20. And if they have conflicting method names? It’s up to you to implement the method inside your own class, so implementation is left entirely up to you. This might cause a problem on a higher-level scale if similarly named methods from different interfaces expect different data, but as far as compiler cares you’re okay.
  21. What’s the difference between an interface and abstract class? In the interface all methods must be abstract. In the abstract class some methods can be concrete. In the interface no accessibility modifiers are allowed, which is ok in abstract classes.
  22. How can you overload a method? Different parameter data types, different number of parameters, different order of parameters.
  23. What is the difference between const and readonly?
    The const keyword is used for compile time constants while the readonly keyword is used for runtime constants.
  24. What’s the difference between System.String and System.StringBuilder classes? System.String is immutable while System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

 

C#面试基础问题

English Version: http://dflying.dflying.net/1/archive/104_c_basic_interview_questions.html

如果你的简历上面写“熟悉/了解C#”,那么你就应该能够回答下面的这些基础问题。我将给出自己的简要答案以供参考。欢迎讨论。
如果是“精通”,那么请参考:http://www.cnblogs.com/dflying/archive/2006/04/01/364458.html

  1. 传入某个属性的set方法的隐含参数的名称是什么?
    value,它的类型和属性所声名的类型相同。
  2. 如何在C#中实现继承?
    在类名后加上一个冒号,再加上基类的名称。
  3. C#支持多重继承么?
    不支持。可以用接口来实现。
  4. 被protected修饰的属性/方法在何处可以访问?
    在继承或间接继承与这个类的子类中可以访问。
  5. 私有成员会被继承么?
    会,但是不能被访问。所以看上去他们似乎是不能被继承的,但实际上确实被继承了。
  6. 请描述一下修饰符protected internal
    被protected internal修饰的属性/方法只能在它的在同一个程序集(Assembly)中的子类被访问。
  7. C#提供一个默认的无参数构造函数,当我实现了另外一个有一个参数的构造函数时候,还想保留这个无参数的构造函数。这样我应该写几个构造函数?
    两个,一旦你实现了一个构造函数,C#就不会再提供默认的构造函数了,所以需要手动实现那个无参数构造函数。
  8. C#中所有对象共同的基类是什么?
    System.Object.
  9. 重载和覆写有什么区别?
    重载提供了对一个方法签名的不同参数调用的实现。覆写提供了子类中改变父类方法行为的实现。
  10. 在方法定义中,virtual有什么含意?
    被virtual修饰的方法可以被子类覆写。
  11. 能够将非静态的方法覆写成静态方法么?
    不能,覆写方法的签名必须与被覆写方法的签名保持一致,除了将virtual改为override。
  12. 可以覆写私有的虚方法么?
    不可以,甚至子类中无法访问父类中的私有方法。
  13. 能够阻止某一个类被其他类继承么?
    可以,使用关键字sealed。
  14. 能够实现允许某个类被继承,但不允许其中的某个方法被覆写么?
    可以,标记这个类为public,并标记这个方法为sealed。
  15. 什么是抽象类(abstract class)?
    一种不可以被实例化的类。抽象类中一般含有抽象方法,当然也可有具体实现。继承类只有实现过所有抽象类的抽象方法后才能被实例化。
  16. 何时必须声明一个类为抽象类?
    当这个类中包含抽象方法时,或是该类并没有完全实现父类的抽象方法时。
  17. 接口(interface)是什么?
    只含有共有抽象方法(public abstract method)的类。这些方法必须在子类中被实现。
  18. 为什么不能指定接口中方法的修饰符?
    接口中的方法用来定义对象之间通信的契约,指定接口中的方法为私有或保护没有意义。他们默认为公有方法。
  19. 可以继承多个接口么?
    当然。
  20. 那么如果这些接口中有重复的方法名称呢?
    这种情况中你可以决定如何实现。当然需要特别得小心。但是在编译环节是没有问题的。
  21. 接口和抽象类的区别是什么?
    接口中所有方法必须是抽象的,并且不能指定方法的访问修饰符。抽象类中可以有方法的实现,也可以指定方法的访问修饰符。
  22. 如何区别重载方法?
    不同的参数类型,不同的参数个数,不同的参数顺序。
  23. const和readonly有什么区别?
    const关键字用来声明编译时常量,readonly用来声明运行时常量。
  24. System.String 和System.StringBuilder有什么区别?
    System.String是不可变的字符串。System.StringBuilder存放了一个可变的字符串,并提供一些对这个字符串修改的方法。

posted on 2006-04-01 20:36 Dflying Chen 阅读(4165) 评论(30) 编辑 收藏 引用 网摘 所属分类: .NETC#


评论:

# re: C#面试基础问题 2006-04-02 14:35 | Phoenix Huang

90%是面向对象设计的问题。  回复

# re: C#面试基础问题 2006-04-02 14:48 | Dflying Chen

@Phoenix Huang
是的,当然这些也考察了C#的语言基础。  回复

# re: C#面试基础问题 2006-04-02 17:15 | 林子

不错!
不过如果答案如果能再OO一些那就现好了!  回复

# re: C#面试基础问题 2006-04-02 18:29 | Dflying Chen

@林子
欢迎您提出修改的建议 :)  回复

# re: C#面试基础问题 2006-04-02 22:41 | 装配脑袋

有几道题的答案错误,列出来供你面试时参考:
3 C#支持多重继承么?
答案是类之间不支持,接口之间支持。类对接口叫做实现,不叫继承。
6 请描述一下修饰符protected internal。
正确答案是,在同一个Assembly中,他的访问级别和public一样,而跨Assembly访问时,他的访问级别同protected一样。即protected的范围 + internal的范围。
11 能够将非静态的方法覆写成静态方法么?
答案并没有大错,只是静态方法根本无override概念,属于引起困惑的问题
13 能够阻止某一个类被其他类继承么?
答案没错,但是如果应试者提到将所有构造函数的访问级别都设为Private或子类无法访问到的其他级别(Assembly外的internal),也有同样的效果。
14 能够实现允许某个类被继承,但不允许其中的某个方法被覆写么?
同样是可能引起困惑的题。因为默认(不带virtual)的方法就是不能被override的。带sealed的方法必定是被override过的。
20 接口和抽象类的区别是什么?
还有一个容易被忽视的区别,C#不允许接口带有静态成员,尽管这不是.NET的限制。
23 const和readonly有什么区别?
还有一些区别,当应试者提到时你不能说是错的:作字段时const隐含静态,const可以是局部的,readonly可以在构造函数中初始化等。  回复

# re: C#面试基础问题 2006-04-03 09:32 | Dflying Chen

@装配脑袋
3 C#支持多重继承么?
--答案是类之间不支持,接口之间支持。类对接口叫做实现,不叫继承。
inherit an interface没有什么不妥,为什么中文就不可以?
6 请描述一下修饰符protected internal。
--正确答案是,在同一个Assembly中,他的访问级别和public一样,而跨Assembly访问时,他的访问级别同protected一样。即protected的范围 + internal的范围。
同一个Assembly中依然是protected,您可以查阅一下相关资料。
11 能够将非静态的方法覆写成静态方法么?
--答案并没有大错,只是静态方法根本无override概念,属于引起困惑的问题
我只是问“能覆写成”么?如果她知道静态方法根本无override概念,显然可以打出这个问题。
13 能够阻止某一个类被其他类继承么?
--答案没错,但是如果应试者提到将所有构造函数的访问级别都设为Private或子类无法访问到的其他级别(Assembly外的internal),也有同样的效果。
您说的对,但如果这样思路想下来,我还可以说用C++写父类不能让C#继承。不要太钻牛角尖了。
14 能够实现允许某个类被继承,但不允许其中的某个方法被覆写么?
--同样是可能引起困惑的题。因为默认(不带virtual)的方法就是不能被override的。带sealed的方法必定是被override过的。
这个答案有理。
20 接口和抽象类的区别是什么?
--还有一个容易被忽视的区别,C#不允许接口带有静态成员,尽管这不是.NET的限制。
有道理,但接口不可以有成员吧,不管静态还是非静态的
23 const和readonly有什么区别?
--还有一些区别,当应试者提到时你不能说是错的:作字段时const隐含静态,const可以是局部的,readonly可以在构造函数中初始化等。
我不是很明白你的意思。但我觉得回答出“const关键字用来声明编译时常量,readonly用来声明运行时常量。”已经足够完美了。  回复

# re: C#面试基础问题 2006-04-03 09:44 | 装配脑袋

第六题你错了,不要坚持错误好吗?去试一下就什么都知道了。只有C++/CLI才支持你描述的那种访问级别,internal AND protected.
第十三题,我并非钻牛角尖,我恰好用到了这种手法。我需要一个抽象基类作为我的泛型类型字典,为了模拟type-traits我要求我的继承体系内类的数量有限而且对外隐藏。所以我就需要一个具有internal构造函数的抽象类,只有我自己的代码可以继承他,外部的不可以。你也许不能理解为什么这样做,但是它工作得很好。  回复

# re: C#面试基础问题 2006-04-03 09:50 | 装配脑袋

第二十题,成员函数也叫成员。这很重要,因为operator overloading的过程是静态的。
第二十三题,const可以用于局部常量,而readonly,实际是类的initonly字段,显然不能是局部的。  回复

# re: C#面试基础问题 2006-04-03 10:02 | 装配脑袋

如果我是参加面试的人,你会对我有何感想?
我真的不是钻牛角尖,我每一个质疑,都是我以我的真实程序为依据的,你忽视的地方并不是我恰好关注的地方,而是我一开始就关注的。  回复

# re: C#面试基础问题 2006-04-03 10:20 | Dflying Chen

@装配脑袋
第六题是我错了,感谢指正.
第十三题我明白你的意思,只是我感觉答出来sealed就够了。
第二十题,我不知道成员函数也叫成员,一直用方法(method)来表示。成员函数是member function么?  回复

# re: C#面试基础问题 2006-04-03 10:21 | Dflying Chen

@装配脑袋
你忽视的地方并不是我恰好关注的地方,而是我一开始就关注的。
这句话什么意思?  回复

# re: C#面试基础问题 2006-04-03 10:24 | 装配脑袋

就是说,我并不是为了挑你的错而想这么深的,而是一开始就知道
PS. method的定义不就是member function吗。。。
回复

# re: C#面试基础问题 2006-04-03 10:27 | Dflying Chen

@装配脑袋
感谢指点,受益匪浅:)
member function好久没有听说了,我基础也不是很好,见笑了。  回复

# re: C#面试基础问题 2006-04-05 16:31 | 小新0574

我都能答出来,哈哈,熟悉就是这种级别么?出些asp.net的题做做?  回复

# re: C#面试基础问题 2006-04-14 13:34 | Inkerman

quite useful, indeed.
Do you have English version of this blog?
I find it's very hard to read in Chinese as I learned my programming language only in English.
Thanks
Inkerman
回复

# re: C#面试基础问题 2006-04-14 13:40 | Dflying Chen

@Inkerman
sure, please refer to http://dflying.dflying.net/
hope may help :-)  回复

# re: C#面试基础问题 2006-05-05 14:58 | Li-Birch

呵呵,不错不错~~
我是在校学生,快毕业了
因为不是计算机专业,但又很喜欢,一直都在自学,也做过项目
不过还是不系统,特别是些基础性的东西
希望能多点类似的资料~
谢谢谢谢~~  回复

# re: C#面试基础问题 2006-05-05 18:18 | Dflying Chen

@Li-Birch
谈这种东西要冒风险的:)  回复

# re: C#面试基础问题 2006-05-07 09:33 | johnsir

@Dflying Chen
为什么要冒风险呀 ?  回复

# re: C#面试基础问题 2006-05-07 09:53 | Dflying Chen

@johnsir
这种问题往往带来很多争吵阿,还会被人骂……  回复

# re: C#面试基础问题 2006-05-09 10:30 | totti

能找到高手,但也存在碰到孔乙己的危险.  回复

# re: C#面试基础问题 2006-05-09 10:32 | totti

这种OO面试题,一定要结合一个Demo进行机试,不然很有可能碰到孔乙己。  回复

# re: C#面试基础问题 2006-05-09 10:33 | Dflying Chen

@totti
呵呵,好多人“架构”都是一套一套的……吓人哦  回复

# re: C#面试基础问题 2006-07-21 20:02 | 我支持 装配脑袋

好的程序员,必须在每一个基础上有最清晰的理解.一知半解很可怕的。  回复

# re: C#面试基础问题 2006-07-28 10:07 | a_a

后面有几道题答不上来,看来我基础还差得很远呀!
不过我也发现了一个问题,就是第24题,以我目前所学习的程度来看,应该是不对的。
24. System.String 和System.StringBuilder有什么区别?
System.String是不可变的字符串。System.StringBuilder存放了一个可变的字符串,并提供一些对这个字符串修改的方法。
我想StringBuilder中封装的应该也是一个普通的String,而不是“可变的字符串”,我特地用Reflector看了一下,结果也是这样。StringBuilder应该是封装了一系列方便对字符串进行操作的方法,并且对效率进行了优化。
不知道我说的对不对,请指正啦。  回复

# re: C#面试基础问题 2006-08-02 18:19 |

C#支持多重继承么?
答案是类之间不支持,接口之间支持。类对接口叫做实现,不叫继承。
这个说的好的,本质上是不支持多重继承的  回复

# re: C#面试基础问题 2006-08-03 21:46 | crazydd

请问: abtract class 可以有构造函数嘛?还是必须有?
abtract class 和 接口的区别,我觉得是不是还有一个? abtract class可以有数据成员,而接口不可以。我是菜鸟,请指教。  回复

# re: C#面试基础问题 2006-09-08 11:28 | 月亮lover

感谢楼主和装配脑袋,受益匪浅  回复

# re: C#面试基础问题 2006-09-08 11:55 | Dflying Chen

@月亮lover
我也要感谢装配脑袋,好多地方都让我豁然开朗!  回复

# re: C#面试基础问题 2006-10-20 00:31 | MK2

@a_a
string 虽然是引用类型,但每次操作都是做值复制,要重新申请和分配内存,再将新的string 的引用传出`````
而StringBuilder 是预先申请了空间,默认大小是16,只要当前字符串实质大小不超过可用空间大小,就不需要申请内存和复制工作。若要对字符串做大量操作,一般推荐用StringBuilder,因为申请分配内存和复制工作是很耗资源的。
在C#中,string类最奇怪,又是类,但传递的又不是引用,只是做值复制,这个特性又是非引用类型的特点``````  回复

0 Comments:

Post a Comment

<< Home