.Net Interview Preparation

.NET Framework / Core


Common Language Runtime (CLR):

1. What is CLR and how does it work in .NET applications?

The Common Language Runtime (CLR) is the core component of the .NET framework, responsible for managing the execution of .NET applications. It provides various services such as memory management, security enforcement, exception handling, and thread management.


2. How does the CLR manage memory?

CLR manages memory via the Garbage Collector (GC), which automates the process of allocating and releasing memory in .NET applications.


3. How does CLR manage exception handling?

CLR provides a structured approach to handle runtime exceptions via the Common Type System (CTS).


4. What is Just-In-Time (JIT) compilation in CLR?

JIT compilation is the process of converting the intermediate language (IL) into native machine code that is specific to the target machine.


5. What are Assemblies and how do they relate to CLR?

Assemblies are the building blocks of .NET applications and contain the compiled code (IL), metadata, and resources for execution.


6. What is Code Access Security (CAS) in CLR?

CAS is a security model in .NET that determines whether code has the necessary permissions to perform certain operations based on its origin and other identity factors.


7. How does the CLR handle threading?

CLR provides a managed threading environment that allows for multi-threading within .NET applications.


Garbage Collection:


1. How does the garbage collector work in .NET?

The Garbage Collector (GC) in .NET is a part of the runtime environment that automates memory management. Its primary purpose is to reclaim memory used by objects that are no longer needed, thus preventing memory leaks and optimizing memory usage.


2. Can you explain the different phases of garbage collection?

Garbage collection in .NET involves several phases, each designed to ensure efficient and effective cleanup of memory.

3. What are Generations in garbage collection?

The concept of Generations helps the GC optimize performance by dividing objects into different groups based on their age.

The GC periodically checks for objects that are no longer referenced by the application and reclaims that memory, making it available for new allocations. Generational Garbage Collector focuses on collecting objects in Gen 0 and Gen 1 first, as they tend to be short-lived, reducing overhead.


4. What are Large Object Heaps (LOH) and how does the GC handle them?

The Large Object Heap (LOH) is a separate part of the managed heap for objects that are larger than 85,000 bytes. These objects are not moved during garbage collection to avoid the overhead of relocating large objects.


5. What are the impacts of Garbage Collection on application performance?

Garbage collection can impact performance in several ways:


6. How can you tune garbage collection for performance in .NET?

Several techniques can be used to optimize garbage collection performance:


7. What is GC latency mode and how does it affect garbage collection?

GC Latency Mode allows you to adjust the behavior of the garbage collector to balance between throughput and responsiveness:


8. How does GC handle finalization in .NET?

Finalization is the process of cleaning up resources before an object is collected. In .NET, this is typically handled using finalizers:


Threading and Task: 


1. Explain the concept of threading in .NET.

Threading in .NET allows an application to perform multiple tasks simultaneously. It involves creating and managing individual threads within a process, where each thread can execute independently of others. Threads in .NET share the same memory space but run their own execution paths.


2. What is the difference between Process and Thread?


3. What is the difference between a Task and a Thread in .NET?


4. Explain Multithreading

Multithreading refers to the ability of a CPU, or a single core in a multi-core processor, to execute multiple threads concurrently. In .NET, multithreading allows you to run multiple tasks in parallel, improving application performance, particularly for tasks that can be performed independently.

Key concepts:

Multithreading is useful in scenarios such as:


5. How do you manage multithreading in .NET applications?


6. What is a ThreadPool in .NET

The ThreadPool is a collection of threads that can be reused for different tasks, reducing the overhead of creating and destroying threads.


7. What is a Task Parallel Library (TPL)

The Task Parallel Library (TPL) is a set of APIs that simplifies parallel programming by abstracting the complexity of threading. It provides efficient and scalable execution of tasks in parallel.


8. What is async and await in .NET

async and await are key keywords for asynchronous programming in .NET, used to make code non-blocking and responsive.

Example:

public async Task<string> FetchDataAsync()

{

    HttpClient client = new HttpClient();

    string result = await client.GetStringAsync("https://example.com");

    return result;

}



9. What are thread-safe collections in .NET?

ConcurrentBag, ConcurrentDictionary, BlockingCollection, etc., are thread-safe collections in .NET that allow multiple threads to access them concurrently without the need for explicit locking.


10. Explain the lock keyword in C# and its importance in multithreading.

The lock keyword is used to prevent multiple threads from simultaneously executing a block of code, ensuring thread safety.


11. What is a deadlock and how can it be avoided in multithreading?

A deadlock occurs when two or more threads are blocked forever, each waiting on the other to release a resource. It can be avoided by using proper synchronization mechanisms like Monitor.TryEnter, acquiring locks in the same order, and using timeout for locks.



Async/Await: 

10. How does async/await work in .NET? 

11. Can you give an example of asynchronous programming? 

12. What is the role of Async and Await?


Memory Management: 

13. How do you manage memory in .NET applications? 

14. What are Large Object Heaps (LOH), Dispose Pattern, and Finalizers? 

15. What is the difference between “Dispose” and “Finalize”? 

16. What is the difference between “Finalize” and “Finally” methods? 

17. Can we force Garbage Collector to run?


Reflection: 

18. What is Reflection in .NET, and how is it used? 

19. What’s the difference between Reflection and Dynamic in C#?


Dependency Injection: 

20. What is Dependency Injection, and why is it used? 

21. What are the differences between AddSingleton, AddScoped, and AddTransient in .NET Core? 

22. How to implement Dependency Injection in .NET Core? 

23. What are the advantages of Dependency Injection in .NET Core?


Entity Framework (EF): 

24. What is Entity Framework, and how does it differ from ADO.NET? 

25. Explain code-first migration in Entity Framework. 

26. What are DbContext and DbSet in EF? 

27. How Entity Framework works? OR How to setup EF? 

28. What is meant by DBContext and DBSet? 

29. What are the different types of development approaches used with EF? 

30. What is the difference between LINQ to SQL and Entity Framework?


SOLID Principles: 

31. Can you explain the SOLID principles in the context of .NET development?


Factory Design Pattern: 

32. Explain the factory design pattern with an example in C#. 

33. What’s the difference between a Factory Method and an Abstract Factory?


Middleware: 

34. What is middleware in ASP.NET Core? 

35. How would you implement custom middleware to handle exceptions or logging? 

36. What’s the difference between filters and middleware?


Difference between .NET Core and .NET Framework: 

37. How do .NET Core and .NET Framework differ in terms of architecture and features?