Monday, 18 April 2011
Friday, 4 March 2011
C# ineterview Questions
What is Private Constructors ?
A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:
Note : if you do not use an access modifier with the constructor it will still be private by default.
However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.
What is Static Class ?
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
E.g: Math Class
As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
Features of Static class :
What is Static Constructor ?
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
E.g:
Properties :
static constructors have the following properties:
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
What are functional and non-functional requirements?
Multiply two integers without using multiplication, division and bit-wise operators, and no loops
A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class. For example:
class NLog { // Private Constructor: private NLog() { } public static double e = Math.E; //2.71828... }The declaration of the empty constructor prevents the automatic generation of a default constructor.
Note : if you do not use an access modifier with the constructor it will still be private by default.
However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.
What is Static Class ?
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
E.g: Math Class
As is the case with all class types, the type information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.
Features of Static class :
- Contains only static members.
- Cannot be instantiated.
- Is sealed.
- Cannot contain Instance Constructors.
What is Static Constructor ?
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
E.g:
class SimpleClass { // Static variable that must be initialized at run time. static readonly long baseline; // Static constructor is called at most one time, before any // instance constructor is invoked or member is accessed. static SimpleClass() { baseline = DateTime.Now.Ticks; } }
Properties :
static constructors have the following properties:
- A static constructor does not take access modifiers or have parameters.
- A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
- A static constructor cannot be called directly.
- The user has no control on when the static constructor is executed in the program.
- A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
- Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.
- If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
- If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
- If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
- If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
- If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
What are events and delegates?
An event is a message sent by a control to notify the occurrence of an action. However it is not known which object receives the event. For this reason, .NET provides a special type called Delegate which acts as an intermediary between the sender object and receiver object.
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 the global assembly cache (GAC)?
GAC is a machine-wide cache of assemblies that allows .NET applications to share libraries. GAC solves some of the problems associated with dll’s (DLL Hell).
What is BLOB ?
A BLOB (binary large object) is a large item such as an image or an exe represented in binary form.
class Program
{
private static int Multiply(int x,int y)
{
int z = 0;
if(y==0 || x==0 )
{
z = 0;
}
if (y > 0)
{
z = x + Multiply(x, y - 1);
}
if (y < 0)
{
z = -Multiply(x, -y);
}
return z;
}
public static void Main(string[] args)
{
int a= Multiply(-5, 10);
Console.WriteLine(string.Format("5 * 10 = {0}",a));
}
}
Refer http://geeksforgeeks.org
Find the maximum of three numbers using ternary operator
private static int Max(int a,int b,int c)
{
return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c));
}
refer http://www.cracktheinterview.org
Find the maximum of four numbers using ternary operator
{
private static int Multiply(int x,int y)
{
int z = 0;
if(y==0 || x==0 )
{
z = 0;
}
if (y > 0)
{
z = x + Multiply(x, y - 1);
}
if (y < 0)
{
z = -Multiply(x, -y);
}
return z;
}
public static void Main(string[] args)
{
int a= Multiply(-5, 10);
Console.WriteLine(string.Format("5 * 10 = {0}",a));
}
}
Refer http://geeksforgeeks.org
Find the maximum of three numbers using ternary operator
private static int Max(int a,int b,int c)
{
return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c));
}
refer http://www.cracktheinterview.org
Find the maximum of four numbers using ternary operator
Subscribe to:
Posts (Atom)