Expert Dot Net

Trust me to find new way !

C Language Overview


C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard.

The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons −


  • Easy to learn
  • Structured language
  • It produces efficient programs
  • It can handle low-level activities
  • It can be compiled on a variety of computer platforms


Facts about C

  • C was invented to write an operating system called UNIX.
  • C is a successor of B language which was introduced around the early 1970s.
  • The language was formalized in 1988 by the American National Standard Institute (ANSI).
  • The UNIX OS was totally written in C.
  • Today C is the most widely used and popular System Programming Language.
  • Most of the state-of-the-art software have been implemented using C.
  • Today's most popular Linux OS and RDBMS MySQL have been written in C.


Why use C?

C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C might be −


  • Operating System
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • Databases
  • Language Interpreters
  • Utilities

C Programs

A C program can vary from 3 lines to millions of lines and it should be written into one or more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any other text editor to write your C program into a file.

This tutorial code inside a program file.assumes that you know how to edit a text file and how to write source. 

C Programming Exercises, Practice, Solution : For Loop

1. Write a program in C to display the first 10 natural numbers. 
Expected Output :
1 2 3 4 5 6 7 8 9 10

2. Write a C program to find the sum of first 10 natural numbers. 
Expected Output : 
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10 
The Sum is : 55

3. Write a program in C to display n terms of natural number and their sum.
Test Data : 7 
Expected Output : 
The first 7 natural number is : 
1 2 3 4 5 6 7 
The Sum of Natural Number upto 7 terms : 28 

4. Write a program in C to read 10 numbers from keyboard and find their sum and average. 
Test Data :
Input the 10 numbers : 
Number-1 :2 
... 
Number-10 :2 
Expected Output : 
The sum of 10 no is : 55 
The Average is : 5.500000

5. Write a program in C to display the cube of the number upto given an integer. 
Test Data :
Input number of terms : 5 
Expected Output : 
Number is : 1 and cube of the 1 is :1 
Number is : 2 and cube of the 2 is :8 
Number is : 3 and cube of the 3 is :27 
Number is : 4 and cube of the 4 is :64 
Number is : 5 and cube of the 5 is :125

6. Write a program in C to display the multiplication table of a given integer. 
Test Data :
Input the number (Table to be calculated) : 15 
Expected Output : 
15 X 1 = 15 
...
... 
15 X 10 = 150

7. Write a program in C to display the multiplication table of a given integer. 
Test Data :
Input the number (Table to be calculated) : 15 
Expected Output : 
15 X 1 = 15 
...
... 
15 X 10 = 150

8. Write a program in C to make such a pattern like right angle triangle with a number which will repeat a number in a row. 

The pattern like :

 1
 22
 333
 4444

9. Write a program in C to display the n terms of odd natural number and their sum . 
Test Data 
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19 
The Sum of odd Natural Number upto 10 terms : 100 

10. Write a program in C to display the pattern like right angle triangle using an asterisk. 

The pattern like :

*
**
***
****

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.



OOP Features

Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic. 

OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects.

  1. The software is divided into a number of small units called objects. The data and functions are built around these objects.
  2. The data of the objects can be accessed only by the functions associated with that object.
  3. The functions of one object can access the functions of another object.

OOP has the following important features.


Class

A class is the core of any modern Object Oriented Programming language such as C#.

In OOP languages it is mandatory to create a class for representing data.

A class is a blueprint of an object that contains variables for storing data and functions to perform operations on the data.

A class will not occupy any memory space and hence it is only a logical representation of data.

To create a class, you simply use the keyword "class" followed by the class name:

Class Employee {

}


Object

Objects are the basic run-time entities of an object oriented system. They may represent a person, a place or any item that the program must handle. 

"An object is a software bundle of related variable and methods."

"An object is an instance of a class" 


A class will not occupy any memory space. Hence to work with the data represented by the class you must create a variable for the class, that is called an object.

When an object is created using the new operator, memory is allocated for the class in the heap, the object is called an instance and its starting address will be stored in the object in stack memory.

When an object is created without the new operator, memory will not be allocated in the heap, in other words an instance will not be created and the object in the stack contains the value null.

When an object contains null, then it is not possible to access the members of the class using that object.

Class Employee {

}

Syntax to create an object of class Employee:

Employee objEmp = new Employee();  


All the programming languages supporting Object Oriented Programming will be supporting these three main concepts, 

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Abstraction

Abstraction is "To represent the essential feature without representing the background details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Real-world Example of Abstraction

Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as in the following: 

Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (necessary and common information) for the object "Mobile Phone" is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following,


abstract class MobilePhone {  

    public void Calling();  

    public void SendSMS();  

}  

public class Nokia1400: MobilePhone {}  

public class Nokia2700: MobilePhone {  

    public void FMRadio();  

    public void MP3();  

    public void Camera();  

}  

public class BlackBerry: MobilePhone {  

    public void FMRadio();  

    public void MP3();  

    public void Camera();  

    public void Recording();  

    public void ReadAndSendEmails();  

}  


Abstraction means putting all the variables and methods in a class that are necessary.

For example: Abstract class and abstract method.

Abstraction is a common thing.

Example

If somebody in your collage tells you to fill in an application form, you will provide your details, like name, address, date of birth, which semester, percentage you have etcetera.

If some doctor gives you an application to fill in the details, you will provide the details, like name, address, date of birth, blood group, height and weight.

See in the preceding example what is in common?

Age, name and address, so you can create a class that consists of the common data. That is called an abstract class.

That class is not complete and it can be inherited by other classes.

Encapsulation

Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.

  1. class Bag {  
  2.     book;  
  3.     pen;  
  4.     ReadBook();  
  5. }  

Encapsulation means hiding the internal details of an object, in other words how an object does something.

Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from another object.

Hide the data for security such as making the variables private, and expose the property to access the private data that will be public. 

So, when you access the property you can validate the data and set it.

Example 1

  1. class Demo {  
  2.     private int _mark;  
  3.     public int Mark {  
  4.         get {  
  5.             return _mark;  
  6.         }  
  7.         set {  
  8.             if (_mark > 0) _mark = value;  
  9.             else _mark = 0;  
  10.         }  
  11.     }  
  12. }  

Real-world Example of Encapsulation

Let's use as an example Mobile Phones and Mobile Phone Manufacturers.
Suppose you are a Mobile Phone Manufacturer and you have designed and developed a Mobile Phone design (a class). Now by using machinery you are manufacturing Mobile Phones (objects) for selling, when you sell your Mobile Phone the user only learns how to use the Mobile Phone but not how the Mobile Phone works.

This means that you are creating the class with functions and by with objects (capsules) of which you are making available the functionality of your class by that object and without the interference in the original class.

Example 2

TV operation

It is encapsulated with a cover and we can operate it with a remote and there is no need to open the TV to change the channel. 
Here everything is private except the remote, so that anyone can access the remote to operate and change the things in the TV.

Inheritance

When a class includes a property of another class it is known as inheritance.

Inheritance is a process of object reusability.

For example, a child includes the properties of its parents.

  1. public class ParentClass {  
  2.     public ParentClass() {  
  3.         Console.WriteLine("Parent Constructor.");  
  4.     }  
  5.     public void print() {  
  6.         Console.WriteLine("I'm a Parent Class.");  
  7.     }  
  8. }  
  9. public class ChildClass: ParentClass {  
  10.     public ChildClass() {  
  11.         Console.WriteLine("Child Constructor.");  
  12.     }  
  13.     public static void Main() {  
  14.         ChildClass child = new ChildClass();  
  15.         child.print();  
  16.     }  
  17. }  

Output

Parent Constructor.
Child Constructor.
I'm a Parent Class.

Polymorphism

Polymorphism means one name, many forms.

One function behaves in different forms.

In other words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism

Example 1

A teacher behaves students.

A teacher behaves his/her seniors.

Here teacher is an object but the attitude is different in different situations.

Example 2

A person behaves the son in a house at the same time that the person behaves an employee in an office.

Example 3

Your mobile phone, one name but many forms:

  • As phone
  • As camera
  • As mp3 player
  • As radio

To read about Polmorphism in detail click the following link: