Frequently Asked Interviews Questions and Answers (28) - Page 1

These FAQs has been especially prepared by DotNetFunda.Com team to help the visitors getting standard and proven answers of the Frequently Asked Interview Quesions. If you would like to suggest a better answer for these questions, please feel free to respond to the question by clicking the question title link. To suggest a new FAQ, click here.

28 records found.
 

What do you mean by String objects are immutable?

Page copy protected against web site content infringement by Copyscape


String objects are immutable as its state cannot be modified once created. Every time when we perform any operation like add, copy, replace, case conversion or when we pass a string object as a parameter
to a method a new object will be created.
 
Example:

String str = "ABC";
str.Replace("A","X");
 
Here Replace() method will not change data that "str" contains, instead a new string object is created to hold data "XBC" and the reference to this object is returned by Replace() method.

So in order to point str to this object we need to write below line.
str = str.Replace("A","X");
Now the new object is assinged to the variable str . Earlier object that was assigned to str will take care by grabage collector as this one is no longer in used.

What is the difference between int.Parse and int.TryParse methods?

Page copy protected against web site content infringement by Copyscape


int.Parse() is a simple method used to convert string to integer. It throws exception when null or invalid input is provided with the Parse() method. Hence it is slow.

As for Example :

        string strQty = "a10";
        //This Line Will Throw Exception "Input string was not in a correct format."
        int qty = int.Parse(strQty);
        Response.Write(qty.ToString());

So, when you  are using Int.Parse() in your code, you need handle the exception properly.

        try
        {
            string strQty = "a10";
            //This Line Will Throw Exception "Input string was not in a correct format."
            int qty = int.Parse(strQty);
            Response.Write(qty.ToString());
        }
        catch(FormatException fx)
        {
          // Handle the exception
        }

Int.TryParse() does not throw any exception instead it returns Boolean value which represent the success or faliure.  We must describe second parameter as out  parameter which holds result.

As for Example:



string strValue = "DotNetFunda";
        int intval = 0;
        if (int.TryParse(strValue, out intval))
        {
            Response.Write("String With Integer : " + intval);
        }
        else
        {
           Response.Write("String Without Integer");
        }

Here The output will be "String Without Integer";


What do you mean by Boxing and Unboxing?

Page copy protected against web site content infringement by Copyscape


Before explain about boxing and unboxing let take a  brief background of  different types of data types.


There two types.
Value Type 
Reference Type



Value Types:

A value type’s variables contain the value itself. I mean it doesn’t contain a pointer to the object. It does not store the data into heap memory. Different value types are

  • Integral Types (sbyte, byte, short, ushort, int, uint, long, ulong), bool type, char type,
    Floating point types(flaot,double) and the decimal types. They are all aliases of the .NET System Types.
  • Struct Types
  • Enumeration Types

Reference Types:



A variable that contains reference or address of the actual data.
Reference types are allocated on the managed heap. Different reference
types are
  • The Object Type
  • The class Type
  • Interfaces
  • Delegates
  • The string type
  • Arrays

Boxing And Unboxing:

Converting a value type to reference type is called Boxing

Unboxing is just opposite to boxing, that is Converting a reference type to value type. But here you have to specify explicitly in which value type you want to extract from the object. 


Ex:
int32 valueTypeVaraible= 100; //value type variable


Memory Allocation into Stack

STACK



 

object referenceTypeVariable= valueTypeVaraible; 
Now the memory is allocated on the heap of size equal to size of the varaiable,the value type bits are copied to the newly allocated memory and the address of the object is returned and stored in reference type.This is basically called Boxing.

   
 




You can see from the above diagram referenceTypeVariable is storing the pointer of the heap address where the actual value has stored.

Int32 valueTypeVaraible2=(Int32) referenceTypeVariable ; //Unboxing



Conclusion:

  • Value types are allocated on the thread's stack and they hold the actual value
  • Reference types are allocated on the thread's stack and they hold a pointer to an object allocated in the managed heap
In both cases variables are allocated on the thread's stack, the difference being that a value type holds the actual value (for example, an integer, a struct), whereas the reference type holds a pointer to the memory area where the actual value is stored (for example, an instance of a StringBuilder class).

What is Delegates and Events ?

Page copy protected against web site content infringement by Copyscape


The word event and delegates always come together, but they are separate to each other. It is true that delegates are independent objects which each event needs to perform its callback.

Delegate:

Delegates can be regarded as function prototypes, which prototype one method so that an object of it can point to that function. In other words, delegates are types which maps a method signature, so that a variable of a delegate can refer a method of same signature.
For instance,
delegate int MySampleDelegate(int x, int y);
  public void add(int x, int y)
  {
     return x + y;
  }
  public void substract(int x, int y)
  {
     return x - y;
  }
 
From the main function you can call a delegate using :

MySampleDelegate obj1 = new MySampleDelegate(add);
MySampleDelegate obj2 = new MySampleDelegate(substract);
Console.Write(obj1(10, 20)); // Prints 30
Console.Write(obj2(40, 10)); //Prints 30

So the first object points to add method, while the second object to substract. You can call a delegate similar to how you call a method.

Events :  

Events are callbacks from a class which invokes the associated delegate directly when the event is executed. In case of event driven approach, there is a subscriber of an event. Event is subscribed from outside using an eventhandler, which is a method declared externally and can be called back whenever the class wants to invoke.

Thus you can say Events are generated from class itself which calls a delegate. The delegate if subscribed from outside will call the actual method.



What are the ways to call a Delegate ?

Page copy protected against web site content infringement by Copyscape


There are quite a few ways a delegate may be called. Say for instance, I may call it as we do as normal method, I can make Asynchronous to the method, serialize a delegate, clone a delegate etc.

Synchronous call :

MySampleDelegate obj1 = new MySampleDelegate(add);
obj1.Invoke(19, 30); // obj1(19,30)

Asynchronous Call :


obj1.BeginInvoke(19, 30, new AsyncCallback(asynchcallback), null);

Where asynchcallback represents the callback method which will be called automatically with an object of AsyncResult from which we can get the methods.


Navigate to page: 1  2 3 4 5 6