.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

18 most asked .NET , C# interview questions and answers Part 1

Posted By:Shivprasad koirala       Posted Date: May 18, 2011    Points: 200    Category: .Net Framework    URL: http://www.questpond.com  

Recently one of friends went to a mid size IT company and he got stuck in some simple .NET interview questions. The questions where pretty simple but I think he did not go prepared. Personally i think working in a software project is different and cracking a .NET interview is a complete different ball game. A developer can be excellent in his company , he can be a hero but then simple interview questions like explain your software architecture , what role did you play gets them stumped.
 

18 most asked .NET , C#  interview questions and answers Part 1

By Shivprasad koirala.



Introduction
Question 1 :- What role did your play in your current project?

Question 2:-Can you explain architecture of your project ?
Question 3:-What is the sequence in which ASP .NET page life cycle is executed ?
Question 4:-
What are Generics ?
Question 5:-What is the advantage of using MVC pattern?

Question 6:- What is MVVM ( Model view view model) Design pattern ?
Question 7:-Which design patterns have you used in your project ?  
Question 8:- What is trigger and different types of Triggers?
Question 9 :-Why Arrays are faster than ArrayList?
Question 11:- What is the difference between Array and ArrayList?
Question 12:-  What is Render and PreRender?
Question 13:-How will you design one to one, one to many and many to many relationships in sql server?
Question 14:-How will you do Unit Testing using VSTS?
Question 15:-How will you do Unit Testing using Nunit?
Question 16:-Typical Complicated SQL query asked in .NET interviews to see if you know how to split columns into a row?
Question 17:-Typical Complicated SQL query asked in .NET interviews to test your SQL CASE syntax capability?

Question 18:-Select second highest salary from the table?

Introduction

Recently one of friends went to a mid size IT company  and  he got stuck in some simple .NET interview questions.

The questions where pretty simple but I think he did not go prepared. Personally i think working in a software project is different and cracking a .NET interview is a complete different ball game.

A developer can be excellent in his company  , he can be a hero but then simple interview questions like explain your software architecture , what role did you play gets them stumped.

In this whole series i am going to walk you through questions which are simple , common but when not prepared you always come out with a bad answer and there of loosing a good opportunity.

Do view my 500 videos on .NET , C# , WCF , Silverlight , design patterns , ASP.NET , UML by click on .NET interview questions

If you want to have a overview of what kind of questions are asked in .NET interviews do click on this link http://youtu.be/pC3aLkf_A98?hd=1 where i have discussed from overall view of different questions asked during .NET and C# interviews,

If you want to understand the physcology of how .NET interviewer conducts interviewer click on this link  http://youtu.be/3PXspVcSSKE?hd=1

 

What role did your play in your current project?
 

Answer:
 

This is a great c# interview question , but .NET professionals tend to answer this question in one liners like i am a developer , project manager.

In  today's world people expect professionals who can do multitasking. The best way to approach the answer is by explaining what things did you do in the complete SDLC cycle. Below goes my version of the answer , you can tailor the same as per your needs.

My main role in the project was coding , unit testing and bug fixing. Said and done that i was involved in all phases of the project.

I worked with the business analyst in the requirement phase to gather requirements and was a active member in writing use cases.

I also assisted the technical architect in design phase. In design phase i helped the architect in proof of concept  and putting down the technical design document.

My main role was in coding phase where i executed project with proper unit testing.

In system integration test and UAT I was actively involved in bug fixing.

Other than the project i help COE team for any kind of RND and POC work.

In case you are doing some extra activities in the company like helping COE , presales team or any other initiative do speak about the same.

Can you explain architecture of your project ?

 

Answer:
 

This is normally the first .NET interview question which pops up in .NET and C# interviews. Personally i think your full  .NET interview ahead depends on how you will answer this question.

Common mistakes :- Many developers make a common mistake of answering this question by saying that we have used C# , SQL server , ASP.NET etc. But please do note that the question is, what is the architecture of your project and not technology.

So a typical answer will be something as below. Please note your answer will vary as per your architecture and structure.

"My current architecture is a simple 3 tier architecture with UI in ASP.NET , middle layer are simple .NET classes and DAL is using enterprise application blocks.

The UI  is responsible to take user inputs , all my business validations are centrally located in middle layer and the data access layer is responsible to execute stored procedured and SQL queries to SQL Server.

Strongly typed business objects are used to pass data from one layer to other layer. The database of the project is in a 3rd normal form."

So first start with the overall architecture , talk about each layer , how data is passed between each layer and the database design structure.

One very important tip if you can draw a diagram and explain....I think you have the job.

 

 

 

What is the sequence in which ASP .NET page life cycle is executed ?

Answer:
This is a great  and the most asked ASP.NET interview question. Out of 100 .NET interviews at least 70 interviewer will ask this question. The answer is very simple but the problem with the answer is that developers forget the  sequence of ASP.NET page life cycle.

The best way to remember this answer is by remembering the word SILVER.

S (Well this word is just to make up the word , you can just skip this.)
I (Init)
L (Load)
V (Validate)
E (Event)
R (Render)

 

What are Generics ?

You can also see the video on Generics at http://youtu.be/7bKhAJpY9ho?hd=1

This is one of the most important typical question, which is asked in most of the interviews to check whether you know about generic.Generic help us to create flexible strong type collection.
Generic basically separates the logic from the data type in order maintain better reusability, better maintainability etc.

Lets see an simple example to understand how exactly generic separate logic from data type.

class Check
    {
        public bool Compare(UNKNOWNDATATYPE i, UNKNOWNDATATYPE j)
        {
            if (i.Equals(j))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

In above code snippet, I have created a class called as "Check" with UNKNOWNDATATYPE so that, I can define different data types at run time. Now, in below code you can see that I have created two object of Check class with two different datatypes(int,string).
 

class Program
    {
        static void Main(string[] args)
        {
            Check ObjCheck = new Check();    //here i have defined int datatype
            bool b1 = ObjCheck.Compare(1, 1);
            Check Obj1 = new Check();    //here i have defined string datatype
            bool b2 = Obj1.Compare("feroz", "kalim");
            Console.WriteLine("Numeric Comparison Result:" + b1);
            Console.WriteLine("String Comparison Result:"+ b2);
            Console.ReadLine();
        }
    }

Below is the full code snippet for the same so that you can try it by yourself and see the resultant output.
 

class Program
    {
        static void Main(string[] args)
        {
            Check ObjCheck = new Check();
            bool b1 = ObjCheck.Compare(1, 1);
            Check Obj1 = new Check();
            bool b2 = Obj1.Compare("feroz", "kalim");
            Console.WriteLine("Numeric Comparison Result:" + b1);
            Console.WriteLine("String Comparison Result:"+ b2);
            Console.ReadLine();
        }
    }

    class Check
    {
        public bool Compare(UNKNOWNDATATYPE i, UNKNOWNDATATYPE j)
        {
            if (i.Equals(j))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

What is the advantage of using MVC pattern?

Answer:
 

MVC is one of the most used architecture pattern in ASP.NET and this is one of those ASP.NET interview question to test that do you really understand the importance of model view controller.

  • It provides a clean separation of concerns between UI and model.
  • UI can be unit test thus automating UI testing.
  • Better reuse of views and model. You can have multiple views which can point to the same model and also vice versa.
  • Code is better organized.

What is MVVM ( Model view view model) Design pattern ?

Answer:

MVVM is a UI design pattern. The main use of this pattern to remove UI cluttered code like bindings , synchronization etc.

In this pattern we create a extra class called as view model  or model which acts as a bridge between model and view. The view sends the actions and data to the model view class who in turns sends the data to model. Any changes in the model is replicated or informed to the UI using the INotifyPropertyChanged interface.

Which design patterns have you used in your project ?

click here to see video of factory design pattern  http://youtu.be/yDEmb5XYc_s

 

Answer:
 

First thing what ever you say , for heaven sake do not say we had used all design patterns ;-) . Look at your project and see which design pattern have you used. Almost all projects use design patterns knowingly or unknowingly , until the project is complete disorganized.

While explaining do ensure to make a 1 line comment atleast to say why that design pattern was used.

Below goes a decent answer of explaining things collectively. Please note answer will change as per your project implementation. 

In my current project we had used MVC, Singleton, factory , iterator   and template pattern.

My project was tier architecture, divided in to 3 main sections, UI, Business layer and data access layer.

At the presentation level we had used MVC pattern, so the UI and model was separated using controller class.  

To share common data across all tiers we had used the singleton pattern.

Factory classes where used between each layer for decoupling and centralizing object creation to avoid code complication.

Iterator pattern was used to browse through middle tier objects thus providing a tight encapsulation.

The   middle tier objects and their child objects where standardized using template pattern for consistent vocabulary and naming convention for business objects.

What is trigger and different types of Triggers?

Answer:
 

Trigger is a SQL server code, which execute when a kind of action on a table occurs like insert, update and delete. It is a database object which is bound to a table and execute automatically.

Triggers are basically of two type's namely "After Triggers" and "Instead of Triggers".
1.After Triggers:- this trigger occurs after when an insert, update and delete operation has been performed on a table.

"After Triggers" are further divided into three types
AFTER INSERT Trigger.
AFTER UPDATE Trigger.
AFTER DELETE Trigger.

Let us consider that we have the following two tables.

Create "Customer" table with the following field as you see in the below table.

 

Cust_ID Cust_Code Cust_Name   Cust_Salary
1 A-31 Moosa 4500
2 A-09 Feroz 5000
3 A-16 Wasim 4000
 

Create "Customer_Audit" table with the following field as you see in the below table.

 

Cust_ID   Cust_Name Operation_Performed Date_Time
 

The main purpose of creating "Customer_Audit" table is to record the data which occurs on the "Customer" table with their respective operation and date-time.

Let's begin with "After Insert Trigger":- This trigger fire after an insert operation performed on a table.
Let us see the example of "After Insert Trigger" for better understanding.

Query:-

Create Trigger TrigInsert on Customer
For insert as 
declare @Cust_ID int;
declare @Cust_Name varchar(100);
declare @Operation_Performed varchar(100);
select @Cust_ID=i.Cust_ID from inserted i; 
select @Cust_Name=i.Cust_Name from inserted i; 
set @Operation_Performed='Inserted Record -- After Insert Trigger';
insert into Customer_Audit
(Cust_ID,Cust_Name,Operation_Performed,Date_Time) 
values(@Cust_ID,@Cust_Name,@Operation_Performed,getdate());
PRINT 'AFTER INSERT trigger fired.'

Now, insert a record into Customer table:

Query:- insert into Customer values ('A-10','Danish')

Once the insert statement is successfully done, the record is inserted into the "Customer" table and the "After Trigger" (TrigInsert) is fired and the same record is also stored into "Cutomer_Audit" table.

To see the record in "Customer_Audit" table write query as below:-

Query:- select * from Customer_Audit

Cust_ID   Cust_Name Operation_Performed Date_Time
4 Danish Inserted Record -- After Insert Trigger 2011-04-06 19:46:56.390

You can see that the same record is seen in the "Customer_Audit" table with Operation_performed and the date_time when it was updated.

Now let's see for "After Update Trigger":-This trigger fire after an update operation performed on a table.
Let us see the example of "After Update Trigger" for better understanding.

Query:- Create trigger TrigUpdate on Customer
For Update as 
declare @Cust_ID int;
declare @Cust_Name varchar(100);
declare @Operation_Performed varchar(100);
select @Cust_ID=i.Cust_ID from inserted i; 
select @Cust_Name=i.Cust_Name from inserted i; 
set @Operation_performed='Inserted Record -- After Insert';

f update(Cust_Name)
set @Operation_Performed='Updated Record -- After Update Trigger.';
insert into Customer_Audit
(Cust_ID,Cust_Name,Operation_Performed,Date_Time) 
values(@Cust_ID,@Cust_Name,@Operation_Performed,getdate())

PRINT 'AFTER UPDATE Trigger fired.'

Now, update a record into "Customer" table:-

Query:- update Customer set Cust_Name = 'Khan Wasim' where Cust_Code like 'A-16'

The record is updated into the Customer table and the TrigUpdate is fired and it stores a record into
"Cutomer_audit" table.

To see the record Customer_Audit table write query for that.

Query:- select * from Customer_Audit

Cust_ID   Cust_Name Operation_Performed Date_Time
4 Danish Inserted Record -- After Insert Trigger 2011-04-06 19:46:56.390
3 Khan Wasim Updated Record -- After Update Trigger 2011-04-06 20:03:05.367

Now for, "After Delete Trigger":-This trigger fire after a delete operation performed on a table.

In a similar way, you can code "After Delete trigger" on the table.

2.Instead of Triggers:- this trigger fire before the DML operations occur, first inserted and deleted get flourished and then trigger fires

"Instead of Triggers" are further divided into three types
Instead of INSERT Trigger.
Instead of UPDATE Trigger.
Instead of DELETE Trigger.

Let us see the example of "Instead of UPDATE Trigger" for better understanding.

Query:-
CREATE TRIGGER trgInsteadOfUpdate ON Customer 
INSTEAD OF update
AS
declare @cust_id int;
declare @cust_name varchar(100);
declare @cust_salary  int;
select @cust_id=d. Cust_ID from deleted d;
select @cust_name=d. Cust_Name from deleted d;
select @cust_salary =d.Cust_Salary from deleted d;

BEGIN
if(@cust_salary >4500)
begin
RAISERROR('Cannot delete where salary > 4500',16,1);
ROLLBACK;
end
else
begin
delete from Customer where Cust_ID =@cust_id;
COMMIT;
insert into Customer_Audit(Cust_ID,Cust_Name,Cust_Salary,Operation_Performed,Date_Time)
values(@cust_id,@cust_name,@cust_salary,'Updated -- Instead Of Updated Trigger.',getdate());
PRINT 'Record Updated -- Instead Of  Updated Trigger.'
end
END
Now, update a record into "Customer" table:-

Query:- update Customer set Cust_Name = 'Khan Wasim' where Cust_Code like 'A-09'

When you try to update Customer table it will raise an error as we have use Instead of Update trigger.

Error:- Server: Msg 50000, Level 16, State 1, Procedure trgInsteadOfUpdate, Line 15
Cannot update where salary > 4500

In a similar way, you can code "Instead Delete trigger" and "Instead Insert trigger" on the table.

 

Why Arrays are faster than ArrayList?

Answer:
Let's demonstrate an example to prove that how arrays are faster than arraylist for that go to visual studio and create a simple window application with a button and two labels like below diagram.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

Below is the code snippet.

using System.Collections;// import this namespace to access arraylist.
using System.Diagnostics;// import this namespace to access Stopwatch.
namespace ArrayandArrayList
{
    public partial class Form1 : Form
    {
        int[] Array = new int[1000]; // here array is declared.
        ArrayList objArraylist = new ArrayList();// here array list is declared.
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch objWatch = new Stopwatch();
            Stopwatch objWatch1 = new Stopwatch();
            objWatch.Start();
            for(int i=0;i<1000;i++)
            {
                Array[i] = DateTime.Now.Second;
            }
            foreach (int i in Array)
            {

            }
            objWatch.Stop();
            label1.Text = objWatch.ElapsedTicks.ToString();
            objWatch1.Start();
            for (int i = 0; i < 1000; i++)
            {
                objArraylist.Add(DateTime.Now.Second.ToString());
            }
            foreach (object i in objArraylist)
            {
            }
            objWatch1.Stop();
            label2.Text = objWatch1.ElapsedTicks.ToString();
        }
    }
}

In the above code, I have used Stopwatch to record the time taken by the array and  arraylist to performance actions.

The Output look like below diagram.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

Conclusion: -As in ArrayList lots of Boxing and UnBoxing are done therefore its performance is slower than Array because in Array there is no type casting.

What is the difference between Array and ArrayList?

Answer:

Array
 
ArrayList
 
They are fixed length.
 
They are resizable and variable length.
 
They are compiled strong type collection.
 
They are flexible and can accommodate any data types.
 
Because arrays are of fixed size and strong type collection performance is faster.
 
In arraylist lots of boxing and unboxing are done there for its performance is slower.
 

Let's see an example to prove the above differences.

The below is how we declare Array.

String [ ] str = new String [5]; // here you see that the length is fixed as [5] and the data type is also defined as string.

Now, see how you can declare ArrayList.

ArrayList   str = new ArrayList (); // here you see that the length is not fixed and is not tied up with a data type.

If you go to add data to an ArrayList you can see it takes object which is a very generic type.Due to this boxing, unboxing or casting are done.
In other words data moves from stack to heap or heap to stack.If you look at Array as they are strong types boxing and unboxing are completely avoided.
Therefore, Array performance is faster as compared to ArrayList. 

The below diagram gives a better idea of the differences between Array and ArrayList.
 

.net interview questions,c# interview questions,ASP .NET interview questions, SQL server interview questions

What is Render and PreRender?

PreRender: -PreRender is an event which is used for modifying server controls just  before sending them to the client.

Render: -Render is an method which actually puts the HTML output to the response stream.

Lets see an simple example to get an better idea, below is the code snippet for the same.
To use PreRender event, we have to override it and can make neccessary changes to the controls we want to.

protected override void On PreRender (EventArgs e)
{
 foreach (GridViewRow row in GridView1.Rows)
  {
      row.ForeColor = Color.Blue;
  }        
}

In the above code snippet, I have override the PreRender event and just changed the forecolor of the GridView.

Now lets see how we can code for Render. Below is the code snippet for the same, in which I have override the Render and using "HtmlTextWriter" just displayed a simple HTML text output.

protected override void Render (HtmlTextWriter writer)
{
   writer. Write ("This displays the grid in blue color");
   base. Render (writer);
}

Below is the full code snippet so that you can try it by yourself and see the respective result.
 

using System.Drawing;
using System.Collections;

namespace Data
{
    public class Customer
    {
        public string CustomerCode { set; get; }
        public string CustomerName { set; get; }
    }
    public partial class WebForm1 : System.Web.UI.Page
    {
     
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }
        public void LoadGrid()
        {
            ArrayList objArray = new ArrayList();
            Customer ObjCustomer = new Customer();
            ObjCustomer.CustomerCode = "1001";
            ObjCustomer.CustomerName = "shiv";
            objArray.Add(ObjCustomer);
            ObjCustomer = new Customer();
            ObjCustomer.CustomerCode = "1002";
            ObjCustomer.CustomerName = "Feroz";
            objArray.Add(ObjCustomer);
            GridView1.DataSource = objArray;
            GridView1.DataBind();
        }
        protected override void OnPreRender(EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.ForeColor = Color.Blue;
            }
         
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("This displays the grid in blue color");
            base.Render(writer);
        }
    }
}

In the above code snippet, I have used prerender event to change the forecolor of the girdview before the gridview is displayed on the client browser and I have override the render method to display the html output to the response stream.

PreRender event executed before the render method gets called which creates the HTML code using the HtmlWriter.so in other words prerender event fires first and do the necessary changes to the server control and later the render method is called.

In below diagram you can see that the gridview forecolor is been changed because of the prerender event and the render method display the html output using html writer.
.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

How will you design one to one, one to many and many to many relationships in sql server?
 

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.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

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 foreignkey reference to VendorId of Vendor table.

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

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

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.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

 

How will you do code review using FxCop in .NET?

FxCop is a code analyzer tool which runs on .NET dll file it helps you to analyze the quality of .NET code. (Quality in terms of coding standards, best practices etc.)

To view how exactly the FxCop do code review you need to download the FxCop from MSDN site and install it on your local host machine.

As FxCop runs on .NET dll file you need to add the dll file of your project on which you want to do code review.
The below diagram will give you an idea of how will you add the dll file in FxCop.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

 

Once you click on the Add Target button just browse to the dll file you want to do code review like below diagram.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

After adding the dll file to FxCop, Now click on the Analze button and you will see the window like below diagram with the respective result.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

Once you click on analyze button it runs all the rules on the selected dll file and shows that what kind of rules are broken by the dll file.As you see in the above diagram some of the rules are broken by the selected dll file.
You can take these rules and try to close your project structure in standard manner and improve the quality of your project.

 

How will you do Unit Testing using VSTS?

Answer:
Unit testing is validation and verification technology where the developer tests the individual units of source code. These individual units can be functions, methods or class.

Below is the simple Math class which has a Add function with two input parameter. The Add function basically adds the number and gives the addition of these two numbers.

As a developer we would like to test is this Add function actually works properly or not.

namespace TestVSTS
{
public class Math
   {
public int Add(int i, int j)
      {
         int sum;
         sum = i + j;
         return sum;
      }
   }
}

In order to do unit testing by using VSTS the first thing you need to create a simple unit test.In visual studio team system (VSTS) they provide the test menu in which you can select the unit testing as you can see in the below diagrams.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

 

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions
 

 

After selecting unit test open the .cs file and write the logic in [TestMethod] attribute which you want to test.

[TestMethod]
public void Check()
     {
      Math obj = new Math();
      int r = obj.Calculate(10,20);
      Assert.AreEqual(30,r);
     }

Once you have completed the above steps, now set unit test project as startup project, and run the application.
If the function work properly it will gives us Passed result as you see in below diagram.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

If the Unit test gives us Failed result it means something wrong in function as you see in the below diagram.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

How will you do Unit Testing using Nunit?

Answer:
Unit testing is validation and verification technology where the developer tests the individual units of source code. These individual units can be functions, methods or class.

Below is the simple Math class which has a Add function with two input parameter. The Add function basically adds the number and gives the addition of these two numbers.

As a developer we would like to test is this Add function actually works properly or not .

namespace NunitTest
{
    public class Math
    {
        public int Add(int i, int j)
        {
            int sum;
            sum = i + j;
            return sum;
        }
    }
}

In order to do unit testing by using Nunit the first thing you need to create a simple CheckNunit class as shown in the code below.

namespace CheckNunit
{
    [TestFixture]
    public class NunitTest
    {
        [Test]//it is called as attribute
        public void check()
        {
            Math obj = new Math();
            int result = obj.Add(10, 20);
            Assert.AreEqual(30, result);
        }
    }
}

In NunitTest class the check function is attributed by the Test.it is saying that we are passing 10 and 20 value and we are expecting the result as 30.if the result is 30 then everything is working properly else there is something wrong in the code.

Once you have completed the above steps, Open Nunit software and select the .dll file of CheckNunit like the following diagrams.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

Now click on run button and see the respective output. If it displays green color then everything is working fine and if it displays red color then there is something wrong in code. The following two diagram will give you an better idea.

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questionsLet inject a small defect, so that we can check how Nunit display the result. Now I change the Addition(+) sign as Multiplication(*) sign so the method fail to add values and Nunit displays Red signal like below diagram. 

.NET Interview questions , C# Interview questions , ASP.NET Interview Questions , SQL Server interview questions

 

 

 Typical Complicated SQL query asked in .NET interviews to see if you know how to split columns into a row?

Answer:
let's us assume that we have the following table of Employee.

Emp_Id Emp_Name Emp_Salary_2010 Emp_Salary_2011
1
 
Shiv 17000 19000
2
 
Raju
 
13500 15000
3
 
Sham 15000 18000
4
 
Moosa 11000 14000
5
 
Feroz 12000 16000

                     
Now we want to merge the Emp_Salary_2010 and Emp_Salary_2011 columns into a single column as Salary.
Query:-

select Emp_Name,Emp_Salary_2010 as Salary from Employee
union 
select Emp_Name,Emp_Salary_2011 as Salary from Employee

Output:-
 
Emp_Name Salary
Shiv 17000
Shiv 19000
Raju
 
13500
Raju
 
15000
Sham
 
15000
Sham
 
18000
Moosa
 
11000
Moosa
 
14000
Feroz 12000
Feroz 15000

 

Typical Complicated SQL query asked in .NET interviews to test your SQL CASE syntax capability?

Answer:
Let's us assume that we have the following table of Employee.
Emp_Id Emp_Name Emp_Salary
1 Shiv 17000
2 Raju 13500
3 Sham 15000
4 Moosa 11000
5 Firoz 12000

There can be a scenario we have to display employee name whose salary is greater than "some amount" or less than "some amount" for that purpose we use case statement.

Let's us consider that we have to display all the employee names from the employee table and the status like salary is greater than 13000 or lesser than 13000.

Query:
SELECT Emp_Name,CASE 
 when (Emp_Salary>13000) then 'Greater than 13000'
 else 'Lesser than 13000'
 end as Status
 FROM Employee
Output:
Emp_Name Status
Shiv Greater than 13000
Raju Greater than 13000
Sham Greater than 13000
Moosa Lesser than 13000
Firoz Lesser than 13000

Hence you can see that all the employee name have been displayed with their salary status in the above output.

 

Select second highest salary from the table?

Answer:
Let's us assume that we have the following table of Employee.
 

Emp_Id
 
Emp_Name
 
Emp_Salary
 
1
 
Shiv
 
17000
 
2
 
Raju
 
13500
 
3
 
Sham
 
15000
 
4
 
Moosa
 
11000
 
5
 
Feroz
 
12000
 

Now we want to find out second highest salary from Employee table, as you see that the second highest salary is 15000 and we want to display the same.

Query:-

SELECT Emp_Name,Emp_Salary
FROM Employee e1
WHERE
2 = (SELECT COUNT(DISTINCT (e2.Emp_Salary))FROM Employee e2 WHERE e2.Emp_Salary >= e1.Emp_Salary)
The above employee table contains two same records as second highest salary so the result set will look like below output table.

Output:-
Emp_Name Emp_Salary
Sham 15000
        
If the table contains two or more same record of salary which is the second highest salary then the query will give you all the record of second highest salary as you see in the above output table.

 Subscribe to Articles

     

Further Readings:

Responses
Author: Dhiraj Ranka         Company URL: http://www.dotnetspark.com
Posted Date: June 23, 2011

Very well written
Author: souvik         Company URL: http://www.dotnetspark.com
Posted Date: October 27, 2012

very fine and useful post.Carry on to help people like us who are trying to crack interview.
Author: Beverly P F         Company URL: http://www.dotnetspark.com
Posted Date: May 12, 2014

Hi, I read all your 18 .NET articles (Part 1, 2 and 3) and I must say that these tips about our previous role and about the architecture explanation are really priceless, thank you.
But for the rest of this technical questions they are more or less ok, these questions can vary depending on the position and experience that is required. Also a great preparation that I used is by trying out some C# coding tests to brush up my problem solving skills, you can check out them here:
http://testdome.com/dome/Tests/Senior-Csharp-Back-End-Programming-Test


Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend