Wednesday, November 14, 2007

Dot Net Interview Questions - Part 5

  1. Can you explain what inheritance is and an example of when you might use it?
  2. How can you write a class to restrict that only one object of this class can be created (Singleton class)?
  3. What are the access-specifiers available in c#?
    Private, Protected, Public, Internal, Protected Internal.
  4. Explain about Protected and protected internal, “internal” access-specifier?
    protected - Access is limited to the containing class or types derived from the containing class.
    internal - Access is limited to the current assembly.
    protected internal - Access is limited to the current assembly or types derived from the containing class.
  5. Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?
    (Class constructor method is also known as type constructor or type initializer)
    Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. (It will get executed only 1st time, when we call any static methods/fields in the same class.) Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention.
    A static constructor is used to initialize a class. It is called automatically to initialize the class before the first instance is created or any static members are referenced.
  6. What is Private Constructor? and it’s use? Can you create instance of a class which has Private Constructor?
    A: When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
    Make a constructor private if:
    - You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class' Clone method.
    - You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory.
  7. I have 3 overloaded constructors in my class. In order to avoid making instance of the class do I need to make all constructors to private?(yes)
  8. Overloaded constructor will call default constructor internally?(no)
  9. What are virtual destructors?
  10. Destructor and finalize
    Generally in C++ the destructor is called when objects gets destroyed. And one can explicitly call the destructors in C++. And also the objects are destroyed in reverse order that they are created in. So in C++ you have control over the destructors.
    In C# you can never call them, the reason is one cannot destroy an object. So who has the control over the destructor (in C#)? it's the .Net frameworks Garbage Collector (GC). GC destroys the objects only when necessary. Some situations of necessity are memory is exhausted or user explicitly calls System.GC.Collect() method.
    Points to remember:
    1. Destructors are invoked automatically, and cannot be invoked explicitly.
    2. Destructors cannot be overloaded. Thus, a class can have, at most, one destructor.
    3. Destructors are not inherited. Thus, a class has no destructors other than the one, which may be declared in it.
    4. Destructors cannot be used with structs. They are only used with classes.
    5. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
    6. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
    7. When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.
  11. What is the difference between Finalize and Dispose (Garbage collection)
    Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object.
    In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object. Dispose can be called even if other references to the object are alive.
    Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from permanently leaking if the programmer fails to call Dispose.
  12. What is close method? How its different from Finalize & Dispose?
  13. What is boxing & unboxing?
  14. What is check/uncheck?
  15. What is the use of base keyword? Tell me a practical example for base keyword’s usage?
  16. What are the different .net tools which u used in projects?
  17. try
    {
    ...
    }
    catch
    {
    ...//exception occurred here. What'll happen?
    }
    finally
    {
    ..
    }

    Ans : It will throw exception.
  18. What will do to avoid prior case?
    try
   {
    try
    {
      ...
    }
    catch
    {
     ...
     //exception occurred here.
    }
    finally
    {
     ...
    }
   }
   catch
   {
     ...
   }
   finally{...}
19.       try
     {
       ...
     }
     catch
     {
       ...
     }
     finally
     {
       ...
     } 

Will it go to finally block if there is no exception happened?
Ans: Yes. The finally block is useful for cleaning up any resources allocated in the try block. Control is always passed to the finally block regardless of how the try block exits.

  1. Is goto statement supported in C#? How about Java?
    Gotos are supported in C#to the fullest. In Java goto is a reserved keyword that provides absolutely no functionality.

No comments: