.NET Interview Questions and Answers for .NET, Web API, and SQL:



1. What is the use of Startup.cs and Program.cs in .NET?

  • Program.cs: Entry point of the application. Builds and runs the host.

  • Startup.cs: Configures services (via ConfigureServices) and the HTTP request pipeline (via Configure).


2. What is middleware in .NET Core?
Middleware is software that handles requests and responses in the HTTP pipeline. Examples: authentication, routing, exception handling.


3. What is the difference between Authentication and Authorization?

  • Authentication: Verifies who the user is.

  • Authorization: Determines what the user can access.


4. How to authenticate Web API?
Common methods include:

  • JWT (JSON Web Token)

  • OAuth2

  • API Keys

  • Cookie-based (less common for APIs)

Use [Authorize] attribute with appropriate schemes.


5. What are HTTP verbs in Web API?

  • GET: Retrieve data

  • POST: Create data

  • PUT: Update/replace data

  • PATCH: Update part of data

  • DELETE: Remove data


6. How to make Web API accept only JSON?
In Startup.cs or Program.cs, remove other formatters:

services.AddControllers()
    .AddJsonOptions(options => { /* settings */ })
    .AddXmlDataContractSerializerFormatters(); // Remove this line

7. What is Dependency Injection and why do we use it?
A design pattern for achieving Inversion of Control (IoC).
We use it to promote modularity, testability, and manage object lifetimes.


8. How to remove duplicate rows in SQL table?
Using CTE with ROW_NUMBER():

WITH CTE AS (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY column1, column2 ORDER BY id) AS rn
  FROM TableName
)
DELETE FROM CTE WHERE rn > 1;

9. Why is TRUNCATE faster than DELETE?

  • TRUNCATE deallocates data pages, minimal logging.

  • DELETE logs row-by-row, maintains integrity constraints.


10. What is ACID in SQL?

  • Atomicity: All or nothing

  • Consistency: Maintains DB rules

  • Isolation: Transactions don’t affect each other

  • Durability: Once committed, changes persist


11. How to improve performance of a stored procedure?

  • Use proper indexing

  • Avoid cursors

  • Use SET NOCOUNT ON

  • Avoid unnecessary calculations and joins

  • Use temp tables or table variables wisely


12. What is the MVC life cycle in ASP.NET?

  1. Routing

  2. Controller Initialization

  3. Action Execution

  4. Result Execution (e.g., ViewResult)

  5. Response to Client



Comments

Popular posts from this blog

API development using .NET Core - ProductManagmentCRUD

Singleton Design Pattern Realtime Example of banking

Consume API in MVC Application