Expert Dot Net

Trust me to find new way !

ASP.NET MVC Interview Questions

Q1 - What is MVC (Model view controller)?

 Answer

Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts, so as to separate internal representation of information from the way that information is presented to or accepted from the user.

MVC is a framework for building web applications using a MVC (Model View Controller) design:

The Model represents the application core (for instance a list of database records).

The View displays the data (the database records).

The Controller handles the input (to the database records).

The MVC model also provides full control over HTML, CSS, and JavaScript.




The MVC model defines web applications with 3 logic layers,

The business layer (Model logic)

The display layer (View logic)

The input control (Controller logic)


The Model is the part of the application that handles the logic for the application data.

Often model objects retrieve data (and store data) from a database.



The View is the part of the application that handles the display of the data.

Most often the views are created from the model data.



The Controller is the part of the application that handles user interaction.

Typically controllers read data from a view, control user input, and send input data to the model.

The MVC separation helps you manage complex applications, because you can focus on one aspect a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application.

The MVC separation also simplifies group development. Different developers can work on the view, the controller logic, and the business logic in parallel.

 

Q2. Explain in which assembly is the MVC framework is defined?


Answer

The MVC framework is defined in System.Web.Mvc.


Q4. List out few different return types of a controller action method?

Answer

View Result

Javascript Result

Redirect Result

Json Result

Content Result


Q5.  Mention what is the advantages of MVC?


Answer

MVC segregates your project into a different segment, and it becomes easy for developers to work on

It is easy to edit or change some part of your project that makes project less development and maintenance cost

MVC makes your project more systematic

 

Q6. Explain the role of components Presentation, Abstraction and Control in MVC?


Answer

Presentation: It is the visual representation of a specific abstraction within the application

Abstraction: It is the business domain functionality within the application

Control: It is a component that keeps consistency between the abstraction within the system and their presentation to the user in addition to communicating with other controls within the system


Q7- Explain MVC application life cycle?


Answer

 Any web application has two main execution steps, first understanding the request and depending on the type of the request sending out appropriate response. MVC application life cycle is not different it has two main phases, first creating the request object and second sending our response to the browser.


Creating the request object,

The request object creation has four major steps. The following is the detailed explanation of the same.

Step 1 - Fill route

MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.

Step 2 - Fetch route

Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.

Step 3 - Request context created

The “RouteData” object is used to create the “RequestContext” object.

Step 4 - Controller instance created 

This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.

Creating Response object

This phase has two steps executing the action and finally sending the response as a result to the view.




Q8 - List out different return types of a controller action method?

 

Answer

 

There are total nine return types we can use to return results from controller to view.


The base type of all these result types is ActionResult.

ViewResult (View)
This return type is used to return a webpage from an action method.

PartialviewResult (Partialview)
This return type is used to send a part of a view which will be rendered in another view.

RedirectResult (Redirect)
This return type is used to redirect to any other controller and action method depending on the URL.

RedirectToRouteResult (RedirectToAction, RedirectToRoute)
This return type is used when we want to redirect to any other action method.

ContentResult (Content)
This return type is used to return HTTP content type like text/plain as the result of the action.

jsonResult (json)
This return type is used when we want to return a JSON message.

javascriptResult (javascript)
This return type is used to return JavaScript code that will run in browser.

FileResult (File)
This return type is used to send binary output in response.

EmptyResult
This return type is used to return nothing (void) in the result.

 

 

Q9. What is Razor in MVC?

Answer

 

ASP.NET MVC has always supported the concept of “view engines” – which are the pluggable modules, which practically implement different template syntax options. The “default” view engine for ASP.NET MVC uses the same .aspx/.ascx/. master file templates as ASP.NET Web Forms. Other popular ASP.NET MVC view engines are Spart&Nhaml. Razor is the new view engine introduced by MVC 3.

 

Q10. Explain what is Database First Approach in MVC using Entity Framework?

Answer

 

Database First Approach is an alternative or substitutes to the Code First and Model First approaches to the Entity Data Model. The Entity Data Model creates model codes (classes, properties, DbContext, etc.) from the database in the project and that class behaves as the link between database and controller.

There are the following approaches, which are used to connect the database with the application.

Database First

Model First

Code First

What is CPU Scheduling?

CPU scheduling is a process which allows one process to use the CPU while the execution of another process is on hold(in waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. The aim of CPU scheduling is to make the system efficient, fast and fair.

Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The selection process is carried out by the short-term scheduler (or CPU scheduler). The scheduler selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them.


Types of CPU Scheduling

CPU scheduling decisions may take place under the following four circumstances:

  1. When a process switches from the running state to the waiting state(for I/O request or invocation of wait for the termination of one of the child processes).
  2. When a process switches from the running state to the ready state (for example, when an interrupt occurs).
  3. When a process switches from the waiting state to the ready state(for example, completion of I/O).
  4. When a process terminates.


CPU Scheduling: Dispatcher

Another component involved in the CPU scheduling function is the Dispatcher. The dispatcher is the module that gives control of the CPU to the process selected by the short-term scheduler. This function involves:


What is a Deadlock?

Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.


Consider an example when two trains are coming toward each other on same track and there is only one track, none of the trains can move once they are in front of each other. Similar situation occurs in operating systems when there are two or more processes hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.



How to avoid Deadlocks

Deadlocks can be avoided by avoiding at least one of the four conditions, because all this four conditions are required simultaneously to cause deadlock.

  1. Mutual Exclusion

    Resources shared such as read-only files do not lead to deadlocks but resources, such as printers and tape drives, requires exclusive access by a single process.

  2. Hold and Wait

    In this condition processes must be prevented from holding one or more resources while simultaneously waiting for one or more others.

  3. No Preemption

    Preemption of process resource allocations can avoid the condition of deadlocks, where ever possible.

  4. Circular Wait

    Circular wait can be avoided if we number all resources, and require that processes request resources only in strictly increasing(or decreasing) order.


Handling Deadlock

The above points focus on preventing deadlocks. But what to do once a deadlock has occurred. Following three strategies can be used to remove deadlock after its occurrence.

  1. Preemption

    We can take a resource from one process and give it to other. This will resolve the deadlock situation, but sometimes it does causes problems.

  2. Rollback

    In situations where deadlock is a real possibility, the system can periodically make a record of the state of each process and when deadlock occurs, roll everything back to the last checkpoint, and restart, but allocating resources differently so that deadlock does not occur.

  3. Kill one or more processes

    This is the simplest way, but it works.

The Critical Section Problem

Critical Section is the part of a program which tries to access shared resources. That resource may be any resource in a computer like a memory location, Data structure, CPU or any IO device.

The critical section cannot be executed by more than one process at the same time; operating system faces the difficulties in allowing and disallowing the processes from entering the critical section.

The critical section problem is used to design a set of protocols which can ensure that the Race condition among the processes will never arise.

In order to synchronize the cooperative processes, our main task is to solve the critical section problem. We need to provide a solution in such a way that the following conditions can be satisfied.







Primary

Mutual Exclusion

Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if one process is executing inside critical section then the other process must not enter in the critical section.

Progress

Progress means that if one process doesn't need to execute into critical section then it should not stop other processes to get into the critical section.

Secondary

Bounded Waiting

We should be able to predict the waiting time for every process to get into the critical section. The process must not be endlessly waiting for getting into the critical section.

Architectural Neutrality

Our mechanism must be architectural natural. It means that if our solution is working fine on one architecture then it should also run on the other ones as well.

Memory Organization in Computer Architecture

Memory Hierarchy Design and its Characteristics

In the Computer System Design, Memory Hierarchy is an enhancement to organize the memory such that it can minimize the access time. The Memory Hierarchy was developed based on a program behaviour known as locality of references. The figure below clearly demonstrates the different levels of memory hierarchy:

 

This Memory Hierarchy Design is divided into 2 main types:

External Memory or Secondary Memory –
Comprising of Magnetic Disk, Optical Disk, Magnetic Tape i.e. peripheral storage devices which are accessible by the processor via I/O Module.

Internal Memory or Primary Memory –
Comprising of Main Memory, Cache Memory & CPU registers. This is directly accessible by the processor.

We can infer the following characteristics of Memory Hierarchy Design from above figure:

Capacity:
It is the global volume of information the memory can store. As we move from top to bottom in the Hierarchy, the capacity increases.

Access Time:
It is the time interval between the read/write request and the availability of the data. As we move from top to bottom in the Hierarchy, the access time increases.

Performance:
Earlier when the computer system was designed without Memory Hierarchy design, the speed gap increases between the CPU registers and Main Memory due to large difference in access time. This results in lower performance of the system and thus, enhancement was required. This enhancement was made in the form of Memory Hierarchy Design because of which the performance of the system increases. One of the most significant ways to increase system performance is minimizing how far down the memory hierarchy one has to go to manipulate data.

Cost per bit:
As we move from bottom to top in the Hierarchy, the cost per bit increases i.e. Internal Memory is costlier than External Memory.

 

Producer Consumer problem

the producer–consumer problem is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate data, put it into the buffer, and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer), one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer.

The solution for the producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores. An inadequate solution could result in a deadlock where both processes are waiting to be awakened. The problem can also be generalized to have multiple producers and consumers.


Inadequate implementation


To solve the problem, some programmer might come up with a solution shown below. In the solution two library routines are used, sleep and wakeup. When sleep is called, the caller is blocked until another process wakes it up by using the wakeup routine. The global variable itemCount holds the number of items in the buffer.

int itemCount = 0;

 

procedure producer()

{

    while (true)

    {

        item = produceItem();

 

        if (itemCount == BUFFER_SIZE)

        {

            sleep();

        }

 

        putItemIntoBuffer(item);

        itemCount = itemCount + 1;

 

        if (itemCount == 1)

        {

            wakeup(consumer);

        }

    }

}

 

procedure consumer()

{

    while (true)

    {

 

        if (itemCount == 0)

        {

            sleep();

        }

 

        item = removeItemFromBuffer();

        itemCount = itemCount - 1;

 

        if (itemCount == BUFFER_SIZE - 1)

        {

            wakeup(producer);

        }

 

        consumeItem(item);

    }

}

Introduction to Operating Systems

A computer system has many resources (hardware and software), which may be require to complete a task. The commonly required resources are input/output devices, memory, file storage space, CPU etc. The operating system acts as a manager of the above resources and allocates them to specific programs and users, whenever necessary to perform a particular task.  it can manage the resource of a computer system internally. The resources are processor, memory, files, and I/O devices. In simple terms, an operating system is the interface between the user and the machine.