OOPS Interview Question With Answer--C#

enforce a call from an inherited constructor to an arbitrary base constructor?

Q) If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

What do you understand by the term "immutable"?

Immutable means you can't change the currrent data,but if you perform some operation on that data, a new copy is created. The operation doesn't change the data itself.

For example let's say you have a string object having "hello" value. Now if you say
temp = temp + "new value" new object is created, and values is saved in that. The temp object is immutable, can't be changed. An object qualifies as being called immutable if its value cannot be modified once it has been created.

For example, methods that appear to modify a String actually return a new String containing the modification. Developers are modifying strings all the time in their code. This may appear to the developer as mutable - but it is not.

What actually happens is your string variable/object has been changed to reference a new string value containing the results of your new string value. For this very reason .NET has the System.Text.StringBuilder class.

If you find it necessary to modify the actual contents of a string-like object heavily, such as in a for or foreach loop, use the System.Text.StringBuilder class.

What is the difference between abstract class and an interface?

An abstract class and Interface both have method only but not have body of method.The difference between Abstract class and An Interface is that if u call Abstract class then u have to call all method of that particular Abstract class but if u call an Interface then it is not necessary that u call all method of that particular interface.Method Over Loading:-Return type, Parameter type, parameter and body of method number may be different.Method Overriding:- Return type, Parameter type, Parameter Number all must be same . Only body of method can change.

Note : This is one of the most commonly asked question in Interviews

When should a struct be used instead of a class?

They should be used for types that contain just a few data members..

How do class property members work in C#?

A property may have a get and/or a set associated with it. Within a set accessor block, C# automatically provides a variable called 'value' which holds the new value to which the property can be set.

A property with a get and set accessor for the int variable age would be defined as follows:

To set the value of age in object instance p to the new value of 34, :

public int Age

{
get { return age; }
set { age = value; }
}

What is the difference between an event and a delegate?

An event is just a wrapper for a multicast delegate. Adding a public event to a class is almost the same as adding a public multicast delegate field. In both cases, subscriber objects can register for notifications, and in both cases the publisher object can send notifications to the subscribers.

However, a public multicast delegate has the undesirable property that external objects can invoke the delegate, something we'd normally want to restrict to the publisher. Hence events - an event adds public methods to the containing class to add and remove receivers, but does not make the invocation mechanism public.

How is method overriding different from method overloading?

When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same name within the class.

Can you prevent a class from overriding ?

Yes, by defining a class as “Sealed” in C# and “NotInheritable” in VB.Net we can prevent a class from overriding.

More discussions related to this at http://www.dotnetinterviews.co.nr

What’s the difference between const and readonly?

You can initialize readonly variables to some runtime values. Let’s say your program uses current date and time as one of the values that won’t change. This way you declarepublic readonly string

DateT = new DateTime().ToString().

What is the difference between the value-type variables and reference-type variables in terms of garbage collection?

The value-type variables are not garbage-collected, they just fall off the stack when they fall out of scope, the reference-type objects are picked up by GC when their references go null.

Does Main() always have to be public in C#?

No not necessarily

More discussions related to this at http://www.dotnetinterviews.co.nr/

What are valid signatures for the Main function in C#?

1) public static void Main()
2) public static int Main()
3) public static void Main( string[] args )
4) public static int Main(string[] args )

What’s the difference between struct and class in C#?

1 Structs cannot be inherited.
2 Structs are passed by value, not by reference.
3 Struct is stored on the stack, not the heap.

What’s a multicast delegate?

A multicast delegate is a delegate that has multiple handlers assigned to it. Each assigned handler (method) is called.

What are sealed classes

Classes marked sealed cannot be inherited further. The keyword “sealed” will prevent the class from being inherited.

When do you use virutal keyword?

We use virtual when we need to override a method of the base class from the sub class, then we give the virtual keyword in the base class method. This makes the method in the base class to be overridable.

What is encapsulation ? Give an example?

Encapsulation is the ability to hide the internal workings of an object's behavior and its data.

For instance, let's say we have a object named Car and this object has a method named run(). When you create an instance of a Car object and call its run() method you are not worried about what happens to accomplish this, you just want to make sure the state of the Car is changed to 'running' afterwards.

This kind of behavior hiding is encapsulation and it makes programming much easier.

What is the Difference between Int and Int32 in .Net?

There is no difference in these two.

Is it possible to use multipe inheritance in C# / VB.Net?

Multiple Inheritance is an ability to inherit from more than one base class i.e. ability of a class to have more than one superclass, by inheriting from different sources and thus combine separately-defined behaviors in a single class.

There are two types of multiple inheritance: multiple type/interface inheritance and multiple implementation inheritance.

C# & VB.NET supports only multiple type/interface inheritance, i.e.you can derive an class/interface from multiple interfaces.

There is no support for multiple implementation inheritance in .NET. That means a class can only derived from one class.

What is the difference between structures and enumeration?

Unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data. They are derived from System.ValueType class.

Enum->An enum type is a distinct type that declares a set of named constants.They are strongly typed constants. They are unique types that allow to declare symbolic names to integral values. Enums are value types, which means they contain their own value, can't inherit or be inherited from and assignment copies the value of one enum to another.
public enum Levels
{
One, Two, Three
}

No comments:

Post a Comment