Monday 30 January 2017

C# basic interview questions Part-2

1. Ques: What is an interface in c#  and what are the advantages of interface ?
Answer:
An interface  is a type  which contains only the signatures of methods, properties, events or indexers.it has no implementation.The class contract with Interface has resposible for Implementation of the signatures of methods, properties.
  • An Interface is a reference type.
  • It does not Contains any data members.
  • No access modifier apply in interface.
  • By Default interface members is public. 
  • It Supports multiple interfaces.
Advantages of  an interface
  • Only Multiple interface supported in C#.but Multiple inheritance not supported.
  • For creating loosely coupled module.
  • Allow different objects to interact easily.
  • Extensibility.
  • Interface help for Dependency injection and inversion of control

2. Ques: What is Garbage Collection ?
Answer:
  • Garbage Collector is part of CLR in .Net Framework which is responsible to Automatic Memory Management.
  • It is collect all unused memory area and give to application to realeae the memory.
  • Garbage Collector use a system.gc.collect() method for release the memory.
  • Garbage Collector dived the heap memory into three Generation Generation 0, Generation  1, Generation 2.
  • It check the object which is short live or long live and put that object into Generation 0,1,2 respectvley.
  • garbage collector does not clean unmanaged objects it clean only managed objects.
Generation 0 (Zero): short-lived objects store in this Generation, e.g., Temporary objects. GC initiates garbage collection process frequently in this generation.
Generation 1 (One): This generation is the buffer between short-lived and long-lived objects.
Generation 2 (Two): This generation holds long-lived objects like a static and global variable.

Garbage collection happend when any one of the following conditions is true:
The system has low physical memory.
The GC.Collect method is called.


3. Ques: What is CLR ?
Answer:
Common Language Runtime (CLR) is the heart of the dot Net framework. it provides run time environment to run all the dot Net Programs. it execute MSIL (Microsoft Intermediate Language) code to native code. The code which runs under the CLR is called as Managed Code.

CLR  provides a common set of services in dot Net framework
Memory management (using Garbage Collector): it relesed unuesed obect from heap memory.
Exception handling and errors: It is allows create code and handle the exceptions.
Security managment : CLR provide Code Access Security (CAS) which restrict to run unauthorized code.
Thread management: Allows to run multiple threads of execution.
JIT compiler: JIT is part of CLR which convert MSIL code into native code.
Type loading:  Finds and loads assemblies and types.
Type safety:  It Ensures the references match compatible types.
Code Manager:  It manages the code at run time.
Class Loader: Used to load all classes at run time.


4. Ques: What is the CLS (Common Language Specification)?
Answer:
Common Language Specification and it is a subset of CTS. It defines a set of rules and restrictions that every language must follow which runs under .NET framework. CLS enables cross-language integration. CLS represents the guidelines to the compiler of a language, which targets the .NET Framework.


5. Ques: What is the CTS ?
Answer:  
Common Type System (CTS) defines a set of types that can use different dot Net languages, and after compilation of different  dot Net languages it treated as single type. It enables cross-language integration, type safety, and high-performance code execution. It provides an object-oriented model for implementation of many programming languages.


6. Ques: What is difference between dispose and finalize method in c# ?
Answer:  
Dispose
it is used to free unmanaged resources like files, database connections, unmanaged memory etc. at any time.
it is called by User Code. So we can say that it called Explicitly.
Dispose() method belongs to IDisposable interface.
Performance of application not affected with Dispose method.

finalize 
it is used to free unmanaged resources like files, database connections, unmanaged memory etc etc. before object is destroyed.
it is called by Garbage Collector. so we can say that it called Internally.
finalize() method belongs to object class.
Performance of application is affected with finalize method.


7. Ques: What is GAC ? What are the steps to create an assembly and add it to the GAC ?
Answer:
The global assembly cache (GAC) is a Directory that stores assemblies that is shared by several applications on the computer. You should share assemblies by installing them into the global assembly cache.

Steps for installing
  • Create a strong name using sn.exe tool eg: sn -k mykey.snk
  • in AssemblyInfo.cs, add the strong name eg: [assembly: AssemblyKeyFile("mykey.snk")]
  • recompile project, and then install it to GAC in two ways :
  • drag & drop it to assembly folder (C:\WINDOWS\assembly OR C:\WINNT\assembly) (shfusion.dll tool)
  •  gacutil -i abc.dll

8. Ques: What is a strong name?
Answer:
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.


9. Ques: Is it possible to force garbage collector to run?
Answer:
Yes, we can force garbage collector to run using System.GC.Collect().
It can be used to avoid calling any of the collect methods and allow the garbage collector to run independently.
It is better at determining the best time to perform a collection.

No comments:

Post a Comment

Thank you for comment