Tuesday, November 22, 2005

Technical Interview Questions

Q.1 What is a diff between an abstract class and an interface ? What would govern your decision for its usage in .NET?
Well I have seen many people get confused in this question and telling differences as:

1 -- An abstract class is a class which cannot be instantiated and an interface is a set of functions. Thier usage would be goverened by the follwoing reasons :-a. If I have some common functions to be implemeted in the base class then one should use abstract class as Interface cannot have funcation implementations.b. Languages like C# and VB.NET only allow single inheritance of classes hence you have only one base class and rest will have to be created as interfaces.e.g. If I did my class A to inherit from Class B and Class C .. then inheritance can only be done from one of these classes the one will have to created as an interface.
c. Interfaces cannot have state information i.e. they cannot contain variables but can contain constants. On the other hand, an abstract class can contain both variables and constants.

All these answers are right but one has missed the key answer. A child class implements interface but extends abstract class.

What it actually means is that an interface is a contract between 2 or more classes i.e. it defines mutual agreement between 2 or more parties like interfaces exposed for COM objects.

On the other hand an abstract class defines a relationship with its child class not the contractual relationship but inheritance. It is a IS-A relationship between abstract class and child class.

So if you want to implement generalization/specialization i.e. HAS-A relationship then use abstract class but if you want to provide a common interface or contract between 2 or more parties so that service-consumer class only needs to know the services of its service-provider class not their implementation use Interfaces.

Q.2 Can you have two applications on the same machine one which is using .NET Framework 1.0 and the other using 1.1 ?
Yes

Q.3 If you are using a web service and you want its path to be picked up from the config file what will you do ?
Set its behaviour property to dynamic this will add a new appSetting entry in the config file for the webservice path.

Q.4 Can I serialize Hash table with XmlSerializer?
XmlSerializer will refuse to serialize instances of any class that implements IDictionary, e.g. Hash table. Soap Formatter and Binary Formatter do not have this restriction.

Q.5 Whether ADO.Net provides conneted recordset like ADO or dis-connected?
ADO.Net provides both types of recordsets. A connected recordset is called data-reader but using data-reader we cannot modify database using it. It is useful for small number of data-records as it is very fast but useless for large number of records as it will keep the connection with tha data-base for a long number of time. We can extract records from multiple tables i.e. multiple record-sets in the same data-reader.

A dis-connected record-set is called a DataSet. It is a complete mirror of data-base i.e. we even can keep relationships in it. A data-set can contain multiple tables with relationships among them. User can write the contents of the dataset in an XML file without any effort. A data-set can also be used to move data from one database to another.

Q.6 What does 'managed' mean in the .NET context?
The term 'managed' is the cause of much confusion. It is used in various places within .NET, meaning slightly different things.

Managed code: The .NET framework provides several core run-time services to the programs that run within it - for example exception handling and security. For these services to work, the code must provide a minimum level of information to the runtime. Such code is called managed code. All C# and Visual Basic.NET code is managed by default. VS7 C++ code is not managed by default, but the compiler can produce managed code by specifying a command-line switch (/com+).

Managed data: This is data that is allocated and de-allocated by the .NET runtime's garbage collector. C# and VB.NET data is always managed. VS7 C++ data is unmanaged by default, even when using the /com+ switch, but it can be marked as managed using the __gc keyword.

Managed classes: This is usually referred to in the context of Managed Extensions (ME) for C++. When using ME C++, a class can be marked with the __gc keyword. As the name suggests, this means that the memory for instances of the class is managed by the garbage collector, but it also means more than that. The class becomes a fully paid-up member of the .NET community with the benefits and restrictions that brings. An example of a benefit is proper interop with classes written in other languages - for example, a managed C++ class can inherit from a VB class. An example of a restriction is that a managed class can only inherit from one base class.

Q.7 What is reflection?
All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies), and can be accessed by a mechanism called reflection. The System.Reflection namespace contains classes that can be used to interrogate the types for a module/assembly.

Using reflection to access .NET metadata is very similar to using ITypeLib/ITypeInfo to access type library data in COM, and it is used for similar purposes - e.g. determining data type, their sizes for marshaling data across context/process/machine boundaries.Reflection can also be used to dynamically invoke methods (see System.Type.InvokeMember), or even create types dynamically at run-time (see System.Reflection.Emit.TypeBuilder).

Reflection can also be used for late-binding,r producing generic methods or mosly for dynamically generating classes and modules. Remarkable, isnt it.

Q.8 What is the difference between Method Hiding and Shadowing?
Well there is no difference in these concepts.They are just 2 sides of the same coin, 2 names of the same term but in different technologies. Method hiding is the concept of C# whereas method shadowing is the concept of VB.Net. Method Hiding and Shadowing are the concepts by which you can provide a new implementation for the base class member without overriding the member. It is similar to method overriding but only in this case you are hiding and shadowing the definition of the parent class. For both these concepts same rules apply as in case of Method overrding i.e. the signature of the method must be same.

Now what is the difference between Method hiding and Shadowing with Method overloading. Hmm, it is simple. The difference lies when you call the derived class object with a base class variable.In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.

To shadow a member of the parent in child class, use Shadow keyword in VB.Net and to hide a member of the parent class in child, use new keyword in C#.

The following is the code snippent, showing these concepts:

Method Shadowing:

Public Class BaseShadow
Public Overridable Function OverrideMe() As String
Return "Base Class Override Me"
End Function

Public Function ShadowMe() As String
Return "Base Class Shadow Me"
End Function
End Class

Public Class DerivedShadow Inherits BaseShadow
Public Overrides Function OverrideMe() As String
Return "Derived Class Override Me"
End Function

Public Shadows Function ShadowMe() As String
Return "Derived Class Shadow Me"
End Function
End Class

Method Hiding:

public class BaseHide
{
public BaseHide()
{ }

public virtual string OverrideMe()
{
return "Base Class Override Me";
}

public string HideMe()
{
return "Base Class Hide Me";
}
}

public class DerivedHide:BaseHide
{
public DerivedHide()
{ }

public override string OverrideMe()
{
return "Derived Class Override Me";
}

public new string HideMe()
{
return "Derived Class Hide Me";
}
}

No comments: