ASP.Net MVC Interview Questions (ASP.Net Webform)


Preparing for an ASP.NET MVC interview? This guide covers the most important ASP.NET MVC interview questions, including key concepts, differences from ASP.NET WebForms, and best practices to help you succeed. 

Q1. CLR, AOT, and JIT in C#:

  • CLR (Common Language Runtime): Manages code execution, memory, and other system services.
  • AOT (Ahead-Of-Time) Compilation: Compiles code into native machine code before execution.
  • JIT (Just-In-Time) Compilation: Compiles IL code into native machine code at runtime.
// Example of JIT compilation:
// When you run this code, the CLR uses JIT to compile the IL to native code.
int result = Add(3, 4);
Console.WriteLine(result); // Outputs: 7

int Add(int a, int b)
{
    return a + b;
}

Q2. Difference between 'break' and 'continue' in C#:

  • 'break': Exits the loop.
  • 'continue': Skips the rest of the loop body and proceeds to the next iteration.
for (int i = 0; i < 5; i++)
{
    if (i == 2) break; // Exits the loop when i is 2
    Console.WriteLine(i);
}

for (int i = 0; i < 5; i++)
{
    if (i == 2) continue; // Skips the rest of the loop when i is 2
    Console.WriteLine(i);
}

Q3. Tuples in C#:

  • Tuples: Allow you to store multiple values in a single variable.
var person = (Name: "Alice", Age: 30);
Console.WriteLine($"{person.Name} is {person.Age} years old.");

Q4. Classes in C#:

  • Class: A blueprint for creating objects.
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Person person = new Person { Name = "Bob", Age = 25 };

Q5. Types of Classes in C#:

  • Abstract Classes: Cannot be instantiated.
  • Sealed Classes: Cannot be inherited.
  • Static Classes: Cannot be instantiated and contain only static members.
  • Partial Classes: Split across multiple files.
public abstract class Animal { }
public sealed class Dog : Animal { }
public static class Utility { }
public partial class Employee { }

Q6. Constructor in C#:

  • Constructor: Initializes an object.
public class Car
{
    public string Model { get; set; }

    public Car(string model)
    {
        Model = model;
    }
}

Car myCar = new Car("Tesla");


Looking for more ASP.NET MVC resources? Read our  ASP.Net Core MVC Interview Questions to build a strong foundation!

Q7. Types of Constructors in C#:

  • Default Constructor: No parameters.
  • Parameterized Constructor: Takes arguments.
  • Copy Constructor: Copies variables from another object.
  • Static Constructor: Initializes static members.
  • Private Constructor: Prevents instantiation from outside the class.
public class Example
{
    public Example() { } // Default
    public Example(int value) { } // Parameterized
    public Example(Example other) { } // Copy
    static Example() { } // Static
    private Example(bool flag) { } // Private
}

Q8. Encapsulation in C#:

  • Encapsulation: Restricts direct access to some of the object's components.
public class BankAccount
{
    private decimal balance;

    public void Deposit(decimal amount)
    {
        balance += amount;
    }

    public decimal GetBalance()
    {
        return balance;
    }
}

Q9. Real-world Example of Encapsulation:

  • Example: A bank account class that restricts direct access to the account balance.

Q10. Abstraction in C#:

  • Abstraction: Hides the complex implementation details and shows only the essential features.
public abstract class Shape
{
    public abstract double Area();
}

public class Circle : Shape
{
    public double Radius { get; set; }

    public override double Area()
    {
        return Math.PI * Radius * Radius;
    }
}

Q11. Real-world Example of Abstraction:

  • Example: A car's interface (steering wheel, pedals) abstracts the complex mechanics of the engine and transmission.

Q12. Compile-time and Run-time Polymorphism in C#:

  • Compile-time Polymorphism: Method overloading.
  • Run-time Polymorphism: Method overriding.
// Compile-time Polymorphism
public class MathOperations
{
    public int Add(int a, int b) { return a + b; }
    public double Add(double a, double b) { return a + b; }
}

// Run-time Polymorphism
public class Animal
{
    public virtual void Speak() { }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Bark");
    }
}

Q13. Virtual Method in C#:

  • Virtual Method: A method marked with the 'virtual' keyword in a base class.
public class BaseClass
{
    public virtual void Display()
    {
        Console.WriteLine("Base Class Display");
    }
}

public class DerivedClass : BaseClass
{
    public override void Display()
    {
        Console.WriteLine("Derived Class Display");
    }
}

Q14. Boxing and Unboxing in C#:

  • Boxing: Converting a value type to a reference type.
  • Unboxing: Converting a reference type back to a value type.
int number = 123;
object obj = number; // Boxing
int unboxedNumber = (int)obj; // Unboxing

Q15. Difference between Parsing and Converting in C#:

  • Parsing: Converts a string to a data type.
  • Converting: Converts between compatible types.
string str = "123";
int parsed = int.Parse(str); // Parsing
int converted = Convert.ToInt32(str); // Converting

Looking for more ASP.NET MVC resources? Read our  ASP.NET MVC Interview Questions to build a strong foundation!

Q16. Difference between 'ref' and 'out' in C#:

  • 'ref': Passes a variable by reference; must be initialized before passing.
  • 'out': Passes a variable by reference; need not be initialized before passing but must be assigned within the method.
public void RefExample(ref int value)
{
    value = 10;
}

public void OutExample(out int value)
{
    value = 20;
}

int refValue = 5;
RefExample(ref refValue);

int outValue;
OutExample(out outValue);

Q17. Difference between ASP and ASP.NET:

  • ASP: Older scripting environment for dynamic web pages.
  • ASP.NET: Advanced framework for building web applications and services.

Q18. Difference between ASP.NET and ASP.NET MVC:

  • ASP.NET: Uses Web Forms with controls and events.
  • ASP.NET MVC: Uses the Model-View-Controller pattern for separation of concerns.

Q19. Razor View in ASP.NET MVC:

  • Razor: A syntax for embedding server-based code into web pages.
@model MyNamespace.MyModel

@Model.Title

@Model.Description


Q20. Razor Engine in ASP.NET MVC:

  • Razor Engine: Parses Razor syntax and generates HTML output.

Q21. ViewBag in ASP.NET MVC:

  • ViewBag: A dynamic property for passing data from the controller to the view.
public ActionResult Index()
{
    ViewBag.Message = "Hello, World!";
    return View();
}

Q22. ViewData in ASP.NET MVC:

  • ViewData: A dictionary object for passing data from the controller to the view.
public ActionResult Index()
{
    ViewData["Message"] = "Hello, World!";
    return View();
}

Q23. TempData in ASP.NET MVC:

  • TempData: A short-lived data storage mechanism that persists data for the duration of a single request and the subsequent request.
public ActionResult Index()
{
    TempData["Message"] = "Hello, World!";
    return RedirectToAction("About");
}

Q24. Filters in MVC:

  • Filters: Run code before or after specific stages in the request processing pipeline.
public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Log action details
    }
}

Q25. Types of Filters in MVC:

  • Action Filters: Run code before or after an action method is called.
  • Authorization Filters: Determine whether a user is authorized to execute an action.
  • Result Filters: Run code before or after the execution of an action result.
  • Exception Filters: Handle exceptions that occur during the request processing.

Q26. Routing in MVC:

  • Routing: Maps incoming URLs to specific controller actions.
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Q27. Middleware in ASP.NET Core:

  • Middleware: Software that handles requests and responses in the ASP.NET Core pipeline.
public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware();
}

Q28. Exception Handling in ASP.NET:

  • Exception Handling: Catching and managing exceptions using try-catch blocks.
try
{
    // Code that may throw an exception
}
catch (Exception ex)
{
    // Handle the exception
}

Q29. Error Handling in ASP.NET:

  • Error Handling: Managing errors and providing user-friendly messages.
public ActionResult Index()
{
    try
    {
        // Code that may cause an error
    }
    catch (Exception ex)
    {
        ViewBag.ErrorMessage = ex.Message;
    }
    return View();
}

Q30. Exception Middleware in ASP.NET Core:

  • Exception Middleware: Catches and handles exceptions in the request pipeline.
public void Configure(IApplicationBuilder app)
{
    app.UseExceptionHandler("/Home/Error");
}

Q31. Difference between Exception Middleware and Custom Exception Middleware:

  • Exception Middleware: Built-in middleware for handling exceptions.
  • Custom Exception Middleware: Custom-built middleware tailored to specific application needs.

Q32. Request Pipeline in ASP.NET Core:

  • Request Pipeline: The sequence of middleware components that handle HTTP requests and responses.
public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware();
    app.UseMiddleware();
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!