Sunday, December 2, 2012

Binary tree

Design and implement a C# class for a binary tree. The class should allow addition/removal of nodes as well as traversing the tree.

Commonly asked C# questions

1. What is the name of the entry point function for all C# programs?

2. The C# data type int is a synonym for what CLR data type?

3. In C# all primitive data types and user defined data types inherit from what super object?

4. How is the C# string class use of == different from all other classes?

5. What encoding does C# use for characters?

6. What is the difference between the C# "ref" and "out" keywords when applied to method parameters?

7. What are the C# "checked" and "unchecked" keywords used for?

8. What is the difference between a C# "using" directive and a C/C++ "#include" directive?

9. What is the difference between a C# struct and a C# class in terms of reference types and value types?

10. Besides "public" and "private", what other two access modifiers can a C# class take?

11. Why does C# use class destructors far less often that C++?

12. In C#, are static methods accessed through a class name, an object name, or both?

13. How do you make a C# class abstract (when you want to inherit from it but never implement it directly)?

14. How do you prevent a C# class from being used as a base class (inherited from)?

15. C# does not support multiple inheritance. What C# mechanism allows you to have a semblance of multiple inheritance functionality?

16. What are the two kinds of C# properties?

17. Syntactically, what is the difference between calling a method and a property?

18. What is the approximate C# equivalent to a C++ function pointer?

19. What C# keyword do you use to implement a variable length argument list?

20. In C# if you must use pointers, how do you do it?

Interesting Features in C#

What are some great features in C#?

- Garbage collection
- Event handlers
- Property Access
- Delegates

Anything Else?

What is the purpose of Boxing?

To convert value types into reference types and store the value on the heap. Boxing (and Unboxing) have performance implications. This is because the boxing process creates/allocates a new object and copies the value into the new object.

Const and ReadOnly

What is the difference between const and read only fields?

Const is evaluated at compile time, readonly is evaluated at runtime. const must be initialized at declaration time only where as readonly field can be initialized in the declaration or constructor.

Const must be used very carefully especially when you try to make them public. Compiler will do something like find-and-replace for consts. So if a 3rd party assembly takes a dependency on your assembly that exposes const as public, then the const value will be there in 3rd party assebly's IL. So, if you change the value of the const later, then the 3rd party assembly has to be re-compiled (and possibly re-packaged and re-shipped).

Class and Struct

What is the difference between a class and a struct in C#?

>> Struct is a value type. Class is a reference type.
>> Struct instances are allocated on the stack (as they are value types). Class instances are allocated on the heap (as they are reference types).
>> A struct is implicitly sealed whereas a class is not.
>> A struct cannot be abstract, whereas a class can.
>> lock does not work on an instance of a struct as it is a value type. lock works on an instance of a class.
>> Events declared in struct do not have their += and -= automatically locked.
>> Struct always have a default built-in constructor whereas class does not have.
>> A class instance can be null whereas a struct instance cannot be null (as it is value type)
>> A struct cannot participate in "as" operator.
>> A struct does not particpate in Garbage Collection and hence it does not a ~Destructor() concept.
>> SizeOf cannot be used on a Class. It can be used on Structs.
>> Fields of a struct do not have a default value whereas fields of a class have a null/false/zero default value.

Polymorphism

What is polymorphism, and how is it implemented?

Polymorphism is one of the pillars of any object oriented programming language. Polymorphism enables us to write applications that process objects that share the same base class heirarchy as if they are all objects of the base class. With Polymorphism, the same method name and signature can be used to cause different actions to occur depending on the type of the object on which the method is invoked.

Ex:
public class Shape
{
public abstract double GetArea();
}

public class Rectangle : Shape
{
public override double GetArea()
{
return width * length;
}
}

public class Square : Shape
{
public override double GetArea()
{
return side * side;
}
}

In the above example, the user can take any shape object instance and call GetArea() method. Depending on the type of the object, the specific GetArea() is called and appropriate value is returned. Note all the methods (GetArea()) has the exact signature including the return type.