Thursday, 9 March 2017

C# basic interview questions Part-7

1. Ques: Explain All the version (history) of C# ?
Answer:
C# is powerful object-oriented programming language developed by Microsoft.its first release in 2000. C# was introduced with .NET Framework 1.0 and the current version of C# is 6.0.

C# 1.0:
  • C# 1.0 was released by Microsoft with Visual Studio 2002. C# 1.0 targets .NET Framework 1.0. It was the first language adopted by developers to build .NET applications.
C# 1.2
  • C# 1.2 was released by Microsoft with Visual Studio 2003. C# 1.2 targets .NET Framework 1.1.
C# 2.0
  • C# 2.0 was released by Microsoft with Visual Studio 2005. C# 2.0 targets .NET Framework 2.0.
  • The new features added in C# 2.0 are as given below:
  • Generics
  • Partial types
  • Anonymous methods
  • Iterators
  • Nullable types
  • Private setters (properties)
  • Method group conversions (delegates)
  • Covariance and Contravariance

C# 3.0
  • C# 3.0 was released by Microsoft with Visual Studio 2008. It is also compatible with Visual Studio 2010. C# 3.0 targets the .NET Framework 2.0 (Except LINQ/Query Extensions), .NET Framework 3.0 (Except LINQ/Query Extensions) and .NET Framework 3.5.
  • The new features added in C# 3.0 are as given below:
  • Implicitly typed local variables (var)
  • Object and collection initializers
  • Auto-Implemented properties
  • Anonymous types
  • Extension methods
  • Query expressions
  • Lambda expressions
  • Expression trees
  • Partial Methods
  • LINQ

C# 4.0
  • C# 4.0 was released by Microsoft with Visual Studio 2010. C# 4.0 targets .NET Framework 4.0.
  • The new features added in C# 4.0 are as given below:
  • Dynamic binding (Late binding)
  • Named and optional arguments
  • Generic Covariance and Contravariance
  • Embedded interop types ("NoPIA")

C# 5.0
  • C# 5.0 was released by Microsoft with Visual Studio 2012. C# 5.0 targets .NET Framework 4.5.
  • The new features added in C# 4.0 are as given below:
  • Asynchronous methods
  • Caller info attributes

C# 6.0
  • Expression Bodied Methods
  • Auto-property initializer
  • nameof Expression
  • Primary constructor
  • Await in catch block
  • Exception Filter
  • String Interpolation


2. Ques: Difference between value type and reference types ?
Answer:
Value Type
  • Value Type stored on stack.
  • Value Type Contains actual value.
  • Cannot contain null values. However this can be achieved by nullable types.
  • Value type is popped on its own from stack when they go out of scope.
  • Memory is allocated at compile time.
  • In value types, the variables each have their own copy of the data, and it is not possible for operations on one to affect the other.
  • Value types are derived from System.ValueType.
Reference Type
  • Reference Type stored on heap
  • Reference Type Contains reference to a value.
  • Can contain null values.
  • Required garbage collector to free memory.
  • Memory is allocated at run time.
  • In reference types, it is possible for two variables to reference the same object and thus possible for operations on one variable to affect the object referenced by the other variable.
  • Reference types are derived from System.Object.


3. Ques:  What are params in c# ? What is the use of params in C#?
Answer:
Params enables methods to receive variable numbers of parameters. With params, the arguments passed to a method are changed by the compiler to elements in a temporary array. This array is then used in the receiving method.
Ex:

class Program
{
  static void Main()
    {
        // Call params method with one to four int arguments.
        int sum1 = SumParameters(1);
        int sum2 = SumParameters(1, 2);
        int sum3 = SumParameters(3, 3, 3);
        int sum4 = SumParameters(2, 2, 2, 2);
    }

    static int SumParameters(params int[] values)
    {
        // Loop through and sum the integers in the array.
        int total = 0;
        foreach (int value in values)
        {
            total += value;
        }
        return total;
    }
}

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments of the type specified in the parameter declaration or an array of arguments of the specified type.


4. Ques:  What is the difference between Array and Collection?
Answer:
Array 
  • You need to specify the size of an array at the time of its declaration.It can not be resized.
  • The member of an array should be of the same data type.
  • Array is strong type. 
Collection
  • The size of a collection can be adjusted dynamically as per users requirement.It does not have fixed size
  • Collection can have different data types.
  • Collection is not strong type.

5. Ques:  What is the difference between managed and unmanaged code?
Answer:
Managed Code
  • Code which is executed by CLR (Common Language Runtime) is called Managed Code, any application which is developed in .Net framework is going to work under CLR, the CLR internally uses the Garbage Collector to clear the unused memory and also used the other functionalities like CTS, CAS etc.
  • managed code executed on fixed location in memory.
  • it  get advantages of CLR like memory management, thread managament,security, garbage mechanism etc.
Unmanaged Code
  • The unmanaged code is basically developed using other languages (other than .Net Framework), so it uses its own language runtime to execute the applications. The application runtime will take care of its memory management, security etc.
  • unmanaged code executes on random location in memory.
  • it doesn't get advantages of CLR like memory management, thread managament,security, garbage mechanism etc.

6. Ques: What is the use of using statement in C# ?
Answer:
The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.

There is 2  main use using statement. 
1:- Importing namespace example:using System; 
2:- Dispose the object  
 using(SqlConnection oConnection=new SqlConnection()) { oConnection.Open(); } 
if you won't close the connection it will close the connection automatically once SqlConnection has been executed completing.



7. Ques: What is Delegates in C# ? What is Use of Delegates ?
Answer:
Delegates in c# are type safe objects which are used to hold reference of one or more methods in c#.net. Delegates concept will match with pointer concept of c++ .
The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked

Whenever we want to create delegate methods we need to declare with delegate keyword and delegate methods signature should match exactly with the methods which we are going to hold like same return types and same parameters otherwise delegate functionality won’t work if signature not match with methods.

Delegates are two types
Single Cast Delegates
Multi Cast Delegates

Single Cast Delegates
Single cast delegate means which hold address of single method like as explained in above example.

Multicast Delegates
Multi cast delegate is used to hold address of multiple methods in single delegate. To hold multiple addresses with delegate we will use overloaded += operator and if you want remove addresses from delegate we need to use overloaded operator -=

Multicast delegates will work only for the methods which have return type only void. If we want to create a multicast delegate with return type we will get the return type of last method in the invocation list


8. Ques: What is the use of “Yield” keyword in C#?  What is use the of “Yield” keyword ?
Answer:
Yield helps you to provide custom stateful iteration over .NET collections.”

Use of “yield” keyword is
  • Customized iteration through a collection without creating a temporary collection.
  • Stateful iteration.
There are a few things to note when defining a function that uses yield. The yield statement can only appear inside an iterator block, which can be implemented as the body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
  • Unsafe blocks are not allowed.
  • Parameters to the method, operator, or accessor cannot be ref or out.
  • A yield return statement cannot be located anywhere inside a try-catch block. It can be located in a try block if the try block is followed by a finally block.
  • A yield break statement may be located in a try block or a catch block but not a finally block.

9. Ques:  What is the Task Parallel Library (TPL) in C#? 
Answer:
Task Parallel Library is new API's to simplify the process of adding parallelism and concurrency to applications. This set of API's is called the "Task Parallel Library (TPL)" and is located in the System.Threading and System.Threading.Tasks namespaces.
The Parallel class found in the System.Threading.Tasks namespace “provides library-based data parallel replacements for common operations such as for loops, for each loops, and execution of a set of statements”. In this article, we will use the  Invoke method of the Parallel class to call multiple methods, possibly in parallel.
 
   TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details. By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.

ASP.NET interview questions Part-7

1. Ques: What is differentiate between web.config and machine.config files ?
Answer:
Machine.Config
  • Machine.config is configuration file for all the application in the IIS.
  • Machine.config is automatically installed when you install Visual Studio. Net.
  • This is also called machine level configuration file.
  • Only one machine.config file exists on a server.
  • This file is at the highest level in the configuration hierarchy.
  • machine.config would be to share values between many applications on the server such as SMTP server settings
Web.Config
  • Web.config is a configuration file for a particular application or folder.
  • Web.Config is automatically created when you create an ASP.Net web application project.
  • This is also called application level configuration file.
  • This file inherits setting from the machine.config 
  • Web.config files contain application specific items such as database connection strings.

2. Ques: What is Global.asax file in asp.net ? What are the main event and their work in  ASP.Net Application ?
Answer:
The Global.asax file, also known as the ASP.NET application file, is an optional file that contains code for responding to application-level events raised by ASP.NET or by HttpModules. The Global.asax file resides in the root directory of an ASP.NET-based application.The Global.asax file is parsed and dynamically compiled by ASP.NET.The Global.asax file itself is configured so that any direct URL request for it is automatically rejected; external users cannot download or view the code written within it.

Main event in global.asax in given bellow.

Application_Start() 
Application_Start() event occurs when the application starts, which is the first time it receives a request from any user. It doesn’t occur on subsequent requests. This event is commonly used to create or cache some initial information that will be reused later.

Application_End()
Application_End() event occurs when the application is shutting down, generally because the web server has restarted. You can insert cleanup code here.

Application_BeginRequest()
Application_BeginRequest() event occurs with each request the application receives, just before the page code is executed.

Application_EndRequest()
Application_EndRequest() event occurs with each request the application receives, just after the page code is executed.

Session_Start()
Session_Start() event occurs whenever a new user request is received and a session is started.

Session_End()
Session_End() event occurs when a session times out or is pro grammatically ended. This event is only raised if you are using in-process session state storage (the InProc mode, not the StateServer or SQLServer modes ).

Application_Error()
Application_Error() event occurs in response to an un-handled error.


3. Ques: What is a Cookieless Session in ASP.Net?
Answer:
You can configure your application to store Session IDs not in a cookie, but in the URLs of pages in your site. By keeping the Session ID in the URL, ASP.NET stores the ID in the browser, in a manner of speaking, and can get it back when the user requests another page. Cookieless sessions can get around the problem of a browser that refuses cookies and allow you to work with Session state.

ASP.NET Session State by default uses a cookie to store session ID. Session ID is a unique string, used to recognize individual visitor between visits. But, if client's web browser doesn't support cookies or visitor has disabled cookies in web browser's settings, ASP.NET can't store session id on client's machine. In this case, new session will be created for every request. This behavior is useless because we can't remember information for certain visitor between two requests. We can say that, by default, sessions can't work if browser doesn't support cookies.

4. Ques:  What is Tracing in ASP.Net ?
Answer:
Tracing is a way to monitor the execution of your ASP.NET application.ASP.NET tracing enables you to follow a page's execution path, display diagnostic information at run time, and debug your application.
here are two ways to implement Tracing in ASP.Net :

Page Level Tracing
Application Level Tracing

Page level Tracing - Enabled on a page-by-page basis by adding "Trace=true" to the Page directive
Application Tracing - You can enable tracing for the entire application by adding tracing settings in web.config. In below example, pageOutput="false" and requestLimit="20"


5. Ques: What is the Difference between Client Side and Server Side Code?
Answer:
Client Side
  • The client-side environment used to run scripts in a browser. 
  • Code is written in a scripting language such as JavaScript and HTML.
  • The browser itself executes the code in response to a user action and no server round trip is involved.
  • Client browser executes code to dynamically modify the HTML. This is called Dynamic HTML.
  • Code is script and therefore it is interpreted.
Server Side
  • The server-side environment that runs a language in a web server. 
  • Code is written in VB, C# or other compiled languages.
  • Code is executed by the server during a roundtrip in response to a user request or action.
  • The server executes server side code and returns HTML code to the client browser for display.
  • Code is either compiled dynamically or precompiled into assemblies.

6. Ques:  What are the different types of IIS Isolation Levels in ASP.Net?
Answer:
IIS5 supports three isolation levels

Low (IIS Process)

  • ASP pages run in INetInfo.Exe, the main IIS process
  • ASP crashes, IIS crashes
Medium (Pooled)

  • ASP runs in a different process, which makes this setting more reliable
  • If ASP crashes IIS won't.
High (Isolated)

  • Each ASP application runs out-process in its own process space
  • If an ASP application crashes, neither IIS nor any other ASP application will be affected