Showing posts with label .NET OOPS. Show all posts
Showing posts with label .NET OOPS. Show all posts

Decribe the term immutable mean?

The data value may not be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory.

What’s the top .NET class that everything is derived from?

Ans:-System.Object.

Explain the accessibility modifier “protected internal” in one line.

It is available to classes that are within the same assembly and derived from the specified base class

Protected class-level variable available for Which Class?

Ans:- It is available to any sub-class (a class inheriting this class).

Does C# support multiple-inheritance?

Ans:- No

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
}

.NET OOPS

Question: What Is OOPS ?
Answer: OOPs is an Object Oriented Programming language,which is the extension of Procedure Oriented Programming language.OOPS reduce the code of the program because of the extensive feature of Polymorphism. OOPS have many properties such as Data-Hiding,Inheritence,Data Absraction,Data Encapsulation and many moreEverything in the world is an object. The type of the object may vary. In OOPS, we get the power to create objects of our own, as & when required.

Question: what is Class ?
Answer:A group of objects that share a common definition and that therefore share common properties, operations, and behavior. A user-defined type that is defined with the class-key 'class,' 'struct,' or 'union.' Objects of a class type consist of zero or more members and base class objects.Classes can be defined hierarchically, allowing one class to be an expansion of another, and classes can restrict access to their members.

Question: What is Constructor?
Answer:When we create instance of class a special method of that class, called that is constructor. Similarly, when the class is destroyed, the destructor method is called. These are general terms and usually not the actual member names in most object-oriented languages. It is initialized using the keyword New, and is destroyed using the keyword Finalize.

Question: What is Abstract Class ?
Answer:Classes that cannot be instantiated. We cannot create an object from such a class for use in our program. We can use an abstract class as a base class, creating new classes that will inherit from it. Creating an abstract class with a certain minimum required level of functionality gives us a defined starting point from which we can derive non-abstract classes. An abstract class may contain abstract methods & non-abstract methods. When a class is derived from an abstract class, the derived class must implement all the abstract methods declared in the base class. We may use accessibility modifiers in an abstract class.An abstract class can inherit from a non-abstract class. In C++, this concept is known as pure virtual method.

Question: What is ValueType?
Answer:Value Types - Value types are primitive types. Like Int32 maps to System.Int32, double maps to System.double.All value types are stored on stack and all the value types are derived from System.ValueType. All structures and enumerated types that are derived from System.ValueType are created on stack, hence known as ValueType.In value type we create a copy of object and uses there value its not the original one.

Question: What is diff. between abstract class and an interface?
Answer: 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 Ablstract 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 OverLoading:-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.