Thursday 4 December 2014

Interview Question on C# Programming concept

What is data type and how many types of data types are  in .Net ?
A data type is a data storage format that can contain a specific type or range value. Whenever you declare variable, each variable must be assigned a specific data type. Some common data types includes integers, float, character etc.
There are two types of data type are available in .Net , they are :
1. Value Type
2. Reference type
Value Type : Value type refers to the data type that contains the data. Means that, the exact value or data is directly stored in this data type. It means when you assign a value type variable to another variable, It copies the value not reference. It stores the value in stack memory. Int, float, char are value type variable.
Reference Type : Refers to the data type that can access data by reference . Reference is an address where the actual data are stores. It stores in the heap memory. String, array, object etc are reference type.

What is difference between int and int32 ?
There are no difference between  int and int32. Int32 is a class and int is an alias name for it.

What is difference between constant and read only variable?
Constent : 
1. Constant are dealt with at compiler time.
2. Constant support value type variable.
3. Constant should be used when it is very unlikely that the value will ever change.
Read only : 
1. Read only veriable are evaluated at runtime.
2. It can hold reference type variable.
3. Read only variable should be used when runtime calculation is required.

What is difference between Boxing and unboxing ?
When value type is converted to an object(reference) type are known as boxing and a value type are known as unboxing.
Ex :
 int I =123;
object obj = i // (boxing )
int  I = int (obj)   // (unboxing )

What is the difference between value type and reference type?
1. Value type memory allocates in stack where as reference type memory manages in heap.
2. Value type contains variable and reference type directly stores in memory.

What is tuple? How can you instantiate a tuple?
Tupple is a fix size collection that can have element of either same or different data type . Similar to array a user must have to specify the size of tupple at the time of declaration. Tupple are allowed to hold up from 1 to 8 element and if there are more than 8 element. Then 8th element can be defined as another tupple. Tupple can be specified as parameter or return type of the method.
There are two method to instantiate a tupple. They are :
1. New Operator : Ex Tuple<string, int> t = new Tuple<string, int> (“Bhimsen”, 2)
2. Create Factory Method : Ex     Tuple<int,int> t = Tuple.Crete<int,int> (2, 4)

What is Enumeration? 
Enumeration is defined as a value type that consists of a set of named values. These values are constants and are called enumerators. An enumeration type is declared using the enum keyword. Each enumerator in an enumeration is associated with an underlying type that is set, by default, on the enumerator. The following is an example that creates an enumeration to store different varieties of fruits:
 enum Fruits {Mango, Apple, orange, Guava};
 In the preceding example, an enumeration Fruits is created, where number 0 is associated with Mango, number .

What is delegate ?
A delegate is similar to class that is used for storing the reference to a method and invoking that method at runtime as required. A deligate can hold the reference of only those method whose signature are same as that of delegate.
When delegate contains the reference of multiple method are called multicast delegate. Some of the examples of delegates are type-safe functions, pointers, or callbacks.

What is an array?
 An array is defined as a homogeneous(identical) collection of elements, stored at contiguous memory locations, which can be referred by the same variable name. All the elements of an array variable can be accessed by index values. An Index value specifies the position of a particular element in an array variable. Index number starts from 0.
EX :            int  arr[5] = {6,20,13,40,55}
In the above emample “arr” is the name of array. [5] is the size of array. And index number for element  6 is 0, 20 at 1st index, 13 at 2nd index and so on.

What is ArrayList?
Array is whose size can increase and decrease dynamically. Array list can hold item of different types.  As Array list can increase and decrease size dynamically you do not have to use the REDIM keyword. You can access any item in array using the INDEX value of the array position.

What is a Hashtable? 
Hashtable is a data structure that implements the IDictionary interface. It is used to store multiple items and each of these items is associated with a unique string key. Each item can be accessed using the key associated with it. In short, hashtable is an object holding the key-value pairs.

What is difference between HashTable and ArrayList?
You can access array using INDEX value of array, but how many times you know the real value of index. Hashtable provides way of accessing the index using a user identified KEY value, thus removing the INDEX problem.

What is the function of the Try-Catch-Finally block? 
The try block encloses those statements that can cause exception and the catch block handles the exception, if it occurs. Catch block contains the statements that have to be executed, when an exception occurs. The finally block always executes, irrespective of the fact whether or not an exception has occurred. The finally block is generally used to perform the cleanup process. If any exception occurs in the try block, the program control directly transfers to its corresponding catch block and later to the finally block. If no exception occurs inside the try block, then the program control transfers directly to the finally block.


Difference between Ref and Out parameter.

Ref and out parameters are used to pass an argument within a method. In this article, you will learn the differences between these two parameters.
Ref
The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.
Out
The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.

EX :

 class Program
    {

        static void Main(string[] args)
        {
         
            string name="Bhimsen";
            string address="Bhagalpur";
            int value = 1;
            Program program=new Program();
            Console.WriteLine("Name Before Calling RefMethod : " + name);
            program.RefMethod(ref name);
            Console.WriteLine("Name After Calling RefMethod : " + name);
            Console.WriteLine("Address Before Calling OutMethod : " + address);
            program.OutMethod(out address, out value);
            Console.WriteLine("Address After Calling OutMethod : " + address);
            Console.WriteLine("value After Calling OutMethod : " + value);
            Console.ReadLine();
           // Console.ReadKey();
        }
        private void RefMethod(ref string nameRef)
        {
            nameRef = "Kumar Bhimsen";
        }
        private void OutMethod(out string AddressOut,out int argval)
        {
            AddressOut = "Nathnagar, Bhagalpur";
            argval = 2 + 5;
        }
    }



No comments:

Post a Comment