Tuesday, January 8, 2013

NET and Sql Server interview questions - By which query do we merge two different columns into single column?


Answer:
This is one of the most typical question asked by the interviewers.

Let’s us assume that we have the following two table of Product and Vendor with their respective fields like below.

ProductId ProductName Cost //Product table

VendorID VendorName ProductId //Vendor table

Now let’s begin with how you can design one to one relationship between these two tables.

One to One relationship:-
Below diagram show one to one relationship between Product and Vendor table.




In the above Product table “ProductId” is defined as a primary key and in the Vendor table “ProductId” is defined as foreign key with reference to the “ProductId” of the Product table.

In this way you can achieve one to one relationship.

One to Many relationship:-
Now we want to define one to many relationship on Vendor and Product table.
In this many records in one table correspond to the one record in another table.

Example: Every one vendor can have multiple products. So there exist one-to-many relationships between Vendor and Product table.

In order to define one to many relationship you have to add one column name as VendorId in Product table which is foreign key reference to VendorId of Vendor table.

Below diagram show one to many relationship on Product and Vendor table.



Many to Many relationship:-

In this, one record in one table corresponds to many rows in another table and also vice-versa.
For instance: In a company, one vendor can sale many products and also one product can be sold by many Vendors.
Given below diagram is a sample of many-to-many relationship. For defining many to many we have to add one more table VendorProduct which is linked to the primary key of Product and Vendor tables.




In the following video, view samples of various questions asked in C#, .NET and Sql Server interview.



Please click here to see more .NET/SQL Server interview questions

Regards,

Visit Authors blog for more .NET and ASP.NET interview questions

Class Diagram

Wednesday, December 19, 2012

C# and .NET interview question: -What is short circuiting in C#?




When developers get this .NET interview question many of them get stunned with the name.



Short circuiting occurs when you do logical operations like ‘AND’ and ‘OR’.

 “When we use short circuit operators only necessary evaluation is done rather than full evaluation.”
Let me explain the above sentence with a proper example. Consider a simple “AND” condition code as shown below. Please note we have only one “&” operator in the below code.

if(Condition1 & Condition2)
{
}

In the above case “Condition2” will be evaluated even if “Condition1” is “false”. Now if you think logically it does not make sense to evaluate “Condition 2”, if “Condition 1” is false.It’s a AND condition right? , soif the first condition is false it means the complete ANDcondition is false and it makes no sense to evaluate “Condition2”.

There’s where we can use short circuit operator “&&”. For the below code “Condition 2” will be evaluated only when “Condition 1” is true.
if(Condition1 && Condition2)
{
}

The same applies for “OR” operation. For the below code (please note its only single pipe(“|”).)   “Condition2” will be evaluated even if “Condition1” is “true”. If you think logically we do not need to evaluate “Condition2” if “Condition1” is “true”.

if(Condition1 | Condition2)
{
}

So if we change the same to double pipe (“||”) i.e. implement short circuit operators as shown in the below code, “Condition2” will be evaluated only if “Condition1” is “false”.


if(Condition1 || Condition2)
{
}

Taken from the best selling interview question book .NETinterview question by Shivprasadkoirala 

Here’s an awesome video on concurrent generic collections in C#.