Translate to your language...

12 January 2014

19 Brainstorming questions with answers in Microsoft Technologies->.Net, C#, ASP.Net, SQL Server 2008, ASP.Net MVC 3

This article contains the questions related to the .Net Technologies. It covers the questions related to OOPs concepts, C# and ASP.Net, MVC and SQL Server. The questions are having answers so that they will be helpful for those who are preparing for the interviews. These are the questions which are asked in various interviews in different companies. So in that way also it will be useful as per the current industry trend and demands for 2+ years experience guys in .Net Technologies.

1. Why we used Inheritance. What is the benefit of this.
Ans. Inheritance is a way to inherit the base class members to the child class so that we can save lot of memory and reuse the inherited members.
Let's say, if you are already having some properties defined in the base class and then if you are inheriting this base class to the child class, the extra memory need not to be wasted to declare the base class members again. While implementing the inheritance, automatically they will be inherited.
e.g.

class Mybase
{
 int i, j;
 float z;
}
class MyChild: Mybase
{
 // no need to declare again the above variables
 // as they will be automatically inherited.
}
As we need not to declare the variables again in the child class, so we have saved here 4 + 4 + 8 = 16 bytes of memory for the child class.
Main usage of inheritance is 'Re-usability'. No need to declare variables, methods again.. If u want, then use them from base class. It reduce your code from complexity.

2. Why C# does not support Multiple Inheritance. 
Ans. Multiple Inheritance is the situation in which when there are 2 base class and a single child class is trying to inherit the members from both of them then there is the confusion that which base class member should be inherited?
e.g.
class MyClass1
{
   public void Show()
   {
     // something
   }
}
class MyClass2
{
   public void Show()
   {
     // something else
   }
}
class MyChildClass: MyClass1, MyClass2
{
 // not possible
}
Due to this, the architecture of .Net framework doesn't support such situation and will throw the compile time error.
In C++, it is possible because there both the base class methods will have in different memory locations and the methods will be accessed through the memory location. But in Java or .Net the method is accessed through the class members objects.
It is quite difficult to implement multiple inheritance in C#. But we can do this through Interfaces. In the above scenario, if we can change the second class as interface and then implement them as below:

interface MyInterface
{
   void Show();   
}
class MyChildClass: MyClass1, MyInterface
{
    public void Show()
   {
     // do something 
   }

    public void MyInterface.Show()
   {
     // do something else 
   }
}

3. Why we used virtual keyword.
Ans. If we want to override the members of the base class, we can make them as virtual. The virtual members have the capability to override them in the child class. If they are not overridden, the base class implementation will be executed.
It is not the mandatory to override the virtual members of the class.
e.g.
class MybaseClass
{
 public virtual void Show()
 {
  Console.WriteLine("hey..I am in base class");
 }
}

class MyChild: MyBaseClass
{
 public override void Show()
 {
  Console.WriteLine("hey..I am in child class");
 }
}
The preference will be given to child class always.
public static void Main(string[] args)
{
 MyChild objChild = new MyChild();
 objChild.Show(); // child class method will be called.
}
If a base class method needs to be overridden, it is defined using the keyword virtual (otherwise the sealed keyword is used to prevent overriding).

4. If my base class does not have virtual keyword but both class have same method , parameter and return type. then what will happen. base class method will be called or only child class method will called.
Ans. By default the preference will be given to child class. The child class method will be called always. If you want to call the base class method, then you need to use the base keyword like base.MethodName()

5. What are events and actions in c#.
Ans. Events are the objects which are binded with the particular action. Whenever an action happens, an event fires. Like button click event.The action is click and event is Button_Click which is attached with the event handler to do the particular action and get the result.

6. What is user define function in SQL or What are the user define function available in SQL.
Ans. There are 2 types of functions in SQL Server-
1. Inline functions
2. Tabular functions
Inline functions gives the result as a single value while the tabular functions return the multiple values like a table.
Both of these types of function can be created as user defined. Means we can create our own function which can be inline or Tabular by using their syntax.
Create function
return int[/Table]
as
Select FirstName+ LastName from Emp where EmpId = 101;
Create Function
return Table
as
Select * from Emp;


7. If we have one Master Page , one aspx page and one ascx page then which one will called 1st while page_load event and which one called 1st while page_Unload event.
Ans. If the content page contains the user controls then below will be the calling route-
Master Page Load event --> Content Page Load event --> User control page Load event
While unloading:
Content page Unload --> user control unload --> Master page unload event

8. What is Indexers and please give the description with example.
Ans. Indexer are the objects which doesn't need to be initialized and we can use them as it is for the storing and retrieving the data in to pages.
Indexer is represented as []. So we can use them in Session, Application types of objects when keeping the state management data.
e.g.
ViewState["UserName"] = txtUserName.Text;
Session["UserName"] = txtUserName.Text;
Application["UserName"] = txtUserName.Text;
To retrieve these data, we can simply type cast them as :
lblUserName.Text = ViewState["UserName"] as string;
lblUserName.Text = Session["UserName"] as string;
lblUserName.Text = Application["UserName"] as string;
Here you can see that there is no need to create object of the indexers. Directly we can retrieve the data.

9. What are indexes in Database. How many types of Indexes in SQL Server
Ans. Indexing is used to increase the performance of a query, instead of performing the entire table scan the query uses the index to execute the query.
There are five types of indexes in the SQL Server database:
1. Clustered Index
2. Non-Clustered Index
3. KeySet Index
4. Default Insex
5. XML Index
One thing here to remember that the Indexes retrieves the data by using the Binary tree formats.
1. Clustered Index:- Those indexes which can be maximum 1 per table and they will be the key to retrieve the data. the clustered indexes retrieves the data in the B-Tree format and improve the performance of the retrieval from the table.
e.g. There are 1000 records in the table and the user want to retrieve the 234th record. Without index, the table scan will compare the record in whole table to get the matched record. So for the 1000 records, it will match for 999 times. If it found the records in the beginning, then also it will search the whole table.
When using the index, the below process will occur for the searching of 234th record:
1. It will take the 234th record as the root and compare the table by partitioning it in to two parts.1-500 and 501-1000
2. then it compares with the left and right and then it will found that 234 is less than 500 so leave the right part.
3. Now comparing will happen for the 1-250 and then 250-500. For this, it will check that 234 will come in the 1-250 range so leave the other part.
4. Now the comparison will be 1-125 and 126-250...and so on..
So if you calculate the total comparison it will be hardly 8-9. So we have reduces almost 990 comparison to improve the performance.
In Clustered index, the actual data exists at the leaf root of the B-tree and it search the data directly.
2. Non-Clustered Index:- it searches the data in B+ tree format where the leaf node contains the memory location of the data. Once it gets the actual memory location, by using the reference it retrieve the actual data.There can be more than one Non-Clustered indexes per table.
There can be 249 non-clustered index(in SQL Server 2005) and 999 non-clustered index (SQL Server 2008).
3. KeySet Indexes:- Only keys are stored and not the actual data. the search will happens based on the key. Once the key found, it searches the complete data for the key. So its faster than all the indexes.4. Default Index:- When the primary is added to the table, by default the index gets created called as Default Index.5. XML index:- When the index is created on the XML column, the index is called as the XML index. This type of index gets introduces since SQL Server 2005 version. XML type column is supported since SQL Server 2005.

10.Why we use Interface.
Ans.Interface is a way to use the global functionality throughout the application. If your application require many unrelated object types to provide certain functionality then you go for interfaces.
It defines a contract between the application and the object.
Let's suppose we want to Show the data or Print the data in many pages of the application, we can create a Interface which will contain the Abstract method and in the page where we want, we can implement it as per our requirements.
e.g.
interface inf
{
 void Print(); // abstract method
}
internal class MyClass: inf
{
 public void Print()
 {
  // functionality to print to PDF document
 }
}
internal class MyTest: inf
{
 public void Print()
 {
  // functionality to print to tiff document
 }
}
internal Class MyTest: inf
{
 public void Print()
 {
  // functionality to print to .jpg format
 }
}
We can see here we have separate classes and the implementation of the Print method is different without making any changes in our interface.
So if we want to implement something global, we can use the Interface, for local or limited to class, we can use the abstract class.

11.Why we use Method Overloading
Ans. To reduce the memory and the good readability, we use the Overloading. This is the concept where we can have the same method name for similar work with different behavior.
Let's say we want to get the Database connection based on the provider name to connect with different databases, we can use the overloaded methods like:
public string GetConnectionString()
{
 // return default connection string
}
public string GetConnectionString(string provider)
{
 // return connection string based on provider name
}

12. Why we use Method Overriding
Ans. Method overriding is the concept where we can use the same method in parent as well as in child class to reduce the memory and use the similar behavior. If we don't want to change the behavior, we need not to override it. So to make the changes in the behavior, we use the overriding.

13. What is the difference between Abstraction and Encapsulation.
Ans. Encapsulation is the biding and hiding of the data while Abstraction is to get the essential information from the raw data.
e.g. Encapsulation
class Test
{
 private string _name;
 public string Name
 {
  set _name= value;
  return _name;
 }
}
Here the _name is the private which is not accessible so hiding the data. All the data will be binded to the private variable as per the statement
set _name= value;
This property will be accessed by the public member called Name.
So Binding and hiding means encapsulation.
Abstraction is done by using the abstract class where we can have abstract and non abstract members.
When ever we are having the abstract members, we need to implement it/override them in the child class to use them.
class MyBaseClass
{
 public abstract void Displaye();
}
class MyChildClass: MyBaseClass
{
 public override void Display()
 {
  // do something
 }
}
Here the essential things is the abstract method which must be overridden. If we add any number of non-abstract method, the child class wont care. So Getting the essential information is Abstraction.

14. In which case we can use Abstract class in our project.
Ans. Abstract class is always used when we have the limited scope or the objects are of similar types.
e.g. Lets suppose we want to get the area of few objects. This is not the global where we need to do.
So we can create the abstract method to calculate the area and then according to shape, we can override them. So here the scope is limited and will not be used throughout the application.
Hence we can use the abstract class.

15. What is the difference between Array List and Ilist.
Ans. IList is an interface for the implementing the List collection.
As the List is the generic so we can use it to restrict the type of the list e g.
List objList = new List();
It means, the List object is restricted to the int. We can't insert here any type of data in to list. Only the int is permitted.
ArrayList is the collection where we can insert any type of data as:
ArrayList objArrayList = new ArrayList();
objArrayList.Add("Hello");
objArrayList.Add(1);
objArrayList.Add("Sure");
The ArrayList will not check the type in the compilation of the program.

16. What is difference between internal class and sealed class.
Ans. The internal class object can be used with-in the assembly and not outside of the assembly. These classes can be inherited.
Sealed classes are those classes which doesn't need anything from outside. These are full classes.
These classes can't be extended.

17. Are these method are overloading methods?
public void show(int x , string y)
public void show(string x , int y)
Ans. Yes, these are the overloading methods because, their signatures are not same
Signature= Method Name + Argument types

Both signatures are different. So overloading methods.
Now create the object of the class A and check for the overloaded method:
As per method overloading, the method should differ in
- type of parameters
- Order of parameters
- number of parameters

18. What is the difference between Remoting and web service.
Ans. Remoting is the concept of calling the remote interfaces like communication with the heterogeneous applications but both should be developed using the same .Net language.
For the client and server communication, we use the Remoting.
Remoting is the platform dependent so both the client and server must be built in .Net Technology and both the systems should use the CLR.
Web-service is small logic which runs on the internet. It is used for the heterogeneous applications and consume the service in any application. It is platform independent so no need to have the CLR or .net framework to be installed on the machines. C++ web service can be consumes in .Net application.

19. Can we use viewstate in MVC.
Ans. No, ViewState cannot be used in MVC. There is no concept of ViewState in the MVC as it is not required. The MVC views are not having anything server side code so the view only be rendered and not loaded.
To pass the data from one page to another, we can use the ViewBag.

The reason we use MVC is to separate business logic from your view or UI and the site should be easily testable.
ViewState mixes your business logic with your UI, while MVC separates the Business Logic from the UI.
Hope they will be useful to all of us.

15 September 2013

OOPs Concepts: When to use

Hai Friends,

This post is regarding the main OOPs concepts and what is there use and when it’s good to use them.

Below are the concepts which are included in this post:

·                     Encapsulation
·                     Abstraction
·                     Overloading
·                     Method Overriding
·                     Constructor
·                     Interface
·                     Inheritance
·                     Abstract class
 
These OOPs concepts you can use while implementing the functionality of your projects in accordance with the requirements and situations.
Hope it will be useful to learn and in the implementation of the projects.
1) In which situation encapsulation is used in your project?
Encapsulation is hiding and binding of the data.
Accessing the public properties by using private variables is one of the good examples of Encapsulation in all the projects.
e.g.
private string _name;
public String Name
{
get{return _name}
set{_name= value;}
}

Here you are not able to access the private variable _name but its value can be accessible by the public property Name. So a kind of Encapsulation here. Namespace is also an example of Encapsulation which is encapsulating the classes, methods, properties etc.
This is the runtime behaviour of an entity.

2) In which situation abstraction is used in your project?
Hiding the behaviour and showing the necessary things regards an entity can be called as Abstraction. Like for a Car, showing the method like Clutch, Gear etc but hiding its internal functions can be an example of Abstraction. This is the compile time behaviour of the entities.
When there are the specific requirements and that can be flown to their child, then we can use abstraction. it means whatever is essential, we implement to its child classes, else will be inherited automatically.
e.g.
class A
{
public abstract void Add(int a, int b);
protected int x;
public string Name{get; set;};
}
Class B: A
{
 public override void Add(int a, int b)
 {
  Console.WriteLine("Sum of two numbers is:" + int. Parse(a + b).toString());
 }
}
Here the essential method Add is implemented to the child class B. rest all the members will be inherited accordance to their protection levels.

3) In which situation method overloading is used in your project?
The concept where we want the similar functionality but different behaviour, we use overloading.
When we have same functionality to implement with different behaviour, we go with the overloading of method.
e.g. If we have to get the sum of 2 numbers, 3 number and 4 numbers. So in that case we can take only one method name Add with its varied parameters like:

public void Add(int a, int b)
{
 Console.WriteLine("Sum of 2 numbers" + int. Parse(a + b).toString());
}

public void Add(int a, int b, int c)
{
 Console.WriteLine("Sum of 3 numbers" + int. Parse(a + b +c).toString());
}

public void Add(int a, int b, int c, int d)
{
 Console.WriteLine("Sum of 4 numbers" + int. Parse(a + b + c + d).toString());
}

Now according to our requirement, we can call the respective method with its arguments.
Here all the methods are doing sum of the numbers, but their behaviour are different- the first method will give the sum of 2 number while the next one will give the sum of 3 number and the final method will give the sum of 4 numbers.

4) In which situation method overriding is used in your project?
When we may(abstract) or may not(virtual) want to implement the particular method at runtime to its child class according to the requirement, we go with overriding concept.
e.g.

Class X
{
 public virtual void Print()
 {
  Console.WriteLine ("This is the print method. You can override according to your definitions and implementation");
 }
}
Class Y:X
{
 public Override void Print()
 {
  Console.WriteLine("Implementation for Excel print methods");
 }
}

5) In which situation constructor is used in your project?
Constructor is the way to initialize the class with the default values of the members. Constructor is by default initialize with the class.
But if you want to initialize some members when the class in getting instantiate, you can write the constructor and code for initialization inside that constructor.
e.g.

Class MyClass
{int a;
 public MyClass()
 {
   a=10; //initialized with the class instantiation
 }
}

Here the constructor code will get initialized with the class instantiation.

6) In which situation interface is used in your project?
Interface is the collection of abstract members.
So whenever we have the requirements such as it requires through the application at many places, then we create an interface with that method so that we can implement it at our own way according to the requirements.
e.g.
Interface Inf
{
 void Print();
}

Class ExcelClass:Inf
{
 public override void Print()
 {
  //Write code to print in excel
 }
}

Class WordPrintClass: Inf
{
 public override void Print()
 {
  //Write code to print in Word
 }
}
Class PDFPrintClass: Inf
{
 public override void Print()
 {
  //Write code to print in PDF
 }
}

Also to implement the multiple inheritances, we use inheritance.

7) In which situation inheritance is used in your project?
When we want to have some common features for multiple classes then we go for inheritance.
To inherit the properties of base class to their derived class.
8) In which situation abstract class is used in your project?
Abstract class is same as abstract. So go to the Q 2 for the details.


So you can go through the link to get the fair idea about its implementation: 
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=295763&

8 September 2013

3-6 yr Experience Interview Questions in .Net Technologies PART - II

Hai Friends,

With lot of encouragement and support, I am posting the Part II of the questions and answers for 3-6 years experienced people. These questions will be helpful for those who are preparing for the interview or attending the interviews. This will be helpful for the last minute preparation in quickest way.
If anyone has better answer, please reply to this post and I will include them.

C# / ASP.net/ .Net framework / other .Net related questions:

1. What is the difference between IEnumerable and IQueryable?
Ans. IEnumerable and IQueryable are used to query data from collections and database.  The IEnumerable is basically inherited from IQueryable, so it’s having all the features of the IQueryable in addition to its having additional own features.

S.NoIEnumerableIQueryable
1.
Exists in System.Collections Namespace
Exists in System.Linq Namespace
2.
Can move to forward only in the collection.
Can move to forward, backward or in between the items.
3.
Best for collection like List, Array or in-memory collection
Best for the collections which is out memory like services, remote databases etc
4.
Suitable for LINQ to Object and Linq to XML queries.
Suitable for Linq to SQL queries
5.
It doesn’t supports custom query, Lazy loading so it’s not good for paging scenarios.
It supports custom query using CreateQuery and Execute methods. Also support lazy loading so good for the paging scenarios.
6.
The extension methods which supports be IEnumerable, takes the functional objects as the parameters.
The extension methods which supports be IQueryable, takes the expression tree as the parameters.




In the first example (By using the IEnumerable), the generated SQL is as below:

In the second example (By using the IQueryable), the generated SQL is as below:
We can see that by using the IQueryable, the performance will increase as it has the TOP clause in the query.

2. What is cross site scripting? (XSS)
Ans. Cross-site scripting is the way to attacks and insert the vulnerabilities in Web page. This attack is injected by client-side code. The script which is inject by the client can embed itself in response data. The response data which send back to the malicious user. The browser can’t recognize the scripts as it is responded from the trusted source.
The cross-site scripting attacks also work on the HTTP and HTTPS.
There are 2 ways to prevent the cross site scripting attack:
Constrain input- Validate the input length, type, formatting, range etc
Encode output- Send the input data with encode e.g. Encode to HTML
To prevent the cross-site attack, we can set the below attributes in web.config file: 
3. If I want to see my website similar in all the browsers then what I need to do for that?
Ans. If you want to see the website with the same look and feel then you need to write the common css style which should be same for all browsers. Actually every browser will not support every css elements so it is not possible to use the same css to support all the browsers. You need to write the common css elements in the file and then apply it across the website. Else you need to write the separate css file as per the browser.

4. If say 1 Lac users are using any particular website then what you will do to prevent crashing of server? Which care you will take at the time of coding?
Ans. There are the ways like we can have multiple servers to handle the requests from the users. In this, we can have the Web Gardening concept where we can have the multiple web servers and then one main server to handle the number of requests and switch the requests to other servers.

5. Why to use design patterns?
Ans. Design Pattern is the way to solve the recurring problems which occur during the designing of the applications. As the requirements increases, the projects becomes complex and due to the complexity, it’s difficult to maintain it.
With the help of design patterns, we can reduce the complexity and with the help of OOPs paradigm, we can make our applications more efficient in all the ways.
According to the GoF (Gang of Four) company, the Design Patterns can be classified to 3 ways:-
  • Creational Design Pattern
  • Behavioral Design Pattern
  • Structural Design pattern
6. If I have a class C and two interfaces I1 and I2 and I have 'Add' method inside I1 and I2 then how to specify which one has to be called?
Ans. By using the explicit implement interface, we can implement the same method which is defined in both the interfaces.
e.g.
The implementation will be as below:
7. Garbage collection uses which type of algorithm? How it will find which object is unused?
Ans. Mark-And-Release is the algorithm which the garbage collection uses to reclaim the memory of the unreferenced objects which are no longer used.
The algorithm Mark-and-Release work in 2 steps:-
a.    In the first steps, it marks all the accessible objects of the heap memory. This is called as mark phase.
b.    In the second step, scan the heap and reclaim all the unmarked objects by the Garbage Collection algorithm. This step is called as sweep phase.
Below are the algorithm steps:
For the detailed description, follow the below link:
8. What is the difference between out and ref?
Ans: Ref and Out are the parameters which are used in the methods  [ref] and [out] both allows the called method to modify a parameter. The difference between them is what happens before you make a call.
  • [ref] means that the parameter has a value on it before going into the function. The called function can read and or change the value any time. The parameter goes in, then comes out
  • [out] means that the parameter has no official value before going into the function. The called function must initialize it. The parameter only goes out.
So the main difference between the ref and out parameter is that the out parameter doesn't needs to be initialized while the ref parameter must be initialized before passing to the function.

9. Is it possible to use more than one out parameter?
Ans.  As we know that the method always returns a single value but by using the out parameter, we can return multiple values from the method or function.
In C# we can write the small code snippet which will describe to return the multiple values using the out parameters:
We can also use the struct which will contain the multiple values as the return:

There is another way to return multiple values like using Tuple class (newly introduced in .Net 4.0). The tuple class can return the object which can contain multiple values in it. 

10. Is it possible to use .js files used under script will be in body and not in header? Why?
Ans. The .js file is used in header because first the .js file should be loaded to the application and then rest of the content should be load.


11. What is Expression Tree in C#? How to use Express Trees to create Dynamic LinQ queries?

Ans. Expression Tree is used to create Dynamic Linq query. Sometimes, when the specific criterias' can't be defined at compile time, Expression tree is useful to generate the Linq query at the runtime.
For E.g. In an application, the filter criteria is based on some user actions and is generated dynamically. In this case, we can use the Expression Tree which will generate the criteria at runtime and then we can use that criteria to filter the records from the IQueryable collection.
System.Linq.Expressions is the namespace required to work with the Expression Tree. Here we need to create the predicates using expressions, which will be used in the query.


Hope these questions and answers will be helpful to you.

Sr .Net Consultant | MCP Certified | Microsoft Technologies(.Net with Sql Server, MVC, AngularJS

My photo
Kuala Lumpur, Kuala Lumpur, Malaysia
Overall 17.0+ years of experience in software industry as a Programmer/Developer/Architect with Web & Windows application development and Maintenance, including analysis, design, coding, testing and documentation. • Proficient in Microsoft Technologies i.e. Visual Studio with .Net Framework. • Excellent analytical, problem solving, communication, interpersonal skills with the ability to handle multiple tasks or projects working independently or in a team. • Consistent attention to detail with an ability to analyze and interpret the implication of decisions to minimize bugs and create user-friendly applications. • Experience of Collaborating with Clients and Team Members. Ability to convey Technical Information at all levels. • Ability to work with team/management to ensure successful execution of project management tasks including scheduling, scope management, outside dependency coordination and priorities resolution. • Experience in working with global clients like UK, USA, Europe, Australia etc. and teams located remotely (e.g. Sydney, Australia, USA, and Sweden). • Worked with the Swedish client ManpowerGroup at onsite client location Stockholm, Sweden.