Object-Oriented Programming - .NET Interview Questions and Answers

  1. What do you mean by data encapsulation?
    Data encapsulation is a concept of binding data and code in single unit called object and hiding all the implementation details of a class from the user. It prevents unauthorized access of data and restricts the user to use the necessary data only.
  2. What is the difference between procedural and object-oriented programming?
    Procedural programming is based upon the modular approach in which the larger programs are broken into procedures. Each procedure is a set of instructions that are executed one after another. On the other hand, OOP is based upon objects. An object consists of various elements, such as methods and variables.

    Access modifiers are not used in procedural programming, which implies that the entire data can be accessed freely anywhere in the program. In OOP, you can specify the scope of a particular data by using access modifiers - public, private, internal, protected, and protected internal.
  3. Explain the concept of destructor?
    A destructor is a special method for a class and is invoked automatically when an object is finally destroyed. The name of the destructor is also same as that of the class but is followed by a prefix tilde (~).

    A destructor is used to free the dynamic allocated memory and release the resources. You can, however, implement a custom method that allows you to control object destruction by calling the destructor.

    The main features of a destructor are as follows:
    • Destructors do not have any return type
    • Similar to constructors, destructors are also always public
    • Destructors cannot be overloaded.
  4. Can you declare a private class in a namespace?
    The classes in a namespace are internal, by default. However, you can explicitly declare them as public only and not as private, protected, or protected internal. The nested classes can be declared as private, protected, or protected internal.
  5. A structure in C# can implement one or more interfaces. Is it true or false?
    Yes, it is true. Like classes, in C#, structures can implement one or more interfaces.
  6. What is a static constructor?
    Static constructors are introduced with C# to initialize the static data of a class. CLR calls the static constructor before the first instance is created.

    The static constructor has the following features:
    • No access specifier is required to define it.
    • You cannot pass parameters in static constructor.
    • A class can have only one static constructor.
    • It can access only static members of the class.
    • It is invoked only once, when the program execution begins.