.NET Framework Interview Questions and Answers (547) - Page 20

What is a deep copy in .Net ?

Deep Copy is creating a new object and then copying the non static fields of the current object to the new object.

Example:

public object DeepCopy(object obj)

{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, obj);

object retval;
ms.Seek(0, SeekOrigin.Begin);
retval = bf.Deserialize(ms);
ms.Close();
return retval;
}

What is the difference between imperative code and interrogative code ?

The major difference between both the codes is that,
Imperative code does not return value whereas,
Interrogative code return value.
What is meant by TRIM() ?

TRIM() helps us to remove the off spaces characters from the beginning and from the end of the instance.

Example:

using System;


public class Example
{
public static void Main()
{
Console.Write("Enter your first name: ");
string firstName = Console.ReadLine();

Console.Write("Enter your middle name or initial: ");
string middleName = Console.ReadLine();

Console.Write("Enter your last name: ");
string lastName = Console.ReadLine();

Console.WriteLine();
Console.WriteLine("You entered '{0}', '{1}', and '{2}'.",
firstName, middleName, lastName);

string name = ((firstName.Trim() + " " + middleName.Trim()).Trim() + " " +
lastName.Trim()).Trim();
Console.WriteLine("The result is " + name + ".");
}
}


// The following is possible output from this example:
// Enter your first name: John
// Enter your middle name or initial:
// Enter your last name: Doe
//
// You entered ' John ', '', and ' Doe'.
// The result is John Doe
What is an Application Manifest ?

Application Manifest is used to give the information to the operating system.
It is an XML file that describes and identifies the shared and private side-by-side assemblies that an application should bind to at run time. These should be the same assembly versions that were used to test the application.
They may also be used to describe the metadata for files that are private to the application.
What is a Strong Name ?

A strong name is an assembly which includes the following :

1) Name of the assembly
2) Version Number
3) Culture Identity
4) A public Key Token.
Why to dynamically link library instead of statically linking the library ?

Using Dynamic Link Library offers several advantages :

1) DLL's save memory
2) Reduces Swapping
3) Saves disk space
4) Upgrades easier.
Explain few capitalization techniques ?

Some of the capitalization techniques are explained as below.

Pascal Casing: This convention capitalizes the first character of each word.
Example: MyWord.

Camel Casing: This convention capitalizes the first character of each word except the first word.
Example: myWord.

Upper Casing: This convention is used to capitalize identifiers, which contains an abbrevation of one or two characters.Identifiers which are of three or more characters use pascal casing instead.
Example: PI
How DLL's save space in RAM ?

The dll does not get loaded into RAM memory together with the main program, so space is saved in RAM.When a dll file is called, then it is loaded.
Are private class-level variables inherited ?

yes, Private class level variables are inheritable, but they are not accessible.

Example:

public class BaseClass

{
private const int _aNumber = 5;
public int ReturnANumber() { return _aNumber; }
}
public class DClass : BaseClass
{
public void SomeMethod()
{
// This is indirectly accessing a private implemented in the class
// this derives from, but existing inside this class.
int numbery = this.ReturnANumber();
}
}
public class Whatever
{
public void AMethod(DClass someDClass)
{
int thisIsNumberFive = someDClass.ReturnANumber();
}
}

What are immutable objects ?

Immutable objects are the objects whose state cannot be changed or modified once it is created.
Ex: String objects are immutable.
What is the Prototype Design Pattern ?

A prototype design pattern relies on creation of clones rather than objects.
Here, we avoid using the keyword 'new' to prevent overheads.
How to call methods in remoting Asynchronously ?

By using delegates, we can make Asynchronous method.

Example:

Below is a class named DataCache, which may periodically need to write its cache to a file. The class contains a method named FlushToDisk to simulate this operation.

class DataCache

{
public int FlushToDisk(string fileName)
{
// simulate a long operation Thread.Sleep(4000);
return 0;
}
}


Since writing to disk is an expensive operation, we would like execute the FlushToDisk method asynchronously. The next example defines the CacheFlusher delegate and then uses it to call the DataCache object’s FlushToDisk method asynchronously.

static void Main(string[] args)

{
// create the object
DataCache cache = new DataCache();
// create the delegate
CacheFlusher flusher = new CacheFlusher(cache.FlushToDisk);
// call the delegate asynchronously
flusher.BeginInvoke("data.dat", new AsyncCallback(CallbackMethod), flusher);
// wait to exit
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}


static void CallbackMethod(IAsyncResult result)

{
// get the delegate that was used to call that
// method
CacheFlusher flusher = (CacheFlusher) result.AsyncState;
// get the return value from that method call
int returnValue = flusher.EndInvoke(result);
Console.WriteLine("The result was " + returnValue);
}


If you run this example you will see that the line “Press Enter to Exit” will be displayed in the console window, followed by the output from CallbackMethod. This is because the Main method calls BeginInvoke and continues processing, displaying its own output without waiting for the CacheFlusher delegate to finish. Later, when the asynchronous operation is complete, the delegate calls the CallbackMethod, which writes its output to the screen.
How many types of Serialization are there in .Net ?

(1) SOAP Serialization
(2) Binary Serialization
(3) XML Serialization
(4) Custom Serialization

Note : Serialization is a process of converting an object into stream of data so that it can be transferred across/over the internet.


Thanks and Regards
Akiii
What are the two different ways by which you can make an object serializable ?

The two different ways by which we can make an object serializable are :-

(1) By using Serializable attribute
(2) By using ISerializable interface



Thanks and Regards
Akiii
What is the difference between ASP.net and VB.net?

VB.NET is a programming language that uses the .NET framework
and runtime when compiled. ASP.NET is a subset of the classes
in the .NET framework that are used in developing web
applications. ASP.NET is not really a language, it is a subset
of the classes in the .NET framework. If you look closely at
the codebehind files of ASP.NET pages, controls, or other
things you can create with ASP.NET, you will notice that they
are actually just .NET classes written using VB.NET, C#, or
whatever .NET language you chose to use. The *.aspx, *.ascx,
and other files that include html and/or directives are merged
with the associated codebehind file into a single class during
runtime (for example, the files index.aspx, index.aspx.vb, and index.aspx.designer.vb would all be compiled into one class during runtime). So to put it simply, an ASP.NET webapplication is just, in a way, a program written in .NET.Hopefully this helps clear things up, if you need more detail I would do a search online
What is SQL Azure ?

It is the relational database service on the Windows Azure platform.

Microsoft SQL Database extends SQL Server capabilities to the cloud.

Using SQL Database, you can easily provision and deploy relational database solutions.

Advantage : Manageability, High availability, Scalability, a familiar development model, and a relational data model.
What is MEF?How MEF Works?

MEF definition:
Managed Extensibility Framework is a new framework being incorporated inside the dotnet framework 4.0 whose purpose is to
plug-in components to an already running application. It comes under the System.ComponentModel.Composition.dll namespace.

Working of MEF:

MEF works upon the principle of demand/need and supply of parts.Initially we create a container and load all the possible sources of parts (export) into it and then Compose it. Whenever there is a requirement of some parts to be plugged into the application, MEF will perform a dynamic discovery of those parts from some location (which we call as Catalog). The composition engine resolves all the dependencies and throws an exception if it fails to do so. During composition phase,all the suppliable parts gets instantiated.The suppliable parts must be discovered and both the need and the suppliable parts must agree to the contract.
e.g.

//Step 1: Initializes a new instance of the AssemblyCatalog  

var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

//Step 2: The assemblies obtained in step 1 are added to the CompositionContainer
var container = new CompositionContainer(assemblyCatalog);

//Step 3: Import and Export components parts are created here
container.ComposeParts(this);

What is Part is MEF?

MEF is a framework for creating extensible applications that allows developers to discover and use extensions with no configuration required.In MEF terminology, Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.The parts are discovered at runtime implicitly via composition.When a part is created, the MEF composition engine satisfies its imports with what is available from other parts.
What is Catalog in MEF?

MEF is a framework for creating extensible applications that allows developers to discover and use extensions with no configuration required.Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.The parts are discovered dynamically via catalogs .Catalogs allow applications to easily consume exports that have self-registered themselves via the Export attribute.In MEF, Catalog's responsibility is to look for the compose-able parts (import and export definitions).
There are various kinds of catalogs as Assembly Catalog,Directory Catalog,TypeCatalog,Aggregate Catalog etc.
What is Import Attribute?

MEF is a framework for creating extensible applications that allows developers to discover and use extensions with no configuration required.Part is an object (e.g. a class, a method or a property) that can be imported or exported to the application.In MEF parlance, Import attribute defines the need that a part has.

e.g.

[Import(typeof(ICalculator))]

public ICalculator CalciPlugin { get; set; }


The [Import] attribute declares something to be import.At runtime, the composition engine will fill it when an object is composed. The import must satisfy a contract and the Export has to match the same contract.The interface specified here is
ICalculator.Any export declared with a matching contract will fulfill this import. e.g.

[Export(typeof(ICalculator))]

public class Add:ICalculator
{
#region Interface members
public int GetNumber(int num1, int num2)
{
return num1 + num2;
}
#endregion
}


Where the interface ICalculator is declared as under

public interface ICalculator

{
int GetNumber(int num1, int num2);
}


Multiple values can be imported by using the [ImportMany] attribute.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories