Dotnet Interview Notes Part II

Dotnet Interview Notes Part II

Garbage Collection in C# | .NET Framework

Automatic memory management is made possible by Garbage Collection in .NET Framework. When a class object is created at runtime, certain memory space is allocated to it in the heap memory. However, after all the actions related to the object are completed in the program, the memory space allocated to it is a waste as it cannot be used. In this case, garbage collection is very useful as it automatically releases the memory space after it is no longer required.

Garbage collection will always work on Managed Heap and internally it has an Engine which is known as the Optimization Engine.

Garbage Collection occurs if at least one of multiple conditions is satisfied. These conditions are given as follows:

· If the system has low physical memory, then garbage collection is necessary.

· If the memory allocated to various objects in the heap memory exceeds a pre-set threshold, then garbage collection occurs.

· If the GC.Collect method is called, then garbage collection occurs. However, this method is only called under unusual situations as normally garbage collector runs automatically.

Phases in Garbage Collection

There are mainly 3 phases in garbage collection. Details about these are given as follows:

  1. Marking Phase: A list of all the live objects is created during the marking phase. This is done by following the references from all the root objects. All of the objects that are not on the list of live objects are potentially deleted from the heap memory.

  2. Relocating Phase: The references of all the objects that were on the list of all the live objects are updated in the relocating phase so that they point to the new location where the objects will be relocated to in the compacting phase.

  3. Compacting Phase: The heap gets compacted in the compacting phase as the space occupied by the dead objects is released and the live objects remaining are moved. All the live objects that remain after the garbage collection are moved towards the older end of the heap memory in their original order.

Heap Generations in Garbage Collection

The heap memory is organized into 3 generations so that various objects with different lifetimes can be handled appropriately during garbage collection. The memory to each Generation will be given by the **Common Language Runtime(CLR)* depending on the project size. Internally, Optimization Engine will call the Collection Means Method* to select which objects will go into Gneration 1 or Generation 2.

· Generation 0 : All the short-lived objects such as temporary variables are contained in the generation 0 of the heap memory. All the newly allocated objects are also generation 0 objects implicitly unless they are large objects. In general, the frequency of garbage collection is the highest in generation 0.

· Generation 1 : If space occupied by some generation 0 objects that are not released in a garbage collection run, then these objects get moved to generation 1. The objects in this generation are a sort of buffer between the short-lived objects in generation 0 and the long-lived objects in generation 2.

· Generation 2 : If space occupied by some generation 1 objects that are not released in the next garbage collection run, then these objects get moved to generation 2. The objects in generation 2 are long lived such as static objects as they remain in the heap memory for the whole process duration.

Note: Garbage collection of a generation implies the garbage collection of all its younger generations. This means that all the objects in that particular generation and its younger generations are released. Because of this reason, the garbage collection of generation 2 is called a full garbage collection as all the objects in the heap memory are.released. Also, the memory allocated to the Generation 2 will be greater than Generation 1’s memory and similarly the memory of Generation 1 will be greater than Generation 0’s memory(Generation 2 > Generation 1 > Generation 0).

Q #1) What is .Net framework?

Ans: It is a platform for building various applications on windows. It has a list of inbuilt functionalities in the form of class, library, and APIs which are used to build, deploy and run web services and different applications. It supports different languages such as C#, VB .Net, Cobol, Perl, etc.

This framework supports object-oriented programming model.

Q #2) What are the important components of .Net?

Ans: The components of .Net are Common language run-time, .Net Class library, Application domain, Common Type System, .Net framework, Profiling, etc. However, the two important components are Class library and Common Language Runtime.

CLR provides building blocks for a wide variety of applications. The class library consists of a set of classes that are used to access the common functionality. The functionality can be shared among different applications.

Q #3) What is CTS?

Ans: CTS stands for Common Type System. It has a set of rules which state how a data type should be declared, defined and used in the program. It describes the data types that are to be used in the application.

We can design our own classes and values by following the rules that are present in the CTS. The rules are made so that the data type declared using a programming language is callable by an application that is developed using a different language.

Q #4) What is CLR?

Ans: CLR stands for Common Language Runtime. It is one of the most important components of .Net framework. It provides building blocks for many applications.

An application built using C# gets compiled by its own compiler and is converted into an Intermediate language. This is then targeted to CLR. CLR does various operations like memory management, Security checks, assemblies to be loaded and thread management. It provides a secure execution environment for applications.

Q #5) What is CLS?

Ans: CLS stands for Common Language Specification. With the rules mentioned under CLS, the developers are made to use the components that are inter-language compatible. They are reusable across all the .Net Compliant languages.

Q #6) What is JIT?

Ans: JIT stands for Just In Time. JIT is a compiler that converts Intermediate Language to a Native code.

The code is converted into Native language during execution. Native code is nothing but hardware specifications that can be read by the CPU. The native code can be stored so that it is accessible for subsequent calls.

Q #7) What is MSIL?

Ans: MSIL stands for Microsoft Intermediate Language.

MSIL provides instructions for calling methods, initializing and storing values, operations such as memory handling, exception handling and so on. All .Net codes are first compiled to IL.

Q #8) What is meant by Managed and Unmanaged code?

Ans: The code that is managed by the CLR is called Managed code. This code runs inside the CLR. Hence, it is necessary to install the .Net framework in order to execute the managed code. CLR manages the memory through garbage collection and also uses the other features like CAS and CTS for efficient management of the code.

Unmanaged code is any code that does not depend on CLR for execution. It means it is developed by any other language independent of .Net framework. It uses its own runtime environment for compiling and execution.

Though it is not running inside the CLR, the unmanaged code will work properly if all the other parameters are correctly followed.

Q #9) How is a Managed code executed?

Ans: Following steps are followed while executing a Managed code:

  • Choosing a language compiler depending on the language in which the code is written.

  • Converting the above code into Intermediate Language by its own compiler.

  • The IL is then targeted to CLR which converts the code into native code with the help of JIT.

  • Execution of Native code.

Q #10) What is ASP.Net?

Ans: ASP .Net is a part of .Net technology and it comprises of CLR too. It is an open source server-side technology that enables the programmers to build powerful web services, websites and web applications.

ASP stands for Active Server Pages.

Q #11) Explain State management in ASP .Net.

Ans: State Management means maintaining the state of the object. The object here refers to a web page/control.

There are two types of State management, Client Side, and Server side.

Client Side — Storing the information in the Page or Client’s System. They are reusable, simple objects.

Server Side — Storing the information on the Server. It is easier to maintain the information on the Server rather than depending on the client for preserving the state.

Q #12) What is an Assembly? What are the different types of Assemblies?

Ans: An Assembly is a collection of logical units. Logical units refer to the types and resources which are required to build an application and deploy them using the .Net framework. The CLR uses this information for type implementations. Basically, Assembly is a collection of Exe and Dlls. It is portable and executable.

There are two types of Assemblies, Private and Shared.

Private Assembly, as the name itself suggests, it is accessible only to the application. It is installed in the installation directory of the Application.

A Shared assembly can be shared by multiple applications. It is installed in the GAC.

Q #13) Explain the different parts of an Assembly.

Ans: The different parts of an Assembly are:

  • Manifest — It contains the information about the version of an assembly. It is also called as assembly metadata.

  • Type Metadata — Binary information of the program.

  • MSIL — Microsoft Intermediate Language code.

  • Resources — List of related files.

Q #14) What is an EXE and a DLL?

Ans: Exe and DLLs are Assembly executable modules.

Exe is an executable file. This runs the application for which it is designed. An Exe is generated when we build an application. Hence the assemblies are loaded directly when we run an Exe. However, an Exe cannot be shared with the other applications.

DLL stands for Dynamic Link Library. It is a library that consists of code which needs be hidden. The code is encapsulated inside this library. An Application can consist of many DLLs. These can be shared with the other applications as well.

Other applications which need to share this DLL need not worry about the code intricacies as long as it is able to call the function on this DLL.

Q #15) What is Caching?

Ans: Caching means storing data temporarily in the memory so that the application can access the data from the cache instead of looking for its original location. This increases the performance of the application and its speed. System.Runtime.Caching namespace is used for Caching information in .Net.

Given below are the 3 different types of Caching:

  • Page Caching

  • Data Caching

  • Fragment Caching

Q #16) What is MVC?

Ans: MVC stands for Model View Controller. It is an architectural model for building the .Net applications.

Models — Model objects store and retrieve data from the database for an application. They are usually the logical parts of an application that is implemented by the application’s data domain.

View — These are the components that display the view of the application in the form of UI. The view gets the information from the model objects for their display. They have components like buttons, drop boxes, combo box, etc.

Controllers — They handle the User Interactions. They are responsible for responding to the user inputs, work with the model objects, and pick a view to be rendered to the user.

Q #17) What is the difference between Function and Stored procedure?

Ans:

Stored Procedure:

  • A Stored procedure is always used to perform a specific task.

  • It can return zero, one or more value.

  • It can have both Input and Output Parameters.

  • Exception handling can be done using a try-catch block.

  • A function can be called from a Procedure.

Functions:

  • Functions must return a single value.

  • It can only have the input parameter.

  • Exception handling cannot be done using a try-catch block.

  • A Stored procedure cannot be called from a function.

Q #18) Explain CAS (Code Access Security).

Ans: .Net provides a security model that prevents unauthorized access to resources. CAS is a part of that security model. CAS is present in the CLR. It enables the users to set permissions at a granular level for the code.

CLR then executes the code depending on the available permissions. CAS can be applied only to the managed code. Unmanaged code runs without CAS. If CAS is used on assemblies, then the assembly is treated as partially trusted. Such assemblies must undergo checks every time when it tries to access a resource.

The different components of CAS are Code group, Permissions, and Evidence.

Evidence– To decide what permissions to give, the CAS and CLR depend on the specified evidence by the assembly. The examination of the assembly provides details about the different pieces of evidence. Some common evidence include Zone, URL, Site, Hash Value, Publisher and Application directory.

Code Group — Depending on the evidence, codes are put into different groups. Each group has specific conditions attached to it. Any assembly that matches those condition is put into that group.

Permissions — Each code group can perform only specific actions. They are called Permissions. When CLR loads an assembly, it matches them to one of the code groups and identifies what actions those assemblies can do. Some of the Permissions include Full Trust, Everything, Nothing, Execution, Skip Verification, and the Internet.

Q #19) What is GAC?

Ans: GAC stands for Global Assembly Cache. Whenever CLR gets installed on the machine, GAC comes as a part of it. GAC specifically stores those assemblies which will be shared by many applications. A Developer tool called Gacutil.exe is used to add any file to GAC.

Q #20) What is meant by Globalization and Localization?

Ans: Internationalization is the process of designing applications that support multiple languages. This is divided into Localization and Globalization.

Globalization is nothing but developing applications to support different languages. Existing applications can also be converted to support multiple cultures.

Whereas Localization means changing the already globalized app to cater to a specific culture or language Microsoft.Extensions.Localization is used for localizing the app content. Some of the other keywords that are used for Localization are IHtmlLocalizer, IStringLocalizer, IViewLocalizer and so on

Q #21) What is a Garbage Collector?

Ans: Garbage collection is a feature of .Net to free the unused code objects in the memory.

The memory heap is divided into three generations. Generation 0, Generation 1 and Generation 2.

Generation 0 — This is used to store short-lived objects. Garbage Collection happens frequently in this Generation.

Generation 1 — This is for medium-lived objects. Usually, the objects that get moved from generation 0 are stored in this.

Generation 2 — This is for long-lived objects.

Collecting a Generation refers to collecting the objects in that generation and all its younger generations. Garbage collection of Generation 2 means full garbage collection, it collects all the objects in Generation 2 as well as Generation 1 and Generation 0.

During the Garbage collection process, as the first phase, list of live objects are identified. In the second phase, references are updated for those objects which will be compacted. And in the last phase, the space occupied by dead objects are reclaimed. The remaining objects are moved to an older segment.

What is BCL?

The Base Class Library is a Common Language Infrastructure. BCL encapsulates a large number of common functionalities which are available to all the .NET Languages. BCL makes the developers life much simpler while implementing various functionalities like I/O operations, Data access operations, graphical user interfaces and interfaces to various hardware devices by encapsulating them into various namespaces and classes. It also encapsulates the services which are required by the latest real world applications. .NET Framework applications, components and the controls are built on BCL.

Explain Different Types of Constructors in C#?

There are four different types of constructors you can write in a class -

  1. Default Constructor

  2. Parameterized Constructor

  3. Copy Constructor

  4. Static Constructor

What are functional and non-functional requirements?

Functional requirements defines the behavior of a system whereas non-functional requirements specify how the system should behave; in other words they specify the quality requirements and judge the behavior of a system.

E.g.

Functional — Display a chart which shows the maximum number of products sold in a region.

Non-functional — The data presented in the chart must be updated every 5 minutes.

What is a stack? What is a heap? Give the differences between the two?

Stack is a place in the memory where value types are stored. Heap is a place in the memory where the reference types are stored.

What is instrumentation?

It is the ability to monitor an application so that information about the application’s progress, performance and status can be captured and reported.

What is object role modeling (ORM) ?

It is a logical model for designing and querying database models. There are various ORM tools in the market like CaseTalk, Microsoft Visio for Enterprise Architects, Infagon etc.

What is a COM Callable Wrapper (CCW)?

CCW is a wrapper created by the common language runtime(CLR) that enables COM components to access .NET objects.

What is a Runtime Callable Wrapper (RCW)?

RCW is a wrapper created by the common language runtime(CLR) to enable .NET components to call COM components.

What is a digital signature?

A digital signature is an electronic signature used to verify/gurantee the identity of the individual who is sending the message.

What is Application Domain and how does it work?

Windows Operating Systems load a set of resources like .EXE, DLLs and allocate the memory for those resources in an area called as Process. Windows OS creates a separate and isolated area for each running application. Making separate isolation area for each application, makes the process more secure and stable. In case, one process fails, it does not affect the other process.

.NET applications, however, are not hosted like traditional applications by Windows Operating System. Under .NET, .EXEs are hosted under a process by logical partitioning which is known as “Application Domain”. Now you can host multiple application domains under one single process.

What is MIME?

The definition of MIME or Multipurpose Internet Mail Extensions as stated in MSDN is “MIME is a standard that can be used to include content of various types in a single message. MIME extends the Simple Mail Transfer Protocol (SMTP) format of mail messages to include multiple content, both textual and non-textual. Parts of the message may be images, audio, or text in different character sets.

What are Cookies in ASP.NET?

Answer: Cookies are a State Management Technique that can store the values of control after a post-back. Cookies can store user-specific Information on the client’s machine like when the user last visited your site. Cookies are also known by many names, such as HTTP Cookies, Browser Cookies, Web Cookies, Session Cookies and so on. Basically cookies are a small text file sent by the web server and saved by the Web Browser on the client’s machine.

List of properties containing the HttpCookies Class:

  1. Domain: Using these properties we can set the domain of the cookie.

  2. Expires: This property sets the Expiration time of the cookies.

  3. HasKeys: If the cookies have a subkey then it returns True.

  4. Name: Contains the name of the Key.

  5. Path: Contains the Virtual Path to be submitted with the Cookies.

  6. Secured: If the cookies are to be passed in a secure connection then it only returns True.

  7. Value: Contains the value of the cookies.

Limitation of the Cookies

  1. The size of cookies is limited to 4096 bytes.

  2. A total of 20 cookies can be used in a single website.

What is Ajax in ASP.NET?

Answer. Ajax stands for Asynchronous JavaScript and XML; in other words Ajax is the combination of various technologies such as a JavaScript, CSS, XHTML, DOM, etc.

AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the entire page.

Web applications running within Microsoft’s Internet Information Services (IIS) utilize what is known as IIS worker processes. These worker processes run as w3wp.exe, and there can be multiple per computer. It is possible to run IIS on a Windows desktop or Windows server, although it is usually only seen on Microsoft Windows Servers configured as web servers.

What is w3wp.exe / IIS Worker Process?

Web applications on Windows Servers are configured via command line or Internet Information Systems (IIS) Manager. Within IIS you can set up websites and which application pools they are assigned. Multiple websites can be assigned to a single IIS application pool.

IIS application pools also provide a bunch of advanced settings.aspx). These impact the behavior of w3wp and your IIS worker process. Including things like what Windows user account it runs as, auto restarting of the process, auto shutdown, and more. It is also possible for one IIS application pool to create multiple IIS worker processes in what is called a web garden.

There is one key thing you need to know about IIS application pools that are a little confusing. Within the IIS management console, you can stop and start application pools. But, just because an IIS application pool is started, there may not be an IIS worker process (w3wp) running. IIS will not start the worker process until the first web request is received.