Sunday, 7 February 2016

Interview Questions

Interface:

1. What is interface?
2. What type we can create n Interface?
3. Interface contain fields ?
4. Common C# Interfaces Examples
5. Benefits of using Interface
6. An Interface can itself inherit from multiple interfaces ?
7. When two interface have same type of method then how to inherit in a class
8. When to use Interface
9. In which design pattern we use Interface 


Abstract Class:

1. What is Abstract Class
2. Common C# Abstract class
3. Benefits of using Abstract Class
4.When to use Abstract Class




http://www.tutorialspoint.com/csharp/csharp_arrays.htm
http://www.sanfoundry.com/csharp-mcqs-floating-decimal-data-types/


Friday, 5 February 2016

OOPS and C# interview question and answers links

http://www.f5debug.net/post/2012/09/17/350-Interview-Questions-and-Answers-on-Net-Framework-OOPS-ASPNet-CNet-SQL-Server-WCF-in-one-place.aspx

http://clickmore.in/50-top-oops-concepts-interview-questions-answers/
http://a4academics.com/interview-questions/52-dot-net-interview-questions/417-c-oops-interview-questions-and-answers?showall=&limitstart=
http://mr-ponna.com/Questions-by-Category/96/1/C-Sharp-OOPs-Object-Oriented-Programming-Interview-Questions-and-Answers/
http://www.slideshare.net/gdrealspace/q-6035099
http://rajeshstutorials.blogspot.in/p/object-oriented-programming-interview.html
http://tutoriz.com/Thread-C-OOPS-INTERVIEW-QUESTIONS-ANSWERS-WITH-EXAMPLE-NET-C
http://www.itechaleart.com/2013/05/top-50-c-interview-questions-answers_10.html
http://atul-dhiman.blogspot.in/2012/10/mostly-asked-aspnet-interview-qusetion_1513.html
https://sites.google.com/site/interviewsharing/asp-net/difference-between-asp-net-3-5-and-asp-net-4-0
http://csharp-station.com/Tutorial/CSharp/Lesson21

Difference between questions (OOPS and .net)



1. Difference between an object and a class?
2. Difference between overloading and overriding?
3. Difference between delegate and events?
4. Difference between Interface and Abstract Class?
5. Difference between Class and Structure?
6. Difference between Reference Type and Value Type?
7. Difference between Generics and Non Generics Collection?
8. Difference between Array and ArrayList?
9. Difference between List and ArrayList?
10. Difference between delegate and events?
11. Difference between string and stringbuilder?
12. Difference between ienumerable and iqueryable?
13. Difference between Private and Static Constructor?
14. Difference between properties and methods?
15. Difference between Singleton Pattern and static class?
16.Difference between IDisposable and Finalize?
17. Difference between Constant and Read-only







Difference between Class and Object


S.No
Class
Object
1
It is a datatype that contains the programming logic.
It is a chunk of memory that implements the class logic.
2
Class is visible in the source code and resides in hard disk.
Object is in the RAM and not visible in source code.
3
Class is like a template or blueprint of the object. It implements reusability, encapsulation, inheritance
It is the real world implementation of the class. Each object has its own copy of data.
4
Example: Button is a class with properties like Text, BackColor,
events like click, methods like Focus
Example: Button1, Button2 are the objects of Button class.
5
We can create subclasses
We cannot create sub-objects




Difference between Overloading and Overriding


S.No.
Overloading
Overriding
1
Having same method name with different Signatures (parameters).
Methods name and signatures must be same.
2
Overloading is the concept of compile time polymorphism.
Overriding is the concept of run time polymorphism.
3
Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading.
When a function of base class is re-defined in the derived class called as Overriding.
4
It doesn't need inheritance.
It needs inheritance.
5
Method can be different access modifiers.
Method should be public.
 
 
6
Overloading = Multiple method signatures, same method name
Overriding = Same method signature (declared virtual), implemented in sub classes
 


Difference between Interface and Abstract class


 
S.No.
 
Interface
 
Abstract class
 
1
 
Interface support multiple inheritance
 
Abstract class does not support multiple inheritance
 
2
 
Interface doesn’t Contains Data Member
 
Abstract class contains Data Member
 
3
 
Interface doesn’t contains Constructors
 
Abstract class contains Constructors
 
4
 
An interface Contains only incomplete member (signature of member)
 
An abstract class Contains both incomplete (abstract) and complete member
 
5
 
An interface cannot have access modifiers by default everything is assumed as public
 
An abstract class can contain access modifiers for the subs, functions, properties
 
6
 
Member of interface cannot be Static
 
Only Complete Member of abstract class can be Static




Difference between Structure and Class


 
S.No.
 
Structure
 
Class
 
1
 
The struct is value type in C# and it inherits from System.ValueType
 
The class is reference type in C# and it inherits from the System.Object Type
 
2
 
The struct value will be stored on the stack memory.
 
The class object is stored on the heap memory. The object will be under garbage collection and automatically removed when there is no reference to the created objects.
 
3
 
The struct use the array type and its good to use for read only and light weight object.
 
The class uses the collection object type and it can perform all the operations and designed for complex data type storage.
 
4
 
The struct can't be base type to the classes and also to the other structure.
 
The class can inherit another class, interface and it can be base class to another class.
 
5
 
The struct can only inherit the interfaces
 
The class can inherit the interfaces, abstract classes.
 
6
 
The struct can have only constructor.
 
The class can have the constructor and destructor.
 
7
 
The struct can instantiated without using the new keyword.
 
The new keyword should be used to create the object for the class
 
8
 
The struct can't have the default constructor
 
The class will have the default constructor
 
9
 
The struct is by default sealed class hence it will not allow to inherit. It can't use the abstract, sealed, base keyword.
 
The class can be declared as abstract, sealed class
 
10
 
The struct can't use the protected or protected internal modifier.
 
The class can use all the access modifiers.
 
11
 
The struct can't initialize at the time of declaration.
 
The class can have the initializes fields.



Difference between Value type and Reference type


 
S.No.
 
Value type
 
Reference type
 
1
 
They are stored on stack
 
They are stored on heap
 
2
 
Contains actual value
 
Contains reference to a value
 
3
 
Cannot contain null values. However this can be achieved by nullable types
 
Can contain null values.
 
4
 
Value type is popped on its own from stack when they go out of scope.
 
Required garbage collector to free memory.
 
5
 
Memory is allocated at compile time
 
 Memory is allocated at run time
 
6
 
Ex. -int, enum, structs.  
 
Ex. -class, interface, delegate, string, object, Array


 
Difference between Generic and Non generic collections
 
S.No.
 
Generic Collections
 
Non generic Collections
 
1
 
These are the collections that can hold data of same type and we can decide what type of data that collections can hold.
 
These are the collections that can hold elements of different data types. It holds all elements as object type.
 
2
 
Type Safe, Secure, reduced overhead of type conversions.
 
It includes overhead of type conversions.
 
3
 
examples of Generic collections are List , Dictionary etc.
    List<double> list = new List<double>();
 
examples of non-Generic collections are ArrayList and Hash tables
    ArrayList arr = new ArrayList();
 
4
 

Similar Generic Type
 
List<T>
 
Dictionary<TKey,TValue>
 
SortedList<TKey,TValue>
 
Queue<T>
 
Stack<T>
 
IEnumerable<T>
 
N/A (use IEnumerable<T> anything that extends it)
 
ICollection<T>
 
IList<T>
 
Collection<T>
 
ReadOnlyCollection<T>
 
N/A (just implement IDictionary<TKey,TValue>
 
SortedDictionary<TKey,TValue>
 
KeyedCollection<TKey,TItem>
 
LinkedList<T>


 
Non-Generic          

 
ArrayList            
 
Hashtable            
 
SortedList           
 
Queue                
 
Stack                
 
IEnumerable          
 
ICollection          
 
N/A                  
 
IList                
 
CollectionBase       
 
ReadOnlyCollectionBase
 
DictionaryBase       
 
N/A                  
 
N/A                  
 
N/A  




Difference between Array and ArrayList collections
 
S.No.
 
Array
 
ArrayList
 
1
 
They are fixed length.
 
They are resizable and variable length.
 
2
 
They are compiled strong type collection.
 
They are flexible and can accommodate any data types.
 
3
 
Because arrays are of fixed size and strong type collection
performance is faster.
 
In arraylist lots of boxing and unboxing are done there for its
performance is slower.
 
4
 
They are fixed length.
 
They are resizable and variable length.
 
5
 
String [ ] str = new String [5]; // here you see that the length is fixed as [5] and the data type is also defined as string.
 
ArrayList   str = new ArrayList (); // here you see that the length is not fixed and is not tied up with a data type.


 
Difference between List and ArrayList collections
 
S.No.
 
Generic Lists (List<T>)
 
ArrayList
 
1
 
Generic List (List<T>) belongs to the System.Collections.Generic namespace           using System.Collections.Generic;
 
ArrayList belongs to the System.Collections namespace                                                          using System.Collections;
 
2
 
In Generic List (List<T>), T means data type, i.e. string, int, DateTime, etc. Thus it will store only specific types of objects based on what data type has been specified while declarations i.e. it is Type Safe. Thus if you have a Generic List of string you can only store string values, anything else will give compilation error.
 
Below I had no option other than having three different Generic Lists for three different data types.
 
List<string> lstString = new List<string>();
 
lstString.Add("Mudassar Khan");
 
lstString.Add("Robert Hammond");
 
lstString.Add("Ramesh Singh");
 
 
 
List<int> lstInt = new List<int>();
 
lstInt.Add(991);
 
lstInt.Add(10);
 
lstInt.Add(4450);
 
 
 
List<DateTime> lstDateTime = new List<DateTime>();
 
lstDateTime.Add(DateTime.Now);
 
lstDateTime.Add(DateTime.Now.AddDays(20));
 
lstDateTime.Add(DateTime.Now.AddHours(-10));
 
 
 
ArrayList does not have type restriction for storing data i.e. it is not Type Safe. You can store anything in ArrayList. In fact same ArrayList can store multiple types of objects.
 
ArrayList arrList = new ArrayList();
 
arrList.Add(921);
 
arrList.Add("Mudassar Khan");
 
arrList.Add(DateTime.Now);
 
 
 
3
 
Generic List stores all data of the data type it is declared thus to getting the data back is hassle free and no type conversions required.
 
C#
 
int number = lstInt[0];
 
string name = lstString[0];
 
DateTime dt = lstDateTime[0];
 
 
 
ArrayList stores all data as object thus to get it back you must remember what you stored where and correspondingly Type Cast it as per its original Type when stored.
 
int number = Convert.ToInt32(arrList[0]);
 
string name = arrList[1].ToString();
 
DateTime dt = Convert.ToDateTime(arrList[2]);
 
 
 
4
 
Generic List must be used instead of ArrayList unless specific requirement for projects higher than .Net 2.0 Framework.
 
ArrayList is mainly for .Net 2.0 Framework projects as during that period Generic List was not invented.
 
5
 
While running a Loop on Generic List again it is problem free as we exactly know what the List contains.
 
foreach (int number in lstInt)
 
{
 
}
 
foreach (string name in lstString)
 
{
 
}
 
foreach (DateTime dt in lstDateTime)
 
{
 
}
 
 
 
While running a Loop on ArrayList you need to use Object data type. Thus this is another disadvantage as you again do not know what type of data particular item contains.
 
foreach (object o in arrList)
 
{
 
 
 
}


 
Difference between Events and Delegates
S.No
Events
Delegates
1
Event can be used in an interface definition
Delegate cannot be used in an interface definition
2
Event can only be invoked from the class that declares it
Delegates can be invoked from child classes and clients.
3
Event comes with its pair of accessors i.e Add and Remove. An event is always assigned and unassigned with a += and -= operator.
There is no pair of accessors concept in delegates.
4
Event has a restrictive signature and must always be of the form Event (object source, EventArgs args)
Delegates do not have restrictive signature as like events


 
Difference between String and StrinbBuilder
S.No.
String
StrinbBuilder
1
It’s an immutable
It’s mutable
2
Performance wise string is slow because every time it will create new instance
Performance wise stringbuilder is high because it will use same instance of object to perform any action
3
In string we don’t have append keyword
In StringBuilder we can use append keyword
4
String belongs to System namespace
Stringbuilder belongs to System.Text namespace
5
string strMyValue = "Hello Visitor";
 // create a new string instance instead of changing the old one
 strMyValue += "How Are";
 strMyValue += "You ??";


StringBuilder sbMyValue = new StringBuilder("");
 sbMyValue.Append("Hello Visitor");
 sbMyValue.Append("How Are You ??");
 string strMyValue = sbMyValue.ToString();




 
Difference between IEnumerable and IQueryable
S.No.
IEnumerable
IQueryable
1
IEnumerable exists in the System.Collections namespace.
IQueryable exists in the System.Linq Namespace.
2
IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.
IQueryable is suitable for querying data from out-memory (like remote database, service) collections.
3
While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.
While querying data from a database, IQueryable executes a "select query" on server-side with all filters.
4
IEnumerable is beneficial for LINQ to Object and LINQ to XML queries.
IQueryable is beneficial for LINQ to SQL queries.
5
IEnumerable you get all the records at once
If you use IEnumerable to get the first 5 customers you end up loading all 1000 and then selecting the first 5.
static void Main(string[] args)
{
    using (var db = new MyDbContext())
    {
        IEnumerable<Client> clients = db.Clients.Take(5).ToList();
        // ToList() executes the query straight away
        // All the list of clients is loaded into memory (1000 clients)
        // After that, only the first 5 are selected
 
    }
           
}
IQueryable you only get the records that you want
With IQueryable you only select the first 5 (saving a lot of resources!)
 
 
static void Main(string[] args)
{
    using (var db = new MyDbContext())
    {
        IQueryable<Client> sameClients = db.Clients.Take(5).ToList();
        // ToList() executes the query straight away
        // The first 5 clients are loaded into memory
    }
           
}


 
Difference between Dispose & Finalize Method


S. No.

Dispose()

Finalize

1

This dispose method will be used to free unmanaged resources like files, database connection etc.

This method also free unmanaged resources like database connections, files etc…

2

To clear unmanaged resources we need to write code manually to raise dispose() method.

It is automatically raised by garbage collection mechanism whenever the object goes out of scope.

3


This Dispose() method belongs to IDisposable interface.


This method belongs to object class.

4

If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.


We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when garbage collection process done.

5


It will not show any effect on performance of website and we can use this method whenever we want to free objects immediately.

 //Implement Dispose Method.
public class TestDispose : IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
}

protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// clean unmanged objects
}
// clean unmanaged objects).

disposed = true;
}
}
}

It will show effect on performance of website and it will not suitable to free objects immediately.  




// Implementing Finalize method
public class Sample
{
//At runtime destructor automatically Converted to Finalize method.
~Sample()
{
// your clean up code
}
}







 




Difference between Constant and Read-only


S. No.

Constant

Read-only

1

Declared and Initialized in Compile time.

The value will be initialized either declaration time or the constructor of the class allowing you to pass the value at run time.

2

Const values will evaluate at compile time only.

Read only values will evaluate at runtime only.

3

Const value can’t be changed these will be same at all the time.

Value can be changed in the constructor of the class.

4

By default Constant is static, Cannot be declared as static.

Can be declared as Static.

5

static void Main(string[] args)

   {

       const int a = 10;

       const int b = 20;

       const int c = a + b; // no error as both a and b are const variable

 

int z = 20;

 

const int d = a + z; // error message "The expression being assigned to d must be const" on runtime as both a and z are not Const variable.          

Console.WriteLine("Result of a+b = "+c);

       Console.ReadKey();

   }

class Program

{

        readonly int x = 10;

        public Program()

        {

             x = 20;// changed the value in the  constructor

 

        }

}