Thursday, 30 October 2014

C# Interview questions

What is overriding?

Overriding is a concept where a method in a derived class uses the same name with the same return type and also the same arguments as a method in its base class. In other words, if the derived class contains its own implementation of the method rather than using the method in the base class, the process is called overriding

What are events and delegates?

An event is a message sent by a control to notify the occurrence of an action. However it is not known which object receives the event. For this reason, .NET provides a special type called Delegate which acts as an intermediary between the sender object and receiver object

Can multiple catch blocks be executed? 

No, Multiple catch blocks can't be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed.

difference between public, static and void?

All these are access modifiers in C#. Public declared variables or methods are accessible anywhere in the application. Static declared variables or methods are globally accessible without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. And Void is a type modifier that states that the method or variable does not return any value.

difference between ref & out parameters?

An argument passed as ref must be initialized before passing to the method whereas out parameter needs not to be initialized before passing to a method

Can “this” be used within a static method? 

We can’t use ‘This’ in a static method because we can only use static variables/methods in a static method.

What is difference between constants and read-only? 

Constant variables are declared and initialized at compile time. The value can’t be changed after wards. Read-only variables will be initialized only from the Static constructor of the class. Read only is used only when we want to assign the value at run time.

What is an interface class? 

Interface is an abstract class which has only public abstract methods and the methods only have the declaration and not the definition. These abstract methods must be implemented in the inherited classes.

What are value types and reference types? 

Value types are stored in the Stack whereas reference types stored on heap.
Following are some of the Value types:

int, enum , byte, decimal, double, float, long 

Reference Types:

string , class, interface, object.

Different categories of inheritance?

There are 4 types of Inheritance in Object Oriented Programming and they are as follows
(i)Single inheritance : Have one base class and one derived class.
(ii)Hierarchical inheritance : Have one base class and multiple derived classes of the same base class.
(iii)Multilevel inheritance : Have a class derived from a derived class.
(iv)Multiple inheritance : Have several base classes and a derived class.

Difference between constants, readonly and, static ?

(i) Constants: Value can’t be changed.
(ii) Read-only: Value will be initialized only once from the constructor of the class.
(iii)Static: Value can be initialized once.

What is multi cast delegates?

Each delegate object holds reference to a single method. However, it is possible for a delegate object to hold references of and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.

What is data encapsulation?

Data encapsulation, also referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions called methods.

Is it possible to override private virtual methods? 

No. Private methods are not accessible outside the class.

Can you store multiple data types in System.Array? 

What’s the difference between the System.Array.CopyTo() and System.Array.Clone()? 
The first one performs a deep copy of the array, the second one is shallow.
What is the difference between string and string builder?
a.It uses the the namespace "System"
b.It is immutable, meaning value will not be overwritten in the memory while assigning new value variable to the string. It creates a new memory space for storing the new variable.
c.It is sequential collection of Unicode characters for representing text
d.Additional memory will be allocated during concatenation.

a.It belongs to the namespace "System.Text"
b.It is mutable, meaning value will be overwritten in the memory while assigning new value variable to the string.
c.Cannot assign value directly to string.You need to create object

      StringBuilder obj = new StringBuilder("Hello World");

d.During concatenation additional memory will allocated only if it exceeds buffer capacity.

What are difference between events and delegate?

Both delegates and events are tied together
1. It is the outcome of an action.
2. Event Source(Which raises the event) and receiver(object that responds to the event)are the two important terms
3. Delegate acts as a communication channel between Event Source and Event Source.

1.It is function pointer.
2.Delegate can call more than one function.
3.There are two types of delegates
(i) Single Cast
(ii)Multi Cast

        //declaring delegate      
        public delegate void sample_delegate();       
        public void GoodMorning()      
        MessageBox.Show("Hello Friend" + "GoodMorning");
        public void GoodEvening()      
        MessageBox.Show("Hello Friend" + "GoodEvening");      
        private void button2_Click(object sender, EventArgs e)      
          // instantiation          
          // Here we are calling two methods so it is multicast delegate
         // If you call only one it is single cast          

         sample_delegate sd = GoodMorning;          
         sd += GoodEvening;          

MVC and difference. Why to choose MVC if fulfills my project requirements?

a. No View State
b. No PostBack
c. Code and HTML are completely separated
d. Best Suited for large projects
e. Unit testing can be done easily.
f. Testing can be done with out invoking the view
g. Can easily plugin more jquery script than easily
h. Viewstate is not used for maintaining state information.
i. Increase the performance

a. It follows 'page Controller' pattern. So code behind class play a major role in rendering controls
b. You cannot do unit testing only for code behind alone. You need to invoke view to do testing
c. It manages the state by using view state and server based controls.
d. HTML Output is not clean

Partial views and strongly typed view difference.

Patial Views:
These are sub-views or reusable views
Whenever we want to reuse the views we can go in for partial views.
Examples of reusable views are header and footer views where we will be using it across the application.
To achieve reusable views partial views are created.
It is light weight.

Strongly Typed Views

It is used for rendering specific types of model.
It inherits from ViewPage (T --> Type of the model)

What is master page?
Master Page is used in web application
We can say master page as a template for the other pages in our project.
For creating a constant look and feel for all our pages in web application.
It acts as a placeholder for the other pages or content.
If a user makes a request to a webpage it merges with masterpage and will provide the output.
How to do JavaScript client side validation? Why we do validation at client side?

The below code is to do client side validation

   function samplejavascript()
    alert('Hello World');

In order to increase the performance of web application and to avoid post backs. So validation are done at client side. 

What are limitations of Open XML?
OpenXML in SQL haS the limitation of storing values upto 8000 characters only 

What is difference between a.equals(b) and a==b?
“==” --> It compares reference 
"Equals" --> It compares object by VALUE.

What is Nuget?
It is Visual Studio extension and a opens ource project
By using Nuget we can easily add,update and remove libraries in Visual Studio Projects.
When you add or remove a library it copies/removes necessary files to your application.
It is a quick way to add reference to our application.

If I want to do debugging in Jquery then which library I need to add in my project?

To debug jQuery code in IE using Visual Studio debugger.Add the keyword "debugger" where you want to debug

       function samplejavascript()
        alert('Hello World');

Have you used any collections in C#? What is the purpose of using it? 

Yes i have used collection in c#.
It is uses the namespace "System.Collection".
It is a specialized class for data storage and retrieval.
It is type safety and increases performance. 

Commonly used collections are

How to find whether data is stored in stack or its in heap?

Both are stored in the computer’s RAM (Random Access Memory)
Value types are stored in stack and stored in sequential (LIFO).
Objects are stored in Heap and data are stored randomly 

some additional points:-
Stack is much faster than heap. It’s because of the memory allocation.
When it wants to allocate memory it moves up in Stack

How to use satellite assemblies? Have you ever thought of using it in your project?

If we want to write multilingual/multicultural app and want to have it separated from your
main application. Those are called satellite assemblies. 
An assembly which is containing culture information is called satellite assemblies. 

What is publisher subscriber model? Any design pattern can be implemented for it? 

It is also called as Observer design pattern.
Windows Communication Foundation (WCF) is the best example for this model.
The user can publish a Service and it can be consumed by many clients at the end point.
The Publisher gives the data to the clients who have subscribed to their methods.


Post a Comment