This post contains the most popular interview questions for the .net framework. If you are a .net developer you must have faced these questions in your interviews or in your future interview. Here, I have listed 30 important interview questions with answers. Hope it will be a good refresher for the guys, preparing for an interview.
Q1.The key component of the .net framework?
- CFL – Class framework library. Such as System.IO, System.Data etc
- CLR – Common Language Runtime.
- Other application templates related component/library which resides on top of CFL and CLR such as windows application, WCF, WebAPI related library.
Q2.Features of .net framework?
- Multiple Language Support
- Automatic Garbage Collector
- Type Safety
- Security
- Rich CLR
- Portability
- Legacy Support
Q3. What is IL – Intermediate Language?
Q4. Source code execution flow?
- Source code first converted to IL by CLR.
- IL converted to byte code[numeric code] and assembly will be created.
- Assembly/Byte code is then converted by JIT and native code will be generated
- Native code will be executed by the operating system.
Q5. What is CLR? What are the features of CLR?
- Type Safety
- Thread Management
- Input/Output Management
- Memory Management- Garbage Collection
- Converting Source code to IL and IL to byte code.
- Security
- Interoperability
Q6. What are CTS and CLS? Difference between CTS and CLS?
- Both CTS and CLS is part of CLR.
- CTS acronyms for the common type system. CTS defines how the data types are created and managed in the .net run-time environment. It also says that all the data types present in various .net supported language should adapt to the same data structure. Such as, While converting vb.net code to csharp.net, integer of vb.net should be shown as Int32 to C# by IL.
- CLS acronyms for common language specification. CLS considered as language guidelines for the third party language developer which targets .net frameworks. Its guidelines for making third party language as CLR compliance. CLS says as guidelines that all .net supported language has to generate exact same type of IL when compiled.
Q7. What is JIT? Types of JIT?
- Pre JIT – It compiles and cached whole code at once and will not compile on demand. This is done by using a utility named ngen.exe.
- Normal JIT – It compiles the required IL Code and cached that only part.
- Econo JIT – This works the same as normal JIT but not caching any part of code. Used for small handheld devices having memory constraints.
Q8. What is NGEN utility?
Q9. What is assembly? Type of assembly? What is manifest?
Q10. What is a strong name and how to signed strong name to the assembly?
Q11. Strongly typed and weakly typed assembly?
Q12. How to add the assembly to GAC?
Q13. Where does GAC locate in the system?
Q14. GAC – Use of GAC?
For Q9,Q10,Q11,12,13,14 : Please see this post – https://techdiksha.com/gac-global-access-cache-dll-hell-problem/
Q15. What is a satellite assembly?
Q16. Compare namespace and assembly?
Q17. What is ILASM & ILDASM ?
Assembled IL can be disassembled into code again using ILDASM(IL Disassembler). There are other tools such as .net reflector that can decompile IL into the high-level language target for reverse engineering.
Q18. What is the delay signing?
Q19. DLL Hell problem & how GAC solve DLL hell problem?
Q20. What is Type safety?
Type safety is a language feature which promotes the robustness of the program. It defines several rules for object type verification, value assignment, range verification etc. For example :
– one data type can not be assigned to other data types if they are not compatible [different]. For example, the string should not be assigned implicitly to an int data type or Customer data type should not be assigned to the Order data type.
– int variable should be assigned before use.
– checking of upper bound for the array.
C# is type-safe language as it does compile-time type verification for the object by using metadata information. It increases robustness and consistency.
Q21. What are Upcasting and Downcasting?
C# Support Upcasting and Downcasting.
Upcasting means small-sized data type being assigned to large-sized data types. For example, int data type can be assigned to long implicitly.
⧭ Upcasting can be implicit.
⧭ Upcasting does not have any data loss.
Downcasting means large-sized data type is assigned to the small-sized data type. for example, long datatype assigned to int.
⧭ Downcasting may lead to data loss. For example, when we convert double 11.15 to int, the decimal point will be lost.
Q21. Managed and Unmanaged code?
Managed code is the source code which runs under the supervision of CLR means, CLR keeping track of memory allocation-deallocation, type safety of the managed object. For such a managed object, CLR facilitates automatic garbage collection.
Unmanaged code: code which does not run under the CLR. Means, these objects are not a native object of CLR, such object is run directly by the OS. In .net we use such object by some wrapper classes, such as database connection object, win32 related object, file streams etc. These objects are not managed by CLR, developers need to takes care of the memory management such as deallocation, overflow, leaks etc.
Q22. How does .net do automatic garbage collection?
Q23. What is the generation and type of generation? Why GC use generation?
For all managed code, .net creates a heap to keep track of objects. Called Managed Heap. These Managed heap further categorized in the generation. Generation is segregation of managed heap based on the longevity of the object.
Generation :
.net have Generation 0 , Generation 1 and Generation 2.
Here, Generation 0 Contains short-lived object such as temporary variable and Generation 2 consists of a long-lived object such as static object.
Why Generation:
For efficient memory management. GC visits Generation 0 frequently than Generation 1 because generally, we have the more short-lived object in code than the long one.
Q24. Can we call GC explicitly by code?
YES, We have method GC.Collect()
Q25. When does garbage collector trigger? Or How to trigger the garbage collector?
Below is the scenario when the garbage collector gets triggered :
⧭ When memory for the generation gets exhausted.
⧭ When Application gets closed
⧭ When we call GC.Collect()
Q26. Difference between Finalize and Dispose?
- Finalize is un-deterministic i.e, generally, we do not know when GC will trigger. Whereas, Dispose we call it explicitly by code all the time.so, it’s deterministic.
- We implemented finalize through using Destructor(~) with class Whereas we define Dispose() by implementing the IDisposable interface.
- Dispose is used for freeing the unmanaged resources such as DB connection, file etc Whereas, Finalize is used for managed resources, however, we can use Dispose and Finalize for both type of objects using IDisposable Pattern.
Q27. Which gives a better performance – finalize or dispose?
Dispose gives better performance.
Because Finalize used 2 step process for freeing the memory.
Step 1, It creates a pool of object/class which have Destructor and does not have any references.
Step 2, Then it frees the resources and reclaims memory.
Q28. What is IDisposable pattern? Why should we use it?
IDisposable pattern is used for combining Finalize and Dispose method implementation.
With this pattern, we can achieve efficient memory management. This pattern also allows finalize() to clean unmanaged resources. It also provides a safety net in case if the user forgets to call Dispose method, as it will be cleaned when destructor will be invoked.
// Code
public static MyClass : IDisposable
{
Private bool statusDisposed = false;
// IDisposable interface’s method implementation
Public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
Protected virtual void Dispose(bool isdisposing)
{
if(!statusDisposed )
{
if(isdisposing)
{
// TODO : Clean up managed objects.
}
// TODO : Clean up unmanaged objects.
statusDisposed = true;
}
}
~ MyClass()
{
Dispose(false);
}
}
Q29. What is Reflection? Why do we use reflection?
Updating soon…
Q30. What is Generics? What is the advantage of using Generics?
Generics:
Generic is a feature of C# which enables the class, methods, property to work with any type. For example, List is a generic collection, so we can create a list of any type such as int, string etc.
The need for using generic is to eliminate the process of boxing and unboxing.
For example, ArrayList is a non-generic collection, it can store any data type value.
While storing items into the ArrayList, each item (whether it is value type or reference type) is converted to Object type and when retrieving, item needs to convert back to their respective type.
here, boxing and unboxing, gives a performance hit as it needed to allocate and deallocate memory for several temp variable.
So, using generic, we can use one type at one time. For example, List<int> will contain only int value. List<string> will contains the only string. here, We don’t need any boxing and unboxing involved.
Conclusion:
This was the first post in this series of interview questions. In the next upcoming post, interview questions on other topics will be added such as C#, object-oriented, data structure, SQL server, web API.
Feel free to comment and any questions on these topics. I would love to clarify and updated in the upcoming interview questions article here. Best of luck.