Important C#(CSharp) Interview Questions with Answers for Freshers - Part 1.

Important C#(CSharp) Interview Questions with Answers for Freshers - Part 1.

This article is for freshers who are looking to crack C# Interviews. It will be a 4 part series which will cover some basic questions asked in C# Interviews.

Watch our youtube video which covers 30 most asked C# Interview Questions with answers

Q1. What's the difference between a .NET and C#?

Note: - This probably would not come as a direct question. But its possible that you get in to a discussion about the differences. Just a warning even unknowingly never ever say C# and .NET are one and the same thing. You will create a negative dent in the interview.

NET is a framework and C# is a programming language, that’s what interviewer wants to hear.

.NET framework has collection of ready-made libraries. So for example you want List, Dictionary it has “System.collections.dll” , you want to make HTTP calls it has “System.Net.Http.dll” and so on.

While C# is programming language it has syntax, grammar, it has those “for loops”,” IF” conditions and so on. C# language invokes / orchestrates .NET framework libraries to create an application.

Q2. Differentiate between .NET framework vs .NET Core vs .NET 5.0 ?

No alt text provided for this image

Q3. What is an IL code?

IL code is a partially compiled code.

Note: - Half compiled means this code is not yet compiled to machine/CPU specific instructions.

Q4. Why IL code is not fully compiled?

We do not know in what kind of environment .NET code will run. In other words we do not know what can be the end operating system, CPU configuration, machine configuration, security configuration etc. So the IL code is half compiled and on runtime this code is compiled to machine specific using the environmental properties (CPU,OS, machine configuration etc).

Q5. Who compiles the IL code and how does it work?

IL code is compiled by JIT (Just in time compiler).

Q6. How does JIT compilation work?

JIT compiles the code just before execution and then saves this translation in memory. Just before execution JIT can compile per-file, per function or a code fragment.

Q7. What are different types of JIT?

In Microsoft .NET there are 3 types of JIT compilers:-?

  • Normal-JIT (Default): - Normal-JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.
  • Econo-JIT: - Econo-JIT compiles only those methods that are called at runtime. However, these compiled methods are not stored in cache so that RAM memory can be utilized in an optimal manner.
  • Pre-JIT: - Pre-JIT compiles complete source code into native code in a single compilation cycle. This is done at the time of deployment of the application. We can implement Pre-jit by using ngen.exe.?

Normal-jit is the default implementation and it produces optimized code. Econo-jit just replaces IL instruction with native counterpart. It does not do any kind of optimization. Econo-jit does not store the compiled code in cache so it requires less memory.

The choice of Normal-JIT and Econo-JIT is decided internally. Econo-JIT is chosen when devices have limitation memory and CPU cycle issues like windows CE powered device. When there is no memory crunch and CPU power is higher than Normal-JIT is used.

Pre-JIT is implemented by using ngen.exe which is explained in the next question.

Q8. What is Native Image Generator (Ngen.exe)?

Ngen stores full compiled.NET native code in to cache. In other words rather than dynamically compiling the code on run time a full image of native compiled code is stored in cache while installing the application. This leads to better performance as the assembly loads and execute faster.

In order to install full compiled native code in cache we can execute the below command line from your visual studio command prompt.

No alt text provided for this image

Q9. So does it mean that NGEN.EXE will always improve performance?

No, it’s not always necessary that ngen.exe produces optimized code because it uses the current environments parameters which can change over a period of time. For instance a code compiled in windows XP environment will not be the optimized code to run under windows 2008 server. So we need to once test with ‘ngen’ and without ‘ngen’ to conclude if really the performance increases.?

Q10. Is it possible to view the IL code?

Yes by using ILDASM simple tool we can view aIL code of a DLL or EXE. In order to view IL code using ILDASM , go to visual studio command prompt and run “ILDASM.EXE”. Once ILDASM is running you view the IL code.

Q11. What is a CLR?

CLR (Common language run time) is the heart of.NET framework and it does 4 primary important things:-

  • Garbage collection
  • CAS (Code Access security)
  • CV (Code verification)
  • IL to Native translation.

Note: - There are many other uses of CLR but I have kept it short from the interview point of view. In the further section we will go in depth of these questions.?

Q12. What is the difference between managed and unmanaged code?

Code that executes under CLR execution environment is called as managed code. Unmanaged code executes outside CLR boundary. Unmanaged code is nothing but code written in C++ , VB6 , VC++ etc. Unmanaged codes have their own environment in which the code runs and it’s completely outside the control of CLR.

Q13. What is a garbage collector?

Garbage collector is a feature of CLR which cleans unused managed (it does not clean unmanaged objects) objects and reclaims memory. It’s a back ground thread which runs continuously checks if there are any unused objects whose memory can be claimed. (GC does not claim memory of unmanaged objects.?)

Note: - Garbage collector is one of the very important interview topics due to complexity of generations, double GC loop because of destructor and the implementation of finalize and dispose pattern. So please do go through the video of “What is Garbage collection, Generation, Finalize, Dispose and Idisposable?” to ensure that you understand the fundamentals well.

Q14. What are generations in Garbage collector (Gen 0, 1 and 2)?

Generations defines age of the object. There are three generations:-?

  • Gen 0:- When application creates fresh objects they are marked as Gen 0.
  • Gen 1:- When GC is not able to clear the objects from Gen 0 in first round it moves them to Gen 1 bucket.
  • Gen 2:- When GC visits Gen 1 objects and he is not able to clear them he moves them gen 2.

Generations are created to improve GC performance. Garbage collector will spend more time on Gen 0 objects rather than Gen 1 and Gen 2 thus improving performance.

Note: - More the objects in Gen 0, more your application is stable.?

Q15. Garbage collector cleans managed code, howdo we clean unmanaged code?

Garbage collector only claims managed code memory. For unmanaged code you need to put clean up in destructor / finalize.

Q16. But when we create a destructor the performance falls down?

Yes, when we define a destructor, garbage collector does not collect these objects in the first round. It moves them to Gen 1 and then reclaims these objects in the next cycle.

As more objects are created in Gen 1 the performance of the application falls down because more memory is consumed.

Q17. So how can we clean unmanaged objects and also maintain performance?

We need to follow the below steps:-?

  • Implement IDisposable interface and implement the dispose function.
  • In Dispose function calls the “GC.SuppressFinalize” method.
  • At the client side ensure that the “Dispose” function is called when the object is no more required.

Below goes the code, this is also called as “Finalize and Dispose pattern”. This ensures that your objects are created in Generation 0 rather than Generation 1. “GC.SuppressFinalize” tells the garbage collector to not worry about destructor and destroy the objects in the first call itself.

No alt text provided for this image

Q18. Can we force garbage collector to run?

“System.GC.Collect ()” forces garbage collector to run. This is not a recommended practice but can be used if situations arise.

Q19. What is the difference between finalize and dispose?

  • Finalize is a destructor and dispose is a function which is implemented via ‘Idisposable’ interface.
  • Finalize is nondeterministic, since it’s called by garbage collector. Dispose is a function and needs to be called by the client for clean up. In other finalize is automatically called by garbage collector while dispose needs to be called forcefully.

Note: -As a good practice Finalize and dispose is used collectively because of double garbage collector loop. You can talk about this small note after you talk about the above 2 differences.?

Q20. What is CTS?

In .NET there are lots of languages like C#, VB.NET, VF.NET etc. There can be situations when we want code in one language to be called in other language. In order to ensure smooth communication between these languages the most important thing is that they should have a common type system. CTS (Common types system) ensures that data types defined in two different languages get compiled to a common data type.

So “Integer” data type in VB6 and “int” data type in C++ will be converted to System.int32, which is data type of CTS.

Note: If you know COM programming, you would know how difficult it is to interface VB6 application with VC++ application. As datatype of both languages did not have a common ground where they can come and interface, by having CTS interfacing is smooth.

Q21. What is a CLS (Common Language Specification)?

CLS is a subset of CTS. CLS is a specification or set of rules or guidelines. When any programming language adheres to these set of rules it can be consumed by any .NET language.

For instance one of the rule which makes your application CLS non-compliant is when you declare your methods members with same name and with only case differences in C#. You can try this create a simple class in C# with same name with only case differences and try to consume the same in VB.NET ,it will not work.

Q22. What is an Assembly?

Assembly is unit of deployment like EXE or a DLL.

Q23. What are the different types of Assembly?

There are two types of assembly Private and Public assembly. A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory beneath. A shared assembly is stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime.

Shared assemblies are needed when we want the same assembly to be shared by various applications in the same computer.

Q24. What is Namespace?

Namespace does two basic functionalities:-

  • It logically groups classes, for instance System.Web.UI namespace logically groups UI related features like textboxes, list control etc.
  • In Object Oriented world, many times it is possible that programmers will use the same class name. Qualifying NameSpace with class names avoids this collision.

Q25. What is Difference between NameSpace and Assembly?

Following are the differences between namespace and assembly:

  • Assembly is physical grouping of logical units, Namespace, logically groups classes.
  • Namespace can span multiple assemblies while assembly is a physical unit like EXE , DLL etc.

Q26. What is ILDASM?

ILDASM is a simple tool which helps you to view IL code of a DLL or EXE. In order to view IL code using ILDASM , go to visual studio command prompt and run “ILDASM.EXE”. Once ILDASM is running you view the IL code.

Q27. What is Manifest?

Assembly metadata is stored in Manifest. Manifest contains metadata which describes the following things :-

  • Version of assembly.
  • Security identity.
  • Scope of the assembly.
  • Resolve references to resources and classes.

The assembly manifest is stored in the DLL itself.

Q28. Where is the version information stored of an assembly?

Version information is stored in assembly inside the manifest.

Q29. Is versioning applicable to private assemblies?

Yes, versioning is applicable to private assemblies also.

Q30. What is the use of strong names?

Note:- This question can also be asked in two different ways , what are weak references and strong reference or how do you strong name a .NET assembly.

When we talk about .NET application it has two parts one is the class library or the DLL and the other the consumer like windows UI etc using this DLL.

If the consumer identifies the DLL library by namespace and class names it’s called as weak reference. It’s very much possible in deployment environment someone can delete the original class library and fake a similar class library with the same class name and namespace name. Strong name is a unique name which is produced by the combination of version number, culture information, public key and digital signature. No one can fake this identity or generate the same name.

So your consumer or UI will refer the class library with strong names rather than class and namespace names. In order to create strong name, right click on the class library, click on properties, click on signing tab and click on the new menu to generate strong names as shown in the below figure.

No alt text provided for this image

Watch more IT interview questions & answers video on QuestPond YouTube Channel .

Happy Learning & Happy Job Hunting.....!!!!

Full Interview series available here https://www.questpond.com/interview-questions---answers-tutorial/cid63 Happy Learning and Job Hunting from Questpond.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了