Friday, 3 November 2017

C# Interview Questions TopicWise

Value Type and Reference Types

1.What are Value Types ? Give example
Variables that store data are called value types. Value types are stored on stack.
They contain the actual values. eg-int, enum, structs.  
2.What are Reference Types? Give Example.
Variables that store reference to actual data are called Reference types.Reference types
stored on heap but contain the address on heap.
eg-class,interface,delegate,string,objectArray

3.Difference between value type and reference types ?
    
Value Type
Reference Type
They are stored on stack
They are stored on heap
Contains actual value
Contains reference to a value
Cannot contain null values. However this can be achieved by nullable types
Can contain null values.
Value type is popped on its own from stack when they go out of scope.
Required garbage collector to free memory.
Memory is allocated at compile time
 Memory is allocated at run time
   
4.Diff bt stack and heap
    
Stack
Heap
Values are stored on one another like a stack.
Values are stored in random order.like dumped into a huge space
Used for value type
Used for reference types




5. Whats the output of the following program ? 
      1.       int x = new int();
                x = 20;
                int y = new int();
                y = x;       
                y = 30;     
//Line3   
                return x;


            Output--20
            At Line 3 only y value is changes. "x" has its own location on stack which is not 
            affected by this and hence retains is old values. 

     2.       class Numbers
               {
                   public int MyValue;
               }


            
               Numbers x = new Numbers();
               x.MyValue = 3;
               Numbers y = new Numbers();
               y = x;
               y.MyValue = 4;
               Console.WriteLine( x.MyValue);
           
               Output- 4. Both x and y are pointing to the same location on the heap.       
 

6.What is boxing and unboxing ? 
    Boxing-- Converting a value type to a reference type .This is implicit.
                     During boxing follo 3 things happen-
                     Memory is allocated on heap,
                     Value is copied from stack to heap,
                      Reference is updated to point t heap
                     eg-
                int i = 123;
  object o = i; 
                                here if you make i= 456; 
                                then o=123, //value does not change 
 
 Unboxing- Converting reference type to value type.
                     The value which is being unboxed has to be boxed first. 
                      eg- int j = (int) o;       

 
7.Are Value Type sealed ?
   Yes                


8.Can you implement IDisposable for value type ?
Structs being a value type can inherit IDisposable interface

10.how do you get size of value type ?
using SizeOf operator.


11.When you declare int i,j hos is it alocated?
Both of them are allocated on the stack at the same time and they go out of scope at the 
same time.

12.What do you mean by Pass By Value ? 
Actual value is copied to another variable  eg-
              calling method--                    Actual Method       
              int a=4,                                    Add(int a,int b){
              int b=5,                                     a=6,b=7 }
             Add(4,5)
             Print a and b --Line 1
Even though the value of a and b is changed in the actualfunction it does not  chane in 
line 1.               
        
13.What do you mean by Pass By Reference?
The calling functions passes the address of the variable instead of the actual values.Any 
changes made inside the actual method will be reflected in the calling function.
 eg-
              calling method--                    Actual Method       
              int a=4,                                    Add(ref int a,ref int b){
              int b=5,                                     a=6,b=7 }
             Add(ref 4,ref 5)
             Print a and b --Line 1
The value at Line 1 willl be a=6,b=7     


13.What do you mean by Pass By out? 
This is similar to pass by ref. Only diff is before passing the values they need not be 
initialized.But they have to be initialized inside actual method.They are mailny used in remoting.
eg-
              calling method--                    Actual Method       
              int a,                                       Add(out int a,out int b){
              int b,                                       a=6,b=7 }
             Add(out 4,out5)
             Print a and b --Line 1
 The value at Line 1 willl be a=6,b=7   

       
14.what are params ? 
They are used when you do not know how many variables you will be passing across methods.
eg-
           calling method--                     Actual Method       
           method(1,2,3,4)                      method(int a,params int[])   
The param array should be last while declaring method arguments.       
   
15.What the difficult parameter type used when calling methods in c# ?
Pass by Value
       
13.What are diff types of parameter in c# ?
Value parameter(in), Reference/ inout paramete  ref),Output parameter(out),  
Parameter  Array(param)

    
      

No comments:

Post a Comment