Saturday, November 14, 2015

HCL interview questions and answers for experience.



1. Differences between VS 2005, 2008 and 2010?

VS 2005 support the then new .net framework 2.0 visual studio 2008 came out to support 3.0 and 3.5 while visual studio 2010 supports the upcoming .net framework 4.0.

Also since the release of visual studio 2008 all newer versions are now backwards compatible to older frameworks. Meaning in visual studio 2008 you are able to work with .net framework 2.0 applications as well as the at the time new 3.0 and 3.5 frameworks. This means the new visual studio 2010 will support development of framework versions 2.0, 3.0, 3.5 and 4.0. Visual studio 2008 however will only support 2.0, 3.0 and 3.5.

2. Differences between static and instance constructor?

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.
class MyClass
{
    // Static variable that must be initialized at run time.
    static readonly long ticks;

    // Static constructor is called at most one time, before any
    // instance constructor is invoked or member is accessed.
    static MyClass()
    {
        ticks= DateTime.Now.Ticks;
    }
}
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.
In this example, class Bus has a static constructor. When the first instance of Bus is created (bus1), the static constructor is invoked to initialize the class. The sample output verifies that the static constructor runs only one time, even though two instances of Bus are created, and that it runs before the instance constructor runs.
Instance Constructor
Instance constructor is used to initialize instance data. Instance constructor is called every time when object of class is created. It is called explicitly. Instance constructor takes parameters. It has access specifiers.

3. Difference between Abstract class and interface?


InterfaceAbstract class
Interface support multiple inheritanceAbstract class does not support multiple inheritance
Interface doesn't Contains Data MemberAbstract class contains Data Member
Interface doesn't contains ConstructorsAbstract class contains Constructors
An interface Contains only incomplete member (signature of member)An abstract class Contains both incomplete (abstract) and complete member
An interface cannot have access modifiers by default everything is assumed as publicAn abstract class can contain access modifiers for the subs, functions, properties
Member of interface can not be StaticOnly Complete Member of abstract class can be Static


When to use abstract class or Interface?
  • An interface is like a contract. It says that a class which implements the interface agrees to implement all of the functions declared (as signatures only; no function definition) by that interface. The class may do so in any way it chooses, and provide any other functionality, as long as it implements each one of the declared functions. An interface is useful when you want to be able to use some common functionality of otherwise unrelated classes- they share no implementation details, only the function signatures. In C#, function declarations within an interface are implicitly pure virtual.
    An abstract class is a partially defined class that cannot be instantiated. It (usually) includes some implementation, but leaves some functions as pure virtual- declared only by their signature. Pure virtual functions are not defined in the class that declares them, so they must be implemented by a subclass (unless it too is an abstract class). Only a subclass which defines all of the pure virtual functions can be instantiated. The purpose of an abstract class is to define some common behavior that can be inherited by multiple sub-classes, without implementing the entire class. In C#, the abstract keyword designates both an abstract class and a pure virtual method.
    In practical terms, the difference between the two is that an interface defines only pure virtual functions, while an abstract class may also include concrete functions, members, or any other aspect of a class.
Interfaces are like skeletons. If you want to build a human, you should use that skeleton.
Interfaces simply create a kind of structure for your classes to denote that your class should have a defined and agree-upon structure. For example, all animals pee, eat and make sounds. Thus you can have an interface, called IAnimal which only mentions that classes should PeeEat, and MakeSound.
Interfaces are just like simple checklists, which should either be implemented totally, or none. When you want to use IAnimal interface, it's just like your boss telling you "Hey, don't create a Cat class unless you write some methods for it to eat, pee, and make sound".
Abstract classes are like skeletons, but with some meat on them as well. It's just there to make your work easier.
You can consider an abstract class to be an interface, which already has some implementation.
That was the plain English answer. But after getting that, please study more, to get the real concept.

An abstract class is a class. It is special in that you cannot create object of abstract class. A non-abstract class in called concrete class.
An abstract class, being a class, can have usual elements in it like fields, properties, methods events, indexers etc. Note that, as it is guaranteed that abstract class will not be instantiated to create object, some of those elements cab be without implementations (e.g. methods without body).
An interface is description of members that some other class will have for sure. C# interface can describe methods, properties, indexers and events only.
If a class implements interface, then it must implement all members described by interface.
This means if a class, implementing an interface, does not implement all members described by that interface then such class must be abstract class!
An abstract class can be 'half-cooked' class meaning it has provided few methods with body but few methods are without body etc. An interface can never implement any member it describes.
Abstract class takes part in hierarchy of classes based on inheritance. Note that in C#, a class can have at most one parent class which may or may not be abstract.
A class can implement many interfaces at the same time.
There many use cases for both of them.
Following statements should give you more clarity:
  • Class must be either abstract or concrete.
  • Interface, by definition, is abstract.
  • Neither interface nor abstract class can be instantiated.
  • Class may inherit from zero or one (abstract or concrete) class.
  • Class may implement zero or more interfaces.
  • Interface may inherit from zero or more interfaces.
  • Interface cannot inherit a class.
  • Abstract class may have members with implementation.
  • Interface cannot have any member with implementation.
  • To be concrete, class must implement all members in interface.
Use Abstract class when there is a 'IS-A' relationship between classes. For example, Lion is a Animal, Cat is an Animal. So, Animal can be an abstract class with common implementation like no. of legs, tail etc.


Abstract classes allow you to provide default functionality for the subclasses. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Why? Because you can make a change to it and all of the inheriting classes will now have this new functionality. If the base class will be changing often and an interface was used instead of an abstract class, we are going to run into problems. Once an interface is changed, any class that implements that will be broken. Now if its just you working on the project, that’s no big deal. However, once your interface is published to the client, that interface needs to be locked down. At that point, you will be breaking the clients code.


Speaking from personal experiences, frameworks is a good place to show when and where to use both an abstract class and an interface. Another general rule is if you are creating something that provides common functionality to unrelated classes, use an interface. If you are creating something for objects that are closely related in a hierarchy, use an abstract class. An example of this would be something like a business rules engine. This engine would take in multiple BusinessRules as classes perhaps? Each one of these classes will have an analyze function on it.
Basically Abstract class should be used for 'is-a' relationship and Interface should be used for 'has-a' relationship.
If we want to retrict all the subclasses must have certain functionality, then Abstract class with implementation is recomended. So, overriding already implemented member in the subclasses is not a good practice here.

In a particular class, where there are more behaviours that will get change during runtime, then implementing from multiple interfaces are recommended. 
Use an abstract class
When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)
Use an abstract class to define a common base class for a family of types.
Use an abstract class to provide default behavior.
Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
Use an interface to design a polymorphic hierarchy for value types.
Use an interface when an immutable contract is really intended.
A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.


Important rules applied to abstract classes

An abstract class cannot be a sealed class. I.e. the following declaration is incorrect.
//Incorrect
abstract sealed class absClass
{
}
Declaration of abstract methods are only allowed in abstract classes.
An abstract method cannot be private.
//Incorrect
private abstract int MultiplyTwoNumbers();
The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
An abstract method cannot have the modifier virtual. Because an abstract method is implicitly virtual.
//Incorrect
public abstract virtual int MultiplyTwoNumbers();
An abstract member cannot be static.
public abstract static int MultiplyTwoNumbers();
4. What is collection?
Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces.


Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.
Ex: ArrayList, Stack Queue etc.

5. Diff between ArrayList and List?

 1. ArrayList is collection class. Stored data in form of object.
When you stored data or retrieve data boxing and unboxing takes place.
It is costly operation and it hinders performance.

  ArrayList arrayList = new ArrayList();
            arrayList.Add("1");
            arrayList.Add("One");

            Console.WriteLine(arrayList[0].ToString());//Output 1

2. List is generic collection and it stores generic data so no boxing and unboxing involves.
  List list = new List();
            list.Add(1);
            list.Add("1");//error
            list.Add(2);


            Console.WriteLine(list[0]);//1

6. Difference between overriding and hidding?


What are the differences between method hiding and overriding in C#?
  1. For hiding the base class method from derived class simply declare the derived class method with the new keyword.
    Whereas in C#, for overriding the base class method in a derived class, you need to declare the base class method as virtual and the derived class method as overriden.
  2. If a method is simply hidden then the implementation to call is based on the compile-time type of the argument "this".
    Whereas if a method is overridden then the implementation to be called is based on the run-time type of the argument "this".
  3. New is reference-type specific, overriding is object-type specific.
What are the differences between method hiding and method shadowing?
  1. Shadowing is a VB concept. In C#, this concept is called hiding.
  2. The two terms mean the same in C#.
    Method hiding == shadowing
  3. In short, name "hiding" in C# (new modifier) is called shadowing in VB.NET (keyword Shadows).
  4. In C# parlance, when you say "hiding" you're usually talking about inheritance, where a more derived method "hides" a base-class method from the normal inherited method call chain.
  5. When you say "shadow" you're usually talking about scope; an identifier in an inner scope is "shadowing" an identifier at a higher scope.
  6. In other languages, what is called "hiding" in C# is sometimes called "shadowing" as well.

7. These static constructors are correct?

class A
statc intA()
{
}
static A(int x,int y)
{
}
static A(int x)
}

Ans: A static constructor does not take access modifiers or have parameters.


8. This abstract class is correct?
abstract class A
{

public abstract void Disp();

public abstract void B()
{

}

public absract virtual C()
{

}



}

Ans:  Method without implementation is called abstract method. It is declared with abstract key word. Please see inline comment.



abstract class A
{

public abstract void Disp();//correct

public abstract void B() //wrong 
{

}

public absract virtual C() //wrong
{

}



}

No comments:

Post a Comment

Write a program to reverse a string? using System; namespace ConsoleApp1 {     class Program     {         static void Main(string[] args)  ...