Translate to your language...

6 October 2015

Most Interesting Questions in C#, ASP.Net and SQL Server, WCF, ASP.Net MVC

Hello Guys,
In this article, we will see the most interesting questions in Microsoft Technologies like in C#, ASP.Net, Sql Server, WCF services, ASP.Net MVC.
Q 1. I have logged in to the ASP.Net website using my credential for the login page. Now I want that when my session is expired, it should logout the application and show the login page. With that It should also logged out from the Windows and I should use the Windows credentials again to reach to my application again. How do you think it can be done?
Ans. This question is related to the Sessions in ASP.Net where the session time out is configured for the current user. And when the session gets expired, we need to set so that it should automatically come back to login page and user need to enter the credentials again.

Session time can be set as:

Q 2. In .Net Framework, Garbage collector is used for the Automatic Memory Management(to free-up the memory which is no longer used by the resources). There are 2 types of memory- the memory which is created using value types(Stack memory) and the Memory created using Reference Types(Heap Memory). Which of these memory will be disposed by the Garbage Collector?
Ans. Garbage Collector is used to free-up the memory of unreferenced objects, the objects which are no longer used by the resources.
Garbage collector only disposes the memory created by using the Reference Types means the objects which are in Heap Memory. The objects which are created using the Value Types will get cleared when they are out of Stack. Stack is the type where the value types gets push when they are created and get popped when they are used. So when the variable is used by the program and gets out of the scope, it will get popped up from the stack.
The Garbage collector will no take care of the stack objects.

Q 3. Abstraction is used to display only essential and necessary features of the Object to the outside world and hiding the unnecessary details. Abstract Classes and Interfaces can be used to achieve abstraction. 
Can I create the Constructor for an Abstract Class? 
Can I write Static methods inside Abstract class?? 
Can I inherit an Abstract class to Another Abstract class? If so, then if we can have an abstract method in the abstract base class the how the child abstract class will behave for the overriding of this method? Can I override the base class abstract method to the abstract child class? If so then do you think, Abstract class support override methods as we are now trying to implement the overriding in the child class which is abstract.
Ans. Abstraction is the way to expose the objects to the outside world as per our requirements. For example, using the public and private members of the class. If we don't want to expose the variable or method which is inside the class,m make them private. Abstraction is the compile time where the compiler gets behaviour of the members during the compilation of the program.
Abstract classes and interfaces are also the ways to achieve abstraction as when we define the Abstract members inside the abstract class or in Interface, we are exposing these members to outside world to use them by their implementation.
Yes, Constructor can be created for the abstract classes. The abstract class works based on the child class memory so when the object of the child class get created, it will call first the abstract class constructor and then then it will call the child class constructor.
Yes,  We can write the Static members(Methods and Properties) inside the abstract class where
 the static members can be called based on the class name. ClassName.MethodName(), ClassName.PropertyName etc.
Yes, Inheritance between the abstract classes is possible where we have both base class and inherited class both are abstract classes. So if we have an abstract method in the abstract base class and then we are inheriting this base class to the abstract child class, the abstract method can be overridden in the child abstract class but it is not mandatory to implement it there. We can also implement the abstract base class method to the last child class. So we can write the virtual, abstract as well as override methods inside the abstract class.

Q.4 Can we write properties inside interfaces? If so then what is the syntax to write a property in the Interface if the name of the property is ID and return type is integer?
Ans. Yes, We can define the public properties inside the Interface but as in interface all the members are public so we don't need to define the property as public.
Syntax: int ID {get; set;}

Q. 5 By default all the members of the Interface are public abstract. Can we write explicitly the 'Public' for the method? Do you think now my program will compile or will give a warning message? If it will give the warning message, then what the text for the warning message. If it will give the compilation error then what error you will get?
Ans. No, We can't write the explicitely 'Public' access specifier inside the interface. If we write the 'public' access specifier inside the interface, it will throw error during the compilation of the program that 'no access specifier are allowed inside the interface'.

3 October 2015

ASP.Net MVC related questions

Hey Friends,
Now a days we can see lot of companies asking about the ASP.net MVC related questions in their interviews. So i thought of to keep few questions which are related to the ASP.Net MVC and they will be helpful for all of them who are going for the interviews or preparing for the ASP.Net MVC.
Here I kept many questions which are related to all the versions of ASP.Net MVC to keep in mind that interviewer can ask the questions from various aspects.

1.    What is MVC? 
Ans. MVC (Model View Controller) is one of the Software Architectural pattern which is built on 3 layers. So it is the pattern which basically talks about the separation of concern for a project. Here the 3 parts are having their own roles like:
a. Model - It is used as the Data Container or it can also have the business rules, functions, logic's etc.
b. View - It is mainly the UI (User Interface) part which is the main application built using the Web technologies like ASP.Net etc. It consists of the User Interface Design, representation of the information for the end users etc.
c. Controller - It is the mediator between the View and Model and used to control the requests and responses floating through the views and Model. The command, Actions, Events are the part of Controller in MVC. All the communication is done via Controller so we can say that Controller is the heart of MVC.

2.    What is ASP.Net MVC? 
Ans. ASP.Net MVC is the Microsoft product based on the core MVC pattern. The Microsoft has taken the core concept of MVC and Designed the ASP.Net MVC structure for the Visual Studio which helps the developers to get the automated structure and on it, they can built the applications.
ASP.Net MVC provides the structure with the separation of concerns where each part of ASP.Net MVC application is separate and has no link among them. Apart from the separation of concerns, the Microsoft has provided the way to communicate between the separate components by using the other additional logic and libraries. These libraries supports in the connection, communication between the components, databases and other external components as well with small effort.

3.    How ASP.Net MVC is different than MVC?
Ans. ASP.Net MVC is the inherited form of Core MVC where Microsoft has designed their MVC patterns based on the original core MVC concept. The Microsoft also provided the new MVC structure (ASP.Net MVC) which has the inbuilt capabilities of communication with their components, connection with the database etc. The ASP.Net MVC has the good support for creating mobile application. We can create the Mobile applications in ASP.Net MVC using the predefined template.

4.    What are the new features of ASP.Net MVC 2?
Ans. There are few major changes happened in the version MVC2. The Microsoft has more concentrated on the HTML Helpers, Validations support, Async controllers etc.Below are the few point which distinguishes the ASP.Net MVC 2 different than the previous versions:
a. New HTML Helpers with Strongly Typed
b. Model based validations- This is very useful feature where the validation is done in the Model and will be used throughout the application.
c. Automatic scaffolding feature also got introduces with this version.
d. Asynchronous controller support which enables the multiple tasks to run simultaneously.
e. Html.RenderAction introduced to render the section/part of the page.

5.    Explain MVC Architecture?
Ans. MVC architecture comes with 3 major components-Model, View and Controller.










The main objective of the MVC design pattern is to separate the logic with the UI. So the separation of Concerns is one of the biggest advantage of the MVC design pattern where the UI is separate with business logic.
The user sends the request and the request is taken by the Controller which is heart of the MVC model and takes care of all the actions and events happens in the View. The controller checks whether the request required the Model intervention and if so then call the model to serve the database operations and get the result back. Based on the Model data, the View will be populated. So the Model and View are nothing but the mapping of each other. View is always gets populated based on the Model and not rendered.

6.    What are the new features of ASP.Net MVC 3?
Ans. a. Scaffolding option for the speedup process
       b. Razor engine with the new razor syntax for good readability of the View code.
       c. HTML 5 support
       d. Model based validations
       e. Model Binders to bind the model with View
       f. Action filters
       g. Nuget enabled
       h. Output caching for partial page
       
7.    What are the new features of ASP.Net MVC 4?
Ans. a. Mobile support by adding a new Mobile Project template
       b. ASP.net Web API added for the HTTP requests
       c. Task class support with Async methods
       d. Bundling and Magnification
       e. Login with the social networking websites
       f. Support of Windows Azure SDK
       g. Display modes for the webpage and mobile layout page
       
8.    What are the new features of ASP.Net MVC 5?
Ans. a. One ASP.Net for all type of Web templates
       b. Bootstrap enabled for the ASP.net MVC 5
       c. New Authentication Filter for Controller/Controllers
       d. Attribute Routing
       e. New Scaffolding option
       f. Filter overrides feature
       
9.    Explain ASP.Net MVC application life cycle?
Ans. The application life cycle of ASP.net MVC includes the request, processing, filtering and execution and then finally the response back to the requester. The request first goes to URL routing module(HTTP Module) and there it gets decided that whether this is MVC request and will be handled or not. Now if the request is for MVC application, the URL routing module selects the first route which is satisfying the requested URL. As soon as the first match found for the requested URL, the process for the execution begins with the Controller and Model(if any) and the response sends back to the browser.

10. Advantages of MVC Framework?
Ans. Below are the advantages of MVC framework:
       a. Separation of Concerns
       b. Light weight
       c. No round trip for the layers
       d. Decoupling between the layers
       e. No dependencies between the Model, View and Controller  
       
11. What do you mean by Separation of Concerns?
Ans. Separation of concerns means all the three components (Model, View and Controller) are not dependent to each other. We can have a single controller which can server the request and can use any number of Views to populate.

12. Where do we see Separation of Concerns in MVC?
Ans. We can see the separation of concern when we add/remove a View, it will not impact on rest of the application because its all depends on when you call, then only it will be used. if there is no call for the View, the application can just keep the view or model with no dependencies.

13. What is Razor View Engine in ASP.Net MVC 3?
Ans. Razor View engine is the new engine which the Microsoft has introduces with the ASP.Net MVC 3. So from ASP.net MVC 3 onward, we can see the ASP.net MVC application will support ASPX as well as Razor view engine. So when we use .aspx to develop our Views, it will use the ASPX engine to execute and when we use the Razor Views(.cshtml or .vbhtml), then it will use the Razor view engine to execute them.

14. Explain the advantages of using routing in ASP.NET MVC?
Ans. There are many advantages of using routing mechanism in ASP.Net MVC like:
a. To keep the URL clean- as in MVC, the default format for the URL is "http://www.myweb.com/Customer/GetCustomer/10". This URL shows that GetCustomer will give the customer details for the id as 10.
b. Discovering the URL by End User- The end user can easily discover the URL details like in above URL, the end user can discover that this URL will give the Customer details for the customer id 10.
c. Avoiding the database id's in URL- with this, we can avoid the id's which are defined in the database.

15. What are the things that are required to specify a route ? Which is a better fit, Razor or ASPX?
Ans. RouteConfig.cs is the class used to define the routes. This file is placed under the App_Start folder. The file RouteConfig.cs contains one default route, however the programmer can define other routes as per their requirements.


To Specify a route, we need Name and URL of the route. The default URL contains the Controller Name, Action name and the id(if required). By this route, the actual URL gets mapped and if it maps correctly, the view will get displayed, else it shows the 404 error (file not found).


16. How can you do authentication and authorization in ASP.Net MVC?
Ans. Windows and Form Authentication are available for the Authentication & Autherization in ASP.Net MVC.

17. How to implement Windows authentication for ASP.Net MVC?
Ans.  To Implement the Windows Authentication, it is similar to other application where we can define the authentication mechanism in the config file. In the same way, we can implement the Windows Authentication & Authorization using the below snippet:
Now, we can retrict the particualr use to access the controller as below:


We can also restrict the action methods by using the same attribute.

18. How do you implement Forms authentication in ASP.Net MVC? Mention some of the return types of a controller action method.
Ans. The Form Authentication implementation is similar to the ASP.Net form authentication where we need to check the user credentials in each forms to validate the user and then provie the access.
For this, we need to first define the Form Authentication Mode in Web.Config file as:






Now once we define the Form Authentication, we can check the user and set the cookies as:

We also need to include the "using System.Web.Security;" as namespace to get the FormAuthentication class.
The above code is checking the user credentials and then setting it in AuthCookie. Now onwards wherver we want to check for the Form Authentication, we just need to add the [Autherize] attrivute to the action methods as:


19. Is ASP.Net MVC suitable for both Windows and web applications?
Ans. No, ASP.Net MVC is primarility made for the Web Applications development. For the Windows Application, MVP is most suitable.

20. What are the benefits of using ASP.Net MVC?
Ans.  The major benefits using ASP.Net MVC are:
a. Separation of concern- separate code behind file
b. Automated testing - testing of the logic with no UI

21. Is MVC different from a three layered architecture?
Ans. Yes, MVC is different than the 3 layered architecture in terms of UI layer(no code behind or separate code behind in MVC), the communication between layers, routing mechanisms etc.
In 3 Layered architecture:
UI <------> Business logic Layer <-------> Data Access Layer
In MVC:
Controller -----> Model
     |                      |
UI(View) <-------

22. What is the latest version of ASP.Net MVC?
Ans.  The current version of ASP.Net MVC is 6. This version of ASP.Net MVC is also called as ASP V-Next which is the common framework to develop MVC, Web Pages, Web API, Signal R applications. Also it can be self hosted and not depends on IIS for hosting. 

23. What is the difference between each version of ASP.Net MVC?
Ans. Below are the features in each of the version in ASP.Net MVC:

ASP.Net MVC 2
a. Async Controller
b. DefaultValueAttribute in Action Method
c. Area concept
d. Bind binary data with the Model binder
e. Model based validations
f. DataAnnotations support
g. Html.ValidationSummary helper method

ASP.Net MVC 3
a. Razor syntax and Razor engine
b. Support for HTML 5
c. Automatic templates available for the Create, List, Edit, View via Scaffolding

ASP.Net MVC 4
a. Web API introduction under ASP.Net MVC
b. Mobile project templates
c. Bundling & Minification
d. Task support for Asynchronous controller
e. Azure SDK
f. Enable login using 3rd party sites like Facebook, Google, Twitter etc by OAuth & OpenId

ASP.Net MVC 5
a. Attribute based routing
b. One ASP.Net
c. Bootstrap in MVC Templates auto-enabled
d. Authentication Filters
e. Filter Overrides

ASP.Net MVC 6
a.  Merging of ASP.Net MVC and Web API
b. Inbuilt DI(Dependency Injection)
c. JSON based project structure(Added JSON)
d. Nuget package based
e. Auto-build by save using Roslyn compiler.
f. Open Source(Runs on Mono, Linux & Mac)

24. What are HTML helpers in MVC?
Ans.  To render HTML controls in View, HTML Helpers are used.
e.g. Html.TextBox("Address")

25. What is the difference between "HTML.TextBox" vs "HTML.TextBoxFor" in ASP.Net MVC?
Ans. Both the "HTML.TextBox"  and  "HTML.TextBoxFor"  will produce the same output but the "HTML.TextBoxFor" is strongly typed while "HTML.TextBox" is weakly typed.
e.g. Html.TextBox("Address") // weakly type
it will give the same output as  
e.g. Html.TextBoxFor(x => x.Address) // strongly type

26. What is routing in MVC?
Ans. Routing in MVC is used to define the URL structure. It also map the URL to the controller and Action method. Fore more details, go to the Q.15

27. Where is the route mapping code written?
Ans. RouteConfig.cs is the file where the route is configured. Fore more details, go to the Q.15

28. Can we map multiple URLs to the same action?
Ans.Yes, we can map the multiple URL's to the same action method.

29. How can we navigate from one view to other view using a hyperlink?
Ans. Simply, We can use the ActionLink method and [provide the controller and action method as the parameters. e.g.
Html.ActionLink("Home","Index")

30. How can we restrict MVC actions to be invoked only by GET or POST?
Ans.  We can restrict the actions to be called by GET or POST by decorating the action method by HttpGet or HttpPost as below:







31. How can we maintain sessions in MVC?

Ans. There are various ways to maintain Sessions like-
a. Use Session variables
b. Use Hidden Fields and Html Controls
c. ViewBag - uses Dynamic keyword internally. No need to do casting.
d. ViewData - used to pass data from Controller to View. Casting is required.
e. TempData - between controller to controller or Action to action. It uses Session internally.

32. What is the difference between tempdata, viewdata, and viewbag?
Ans. TempData maintains the data for complete request while ViewData can be used from controller to View.See Q.31

33. What are partial views in ASP.Net MVC?
Ans. Partial View is like User Controls in ASP.Net so they can be used in other views. We can embed as many Partial Views in Main View.

34. How do you create a partial view and consume it in ASP.Net MVC 4?
Ans.  While creating a new view, we can see the option is given to make this view as partial. We need to check the checkbox "Create a Partial View". Once we design and do all the stuff related to filling the data in to the partial view, we can call it using the Html.RenderPartial("MyPartialView") into our main view.

35. How can we do validations in ASP.Net MVC?
Ans. In ASP.Net MVC, there is no need to do validation in the page(view). We can do the Model based validation which will be done in the Model using the Data Annotations.
To use the Data Annotation, we need to include
using System.ComponentModel.DataAnnotations; namespace in to our model.











36. Can we display all errors in one go? or Validation summery in ASP.Net MVC.
Ans. Html helper class contains a method with the name as ValidationSummary which can be used to show all the errors in one go.
e.g. Html.ValidationSummary()

37. How can we enable data annotation validation on the client side?
Ans.  To enable the Data Annotations at the client side, first we need to include the JQuery validation file and then call the Html.EnableClientValidation() method.

38. What is Razor in MVC?
Ans. Razor is the new engine to parse the MVC code. From ASP.Net MVC 3, Microsoft has introduces this engine. So in ASP.Net MVC 3, there are 2 View Engines- Razor and ASPX

39. Why Razor when we already have ASPX?
Ans. The Razor view Engine is lightweight as compare to the ASPX view engine. Razor engine has quite readable and simple syntax's.

40. How to implement AJAX in MVC
Ans. There are 2 ways to implement AJAX in MVC:
a. By using JQuery
b. By using Ajax Libraries

41. What kind of events can be tracked in AJAX?
Ans.

42. What is the difference between ActionResult and ViewResult?
Ans.

43. What are the different types of results in MVC?
Ans.

44. What are ActionFilters in MVC?
Ans.

45. Can we create our own custom view engine using MVC?
Ans.

46. How to send result back in JSON format in MVC
Ans.

47. What is WebAPI?
Ans.

48. But WCF SOAP also does the same thing, so how does WebAPI differ? With WCF you can implement REST, so why WebAPI?
Ans.

49. How can we detect that a MVC controller is called by POST or GET?
Ans.

50. What is Bundling and Minification in MVC?
Ans.

51. How does bundling increase performance?
Ans.

52. How do we implement bundling in MVC?
Ans.

53. How can you test bundling in debug mode?
Ans.

54. Explain minification and how to implement it. How do we implement Minification?
Ans.

55. Explain Areas in MVC?
Ans.

56. Explain the concept of View Model in MVC?
Ans.

57. What kind of logic view model class will have?
Ans.

58. How can we use two(multiple) models in a single view?
Ans.