c# interview examination questions

 

What is the difference between Finalize() and Dispose()?

 

Dispose() is called by as an indication for an object to release any unmanaged resources it has held. 
Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object. 

Dispose() operates determinalistically due to which it is generally preferred. 

 

How does the XmlSerializer work? What ACL permissions does a process using it require?

The XmlSerializer constructor generates a pair of classes derived from XmlSerializationReader and XmlSerializationWriter by analysis of the classes using reflection.

Temporary C# files are created and compiled into a temporary assembly and then loaded into a process.

The XmlSerializer caches the temporary assemblies on a per-type basis as the code generated like this is expensive. This cached assembly is used after a class is created

Therefore the XmlSerialize requires full permissions on the temporary directory which is a user profile temp directory for windows applications.


What are circular references? Explain how garbage collection deals with circular references.

A circular reference is a run-around wherein the 2 or more resources are interdependent on each other rendering the entire chain of references to be unusable.

There are quite a few ways of handling the problem of detecting and collecting cyclic references.

1. A system may explicitly forbid reference cycles.
2. Systems at times ignore cycles when they have short lives and a small amount of cyclic garbage. In this case a methodology of avoiding cyclic data structures is applied at the expense of efficiency.
3. Another solution is to periodically use a tracing garbage collector cycles.
Other types of methods to deal with cyclic references are:
•Weighted reference counting

•Indirect reference counting 

Explain how to add controls dynamically to the form using C#.NET.

The following code can be called on some event like page load or onload of some image or even a user action like onclick

protected void add_button(Button button)
{
   try
   {
        panel1.Controls.Add(button); // Add the control to the container on a page
   }
   catch (Exception ex)
   {
         label1.Text += ex.Message.ToString();
   }

} 

 

 

Describe the configuration files in .Net. What are different types of configuration files in .Net framework?

The Machine.Config file, which specifies the settings that are global to a particular machine.

This file is located at the following path:
\WINNT\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config

The simplest way to add application-specific settings into an application configuration file is to use an application configuration file. 
The file is an XML file and contains add elements with key and value attributes.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
      <add key="ApplicationTitle" value="Sample Console Application" />
     <add key="ConnectionString"value="Server=localhost;Database=Northwind;Integrated Security= false;UserId=sa;Password=;" />
</appSettings>
</configuration>

<authentication> The authentication section controls the type of authentication used within your Web application Windows, Forms or Passport type of authentication can be defined.

Eg: <authentication mode="Windows" />
<allow> or <deny> tags can be used with authorization to allow or deny access to your web application to certain users or roles,
<authorization> 
      <allow roles="Administrators,Users" /> 
      <deny users="*" /> 
</authorization>

 

Explain the use of virtual, sealed, override, and abstract.

The virtual keyword enables a class to be overridden. If it has to be prevented from being overridden, then the sealed keyword needs to be used. If the keyword virtual is not used, members of the class can even then be overridden. However, its usage is advised for making the code meaningful.

The override keyword is used to override the virtual method in the base class. Abstract keyword is used to modify a class, method or property declaration. You cannot instantiate an abstract class or make calls to an abstract method directly.

An abstract virtual method means that the definition of the method needs to be given in the derived class.


 

What is the difference between const and readonly in C#.NET?

The read only can be modified by the class it is contained in. However, the const cannot be modified. It needs to be instantiated only at the compile time.

 

What are Custom Exceptions?

Custom Exceptions are user defined exceptions.

There are exceptions other than the predefined ones which need to be taken care of.

For example: The rules for the minimum balance in a Salary A/C would be different from that in a Savings A/C due to which these things need to be taken care of during the implementation.

C#.Net - What are Custom Exceptions? - June 04, 2009 at 15:00 PM by Shuchi Gauri

Custom exception needs to derive from the System.Exception class. You can either derive directly from it or use an intermediate exception like SystemException or ApplicationException as base class. Custom exeptions are created to handle very specific exceptions and to provide more details about it.

We need custom exceptions to have better predictability of our application. By using custom exceptions we can throw and handle our own exceptions, providing a much more predictable and stable application.

public class MyCustomException : Exception
{
        public MyCustomException()
        : base()
        {
        }

        public MyCustomException(string Message)
        : base(Message)
        {
        }

        public MyCustomException(string Message, Exception InnerException)
        : base(Message, InnerException)
        {
        }

        protected MyCustomException(SerializationInfo Info, StreamingContext Context)
        : base(Info, Context)        
        {
        }

} 

 

What are delegates and why are they required?

The delegates in .NET are like the pointers to the functions in C/C++. The difference is that these are type safe unlike the once in C/C++.

There are situations where in a programmer or an application needs to perform an action on a particular event. Eg: Some user action like click, text change, etc. So when these actions are performed by the user, the delegates invoke the respective functions.

C#.Net - What are delegates and why are they required? 

Delegates are like type safe function pointers. We need delegates as they can be used to write much more generic functions which are type safe also.

It encapsulates the memory address of a function in the code. Events are created using delegates in .Net. When an event is published/thrown, the framework examines the delegate behind the event and then calls the function that is referred to by the delegate. 

Can private virtual methods be overridden in C#.NET

No, moreover, you cannot access private methods in inherited classes,
They have to be protected in the base class to allow any sort of access.
 

Can you override private virtual methods?

First of all private methods in the inherited class can't be accessed, so there is no question of overriding of private virtual methods. 

 

Is is possible to force garbage collector to run? 

Yes, we can force garbage collector to run using System.GC.Collect(). 

 

What is an Event? 

When an action is performed, this action is noticed by the computer application based on which the output is displayed. These actions are called events. Examples of events are pressing of the keys on the keyboard, clicking of the mouse. Likewise, there are a number of events which capture your actions.

Define Delegate.

Delegates are kind of similar to the function pointers. But they are secure and type-safe.
A delegate instance encapsulates a static or an instance method. 

Declaring a delegate defines a reference type which can be used to encapsulate a method having a specific signature. 

 

What is Assembly manifest?

The manifest of an assembly contains assembly's data like version, scope, security information (strong name),etc. 
It also contains a reference to the resource and classes. 

It is stored in either an .exe or a .dll with Microsoft intermediate language (MSIL) code.  

 

 

What is GAC (global assembly cache)? 

GAC, global assembly cache is an area of memory reserved to store the assemblies of all .NET applications that are running on a certain machine.

It shares assemblies among multiple .NET applications. The assemblies must have a strong name and must be publicly shared to be installed in the GAC. 

 

 

System.String vs. System.StringBuilder classes 

System.String is immutable.

System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.  

 

 

 

Method overriding vs. overloading 

Overloading means having a method with the same name within the class
Through overriding, you change the method behavior for a derived class.
 

List down difference between overriding and overloading.

When overriding, you change the method behavior for a derived class.

Overloading simply involves having a method with the same name within the class. 

 

 

 

What are design patterns? Define basic classification of patterns - Creational, Structural and Behavioral patterns.

A design pattern in Software is used to solve similar problems that occur in different scenarios. A design pattern is not a design that can be converted to a chunk of code. It can be considered as a solution that can be reused. A relation between classes and object is a design pattern in OOPL.

Creational design pattern: Helps in creation of objects and controlling any design issues. Abstract factory, factory, builder pattern are the examples.

Structural design pattern: - Helps in defining relationships between entities. Adapter pattern, aggregate, Proxy pattern are the examples.

Behavioral design pattern: - helps in realizing communication patterns between objects. Iterator pattern, strategy pattern, and state pattern are the examples.

 

How is .NET able to support multiple languages? - a language should comply with the Common Language Runtime standard to become a .NET language. In .NET, code is compiled to Microsoft Intermediate Language (MSIL for short). This is called as Managed Code. This Managed code is run in .NET environment. So after compilation to this IL the language is not a barrier. A code can call or use a function written in another language. 

 

What is the difference between constants and read-only variables that are used in programs?

Constants perform the same tasks as read-only variables with some differences. The differences between constants and read-only are

Constants:

  1. Constants are dealt with at compile-time.
  2. Constants supports value-type variables.
  3. Constants should be used when it is very unlikely that the value will ever change.

Read-only:
  1. Read-only variables are evaluated at runtime.
  2. Read-only variables can hold reference type variables.
  3. Read-only variables should be used when run-time calculation is required.

 

 

posted @ 2012-04-25 21:25  umlchina  阅读(316)  评论(0编辑  收藏  举报