.NET Framework Interview Questions

1. What is the .NET Framework?

The .NET Framework is an application development platform that provides services for building, deploying, and running desktop, web, and phone applications and web services. It consists of two major components: the common language runtime (CLR), which provides memory management and other system services, and an extensive class library, which includes tested, reusable code for all major areas of application development.

The .NET Framework provides the following services for application developers:

· Memory management

· A common type system

· An extensive class library

· Development frameworks and technologies.

· Language interoperability

· Version compatibility

· Side-by-side execution

· Multitargeting

2. What are the main components of .NET Framework?

The .NET Framework has two main components: the common language runtime and the .NET Framework class library.

The common language runtime manages memory, thread execution, code execution, code safety verification, compilation, and other system services. These features are intrinsic to the managed code that runs on the common language runtime.

The .NET Framework class library is a collection of reusable types that tightly integrate with the common language runtime. The class library is object oriented, providing types from which your own managed code can derive functionality. This not only makes the .NET Framework types easy to use, but also reduces the time associated with learning new features of the .NET Framework.

3. What is CLR [1]?

· CLR stands for Common Language Runtime and it forms the heart of the .NET framework.

· The CLR can be compared to the Java Virtual Machine or JVM in Java.

· CLR provides an environment to execute .NET applications on target machines. CLR is also a common runtime environment for all .NET code irrespective of their programming language, as the compilers of respective language in .NET Framework convert every source code into a common language known as MSIL or IL (Intermediate Language).CLR provides services such as memory management, thread management, security management, code verification, compilation, and other system services. It enforces rules that in turn provide a robust and secure execution environment for .NET applications.

Following are the responsibilities of CLR:

· Garbage Collection

· Automatic memory management

· Code Access Security

· Code Verification

· JIT compilation of .NET code

4. What is an IL?

Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. IL is then converted to machine code at the point where the software is installed, or at run-time by a Just-In-Time (JIT) compiler.

5. What is CTS?

Common Type System (CTS) describes the data types that can be used by managed code. CTS defines how these types are declared, used and managed in the runtime. It facilitates cross-language integration, type safety, and high performance code execution. The rules defined in CTS can be used to define your own classes and values.

6. What is CLS?

Common Language Specification (CLS) defines the rules and standards to which languages must adhere to in order to be compatible with other .NET languages. This enables C# developers to inherit from classes defined in VB.NET or other .NET compatible languages.

7. What is managed code?

The code that runs within the Common Language Runtime is called Managed Code.

In short, all IL are managed code. However, if you are using some third party software example VB6 or C++ component they are unmanaged code; as .NET runtime (CLR) does not have control over the source code execution of these languages.

8. What are the differences between managed and unmanaged code?

· Managed Code is the code that is executed directly by the CLR instead of the operating system. The code compiler first compiles the managed code to intermediate language (IL) code, also called as MSIL code. This code doesn’t depend on machine configurations and can be executed on different machines.

· Unmanaged Code is the code that is executed directly by the operating system outside the CLR environment. It is directly compiled to native machine code which depends on the machine configuration.

In the managed code, since the execution of the code is governed by CLR, the runtime provides different services, such as garbage collection, type checking, exception handling, and security support. These services help provide uniformity in platform and language-independent behavior of managed code applications. In the unmanaged code, the allocation of memory, type safety, and security is required to be taken care of by the developer. If the unmanaged code is not properly handled, it may result in memory leak. Examples of unmanaged code are ActiveX components and Win32 APIs that execute beyond the scope of native CLR.

9. Which is the execution process for managed code?

A piece of managed code is executed as follows:

1. Choosing a language compiler

2. Compiling the code to MSIL

3. Compiling MSIL to native code

4. Executing the code

10. What is MSIL?

When the code is compiled, the compiler translates your code into Microsoft intermediate language (MSIL). The common language runtime includes a JIT compiler for converting this MSIL then to native code.

MSIL contains metadata that is the key to cross language interoperability. Since this metadata is standardized across all .NET languages, a program written in one language can understand the metadata and execute code, written in a different language. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations.

11. What is JIT?

JIT is a compiler that converts MSIL to native code. The native code consists of hardware specific instructions that can be executed by the CPU.

Rather than converting the entire MSIL (in a portable executable [PE] file) to native code, the JIT converts the MSIL as it is needed during execution. This converted native code is stored so that it is accessible for subsequent calls.

12. What is the role of the JIT compiler in .NET Framework?

The JIT compiler is an important element of CLR, which loads MSIL on target machines for execution. The MSIL is stored in .NET assemblies after the developer has compiled the code written in any .NET-compliant programming language, such as Visual Basic and C#.
JIT compiler translates the MSIL code of an assembly and uses the CPU architecture of the target machine to execute a .NET application. It also stores the resulting native code so that it is accessible for subsequent calls. If a code executing on a target machine calls a non-native method, the JIT compiler converts the MSIL of that method into native code. JIT compiler also enforces type-safety in runtime environment of .NET Framework. It checks for the values that are passed to parameters of any method.

13. What is portable executable (PE)?

PE is the file format defining the structure that all executable files (EXE) and Dynamic Link Libraries (DLL) must use to allow them to be loaded and executed by Windows. PE is derived from the Microsoft Common Object File Format (COFF). The EXE and DLL files created using the .NET Framework obey the PE/COFF formats and also add additional header and data sections to the files that are only used by the CLR.

14. What is an application domain?

Application domain is the boundary within which an application runs. A process can contain multiple application domains. Application domains provide an isolated environment to applications that is similar to the isolation provided by processes. An application running inside one application domain cannot directly access the code running inside another application domain. To access the code running in another application domain, an application needs to use a proxy.

15. What is an assembly?

· An assembly is the fundamental unit for application development and deployment in the .NET Framework.

· An assembly consists of one or more files (dlls, exes, html files,etc.) and represents a group of resources, type definitions, and implementations of those types. An assembly may also contain references to other assemblies. These resources, types and references are described in a block of data called a manifest. The manifest is part of the assembly, thus making the assembly self-describing.

· An assembly is completely self-describing. An assembly contains metadata information, which is used by the CLR for everything from type checking an security to actually invoking the components methods. As all information is in the assembly itself, it is independent of registry. This is the basic advantage as compared to COM where the version was stored in registry.

· Multiple versions can be deployed side by side in different folders. These different versions can execute at the same time without interfering with each other. Assemblies can be private or shared. For private assembly deployment, the assembly is copied to the same directory as the client program that references it. No registration is needed, and no fancy installation program is required. When the component is removed, no registry cleanup is needed, and no uninstall program is required. Just delete it from the hard drive. In shared assembly deployment, an assembly is installed in the Global Assembly Cache (or GAC). The GAC contains shared assemblies that are globally accessible to all .NET applications on the machine.

16. What are the contents of assembly?

A static assembly can consist of four elements:

· Assembly manifest: Contains the assembly metadata. An assembly manifest contains the information about the identity and version of the assembly. It also contains the information required to resolve references to types and resources.

· Type metadata: Binary information that describes a program.

· Microsoft intermediate language (MSIL) code

· A set of resources.

17. What are the different types of assembly?

Assemblies can also be private or shared. A private assembly is installed in the installation directory of an application and is accessible to that application only. On the other hand, a shared assembly is shared by multiple applications. A shared assembly has a strong name and is installed in the GAC.

We also have satellite assemblies that are often used to deploy language-specific resources for an application.

18. What is a dynamic assembly?

A dynamic assembly is created dynamically at run time when an application requires the types within these assemblies.

19. What is a strong name?

You need to assign a strong name to an assembly to place it in the GAC and make it globally accessible. A strong name consists of a name that consists of an assembly’s identity (text name, version number, and culture information), a public key and a digital signature generated over the assembly. The .NET Framework provides a tool called the Strong Name Tool (Sn.exe), which allows verification and key pair and signature generation.

Strong names use public key cryptography (PKC) to ensure that no one can spoof it. PKC use public key and private key concept

20. What is GAC?

The global assembly cache (GAC) is a machine-wide code cache that stores assemblies specifically designated to be shared by several applications on the computer. You should share assemblies by installing them into the global assembly cache only when you need to.

21. How to add and remove an assembly from GAC?

To add an assembly to the GAC is needed to perform the following steps:

1. Create a strong name using sn.exe tool eg: sn -k mykey.snk

2. In AssemblyInfo.cs, add the strong name

a. e.g.: [assembly: AssemblyKeyFile("mykey.snk")]

3. Recompile project, and then install it to GAC in two ways:

a. Drag & Drop it to assembly folder (C:\WINDOWS\assembly OR C:\WINNT\assembly) (shfusion.dll tool)

b. GACUTIL -i abc.dll

c. Using Microsoft Installer Package. You can get download of installer from www.microsoft.com

22. What is the caspol.exe tool used for?

The caspol tool grants and modifies permissions to code groups at the user policy, machine policy, and enterprise policy levels.

23. What is a Garbage collection?

A performs periodic checks on the managed heap to identify objects that are no longer required by the program and removes them from memory.

Garbage collection prevents memory leaks during execution of programs. is a low-priority process that manages the allocation and deallocation of memory for your application. It checks for the unreferenced variables and objects. If GC finds any object that is no longer used by the application, it frees up the memory from that object.

24. What are generations and how are they used by the ?

Generations are the division of objects on the managed heap used by the . This mechanism allows the to perform highly optimized garbage collection. The unreachable objects are placed in generation 0, the reachable objects are placed in generation 1, and the objects that survive the collection process are promoted to higher generations.

25. How many types of generations are there in a ?

Memory management in the CLR is divided into three generations that are build up by grouping memory segments. Generations enhance the garbage collection performance. The following are the three types of generations found in a :

· Generation 0 – When an object is initialized, it is said to be in generation 0.

· Generation 1 – The objects that are under garbage collection process are considered to be in generation 1.

· Generation 2 – Whenever new objects are created and added to the memory, they are added to generation 0 and the old objects in generation 1 are considered to be in generation 2.

26. Can we force to run?

System.GC.Collect () forces to run. This is not recommended but can be used if situations arise.

27. Which are the differences between the Dispose() and Finalize() Method?

CLR uses the Dispose and Finalize methods to perform garbage collection of run-time objects of .NET applications.

· The Finalize method is called automatically by the runtime. CLR has a (GC), which periodically checks for objects in heap that are no longer referenced by any object or program. It calls the Finalize method to free the memory used by such objects.

· The Dispose method is called by the programmer. Dispose is another method to release the memory used by an object. The Dispose method needs to be explicitly called in code to dereference an object from the heap. The Dispose method can be invoked only by the classes that implement the IDisposable interface.

28. Is there a way to suppress the finalize process inside the forcibly in .NET?

Use the GC.SuppressFinalize() method to suppress the finalize process inside the forcibly in .NET.

29. What is Ilasm.exe used for?

Ilasm.exe is a tool that generates PE files from MSIL code. You can run the resulting executable to determine whether the MSIL code performs as expected.

30. What is Ildasm.exe used for?

Ildasm.exe is a tool that takes a PE file containing the MSIL code as a parameter and creates a text file that contains managed code.

31. How to prevent my .NET DLL to be decompiled?

By design, .NET embeds rich Meta data inside the executable code using MSIL. Anyone can easily decompile your DLL back using tools like ILDASM (owned by Microsoft) or Reflector for .NET which is a third party. Secondly, there are many third party tools, which make this decompiling process a click away. So any one can easily look in to your assemblies and reverse engineer them back in to actual source code and understand some real good logic, which can make it easy to crack your application.

The process by which you can stop this reverse engineering is using obfuscation. It is a technique, which will foil the decompilers. Many third parties (XenoCode, Demeanor for .NET) provide .NET obfuscation solution. Microsoft includes one that is Dotfuscator Community Edition with Visual Studio.NET.

32. What is the ResGen.exe tool used for?

ResGen.exe is a tool that is used to convert resource files in the form of .txt or .resx files to common language runtime binary .resources files that can be compiled into satellite assemblies.

33. What is the Manifest?

Assembly metadata is stored in Manifest. Manifest contains all the metadata needed to do the following things:

· Version of assembly

· Security identity

· Scope of the assembly

· Resolve references to resources and classes.

The assembly manifest can be stored in a PE file either (an .exe or) .dll with Microsoft intermediate language (MSIL code with Microsoft intermediate language (MSIL) code or in a stand-alone PE file, that contains only assembly manifest information.

34. Is versioning applicable to private assemblies?

Versioning concept is only applicable to global assembly cache (GAC) as private assembly lie in their individual folders. This does not mean versioning is not needed , you can still version it to have better version control on the project.

35. What is side-by-side execution. Can two applications, one using private assembly and the other using the shared assembly be stated as side-by-side executables?

Side-by-side execution enables you to run multiple versions of an application or component and CLR on the same computer at the same time. As versioning is applicable only to shared assemblies and not to private assemblies, two applications, one using a private assembly and other using a shared assembly, cannot be stated as side-by-side executables.

36. Which is the root namespace for fundamental types in .NET Framework?

System.Object is the root namespace for fundamental types in .NET Framework.

37. What is code access security (CAS)?

Code access security (CAS) is part of the .NET security model that prevents unauthorized access of resources and operations, and restricts the code to perform particular tasks.

38. How can you turn-on and turn-off CAS?

You can use the Code Access Security Tool (Caspol.exe) to turn security on and off.

· To turn off security, type the following command at the command prompt: caspol -security off

· To turn on security, type the following command at the command prompt: caspol -security on

39. What is lazy initialization?

Lazy initialization is a process by which an object is not initialized until it is first called in your code. Lazy initialization helps you to reduce the wastage of resources and memory requirements to improve performance. It also supports thread-safety.

40. What is difference between System.String and System.StringBuilder classes?

String and StringBuilder classes are used to store string values but the difference in them is that String is immutable (read only) by nature, because a value once assigned to a String object cannot be changed after its creation. When the value in the String object is modified, a new object is created, in memory, with a new value assigned to the String object. On the other hand, the StringBuilder class is mutable, as it occupies the same space even if you change the value. The StringBuilder class is more efficient where you have to perform a large amount of string manipulation.

41. What is reflection?

All .NET assemblies have metadata information stored about the types defined in modules. This metadata information can be accessed by mechanism called as “Reflection”. System.Reflection can be used to browse through the metadata information.

42. What is the difference between VB.NET and C#?

Well this is the most debatable issue in .NET community and people treat languages like religion. It is a subjective matter which language is best. Some like VB.NET’s natural style and some like professional and terse C# syntaxes. Both use the same framework and speed is very much equivalents. Still let us list down some major differences between them:

Advantages VB.NET

· Has support for optional parameters that makes COM interoperability much easy.

· With Option Strict off late binding is supported. Legacy VB functionalities can be used by using Microsoft.VisualBasic namespace.

· Has the WITH construct which is not in C#.

· The VB.NET parts of Visual Studio .NET compiles your code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.

Advantages of C#

• Use of this statement makes unmanaged resource disposal simple.

• Access to Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies).This is the major difference that you can access unmanaged code in C# and not in VB.NET.

43. What is the Difference Between Application Exception and System Exception

All exception derives from Exception Base class. Exceptions can be generated programmatically or can be generated by system. Application Exception serves as the base class for all application specific exception classes. It derives from Exception but does not provide any extended functionality. You should derive your custom application exceptions from Application Exception. Application exception is used when we want to define user defined exception. While system exception are all which are defined by .NET

44. what is the difference between Convert.ToString() and .ToString() method?

The basic difference between them is Convert.ToString() function handles NULLS while .ToString() does not it will throw a NULL reference exception error. So as a good coding practice using Convert.ToString() is always safe.

45. If we have two version of same assembly in GAC how do we make a choice? 46. What is a satellite assembly?

When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

47. What is Boxing and Unboxing?

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Boxing is used to store value types in the garbage-collected heap.

Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system, in which a value of any type can be treated as an object.

For the unboxing of value types to succeed at run time, the item being unboxed must be a reference to an object that was previously created by boxing an instance of that value type. Attempting to unbox null causes a NullReferenceException. Attempting to unbox a reference to an incompatible value type causes an InvalidCastException.

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.

48. Why should boxing be avoided?

It adds overhead.

49. What is object pooling?

Object Pooling is something that tries to keep a pool of objects in memory to be re-used later and hence it will reduce the load of object creation to a great extent.
Object Pool is nothing but a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.

50. How to create a strong name?

To create a strong name key use following code:

C:\>sn -k s.snk

Strong name key is used to specify the strong name used to deploy any application in the GAC (Global Assembly Catch) in the system.

https://hectorea.wordpress.com/2012/08/06/net-framework-interview-questions-2/

https://hectorea.wordpress.com/

你可能感兴趣的:(.NET Framework Interview Questions)