Blokes and Blokettes:
Here are some questions I was asked by a Consulting company for a mid-level programmer’s position for a .NET C# Web Developer role:
1) Can you have Multiple Inheritance in .NET ? Why did the language designers decide to exclude it?
1a) What is Encapsulation ?
Ans: I gave the classic answer which is that Encapsulation is the OO concept that a class contains both all the data it needs plus all the methods it needs to operate on the data. I was stunned to be told I was incorrect. The interviewer went on to describe what I would call ‘aggregation’. See below.
1 b) As to why Multiple Inheritance is not supported in .NET, here is a great answer from a the Microsoft Architect David Chou, .NET and Multiple Inheritance.
The basic answers are:
– .NET is a multi-language platform. Not all languages can be easily tweaked to support Multiple Inheritance.
– It would decrease cross-language library interoperability via the CLS
– Increases language complexity e.g. resolving inheritance ambiguities e.g classic ‘Diamond Problem’
– Complexity reduces readability and maintainability
AND There is no solution provided by Multiple Inheritance that cannot be solved by other means.
2) How can you simulate Multiple Inheritance in .NET?
Ans: By aggregation. Let’s say you wanted to simulate multiple inheritance of classes B and C into a new class A. What you do is instatiate instances of B and C as nested (inner) classes inside A. Now you have access to all of B and C’s members inside A.
The answer I gave was that Multiple Inheritance can be simulated through implenting multiple interfaces, which has been an acceptable answer elsewhere. This interviewer rejected it saying Interafce implementation is an example of polymorphism, not a way to simulate multiple inheritance.
3) What’s an abstract class ? Compare it to an Interface. Why would you implement an abstract class?
Ans: You would implement an abstract class if you wanted to provide a partial implementation of a class, yet leave some details to the final implementer. e.g. Framework classes are often implemented as Abstract Classes.
4) Let’s say you had a Framework Class (source code not available to you) and you wanted to extend it. How would you do it.
Ans: If Sealed class use Method Extensions
5) Building on the above, what if the class was not sealed and you wanted to implement a method which had an identical name to one in the Framework Class. Assume the original method is NOT virtual. What would you do ?
Ans: Use ‘new’ keyword in the method signature.
6) Let’s say you wanted to provide a new implementation of an existing method and the method is virtual. How would you do it.
Ans: Provide new implementation in sub-class using keyword ‘overrides’
7) Can you instatiate a class with some methods abstract ?
Ans: No. A class with any methods abstract must be marked as abstract and cannot be instantiated.
8. Provide the UML for the following scenario. Demonstrate knowledge of UML for class inheritace, interface implementation, aggregation, association.
9) What is the keyword ‘volatile’ used for ? Surely you want the most recent result available to you at all times. Shouldn’t all variables be marked volatile? Why or why not?
10) What is a delegate ? Provide some examples of how they are used.
11) Write the code that would enable you to update all the TextBoxes situated on a Panel with the data from one row of a GridView when that row is clicked.
Ans: Custom Event and Handler
12) Given a class B which inherits from class B, in which order are the constructors executed if you instantiate an instance of Class B using new B() ?
Ans: A constructor is called first then B’s constructor.
13) What is the effect of compilation debug=true in web.config ?
Ans: Generates debug symbols in pdb file, but also, and this is what he was after, it generates one file per class in the Temporary ASP.NET files folder. In a large project this is a huge amount of disk space consumed and can lead to all your disk space being consumed.
14) What is the effect of selecting ‘Debug’ from the Configuration Manager in Visual Studio.
Ans: They said ‘nothing’. I’m going to have to look this up.
Yeah, so what the guy was getting at is that the Configuration Manager has a ‘Debug’ setting by default, but it doesn’t do anything until you activate it by selecting options that are associated with it. One of those options is compilation options, which you can set to ‘debug’. But just selecting Configuration debug from the drop-down menu doesn’t generate pdb symbols automatically. You have to set up the debug configuration to actually do this.
Other things that a Configuration in Visual Studio can do are described in a book extract here . You can target seperate config files for each Configuration, select a build order for your projects, a different start page for your app, exclude and include different projects in your build and so on.
15) What is ViewState ? What can you do about a page that is heavy on View State ?
Ans: View State allows Web Apps to emulate stateful behaviour. It contains the encrypted value of all ASP server controls on the page and their original values. It can be turned off through control attributes and Page Directives. If ViewState is very heavy you may decide to use HTMLControls. These are not server controls and do not contribute to ViewState. You can also use IIS Compression which reduces Request size by 90% or use custom compression on the request i.e. you implement it say using gzip in the Application-level events.