OOPS Interview Questions

What is OOPS?

OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

What are the basic concepts of OOPS?

Following are the concepts of OOPS and are as follows:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

What are OOPS design principles?

  • SRP – The Single Responsibility Principle
    • A class should have one and only one reason to change
  • OCP- The Open Closed Principle
    • Should be able to extend any classes’ behaviors without modifying the classes.
  • LSP- The Liskov Substitution Principle
    • Derived classes must be substitutable for their base classes.
  • DIP- The Dependency Inversion Principle
    • Depend on abstractions, not on concretions.
  • ISP- The Interface Segregation Principle
    • Make fine grained interfaces that are client specific.

What is the Class?

  • A class is simply a representation of a type of object.
  • It is the blueprint or plan or template, that describes the details of an object.
  • Class is composed of three things: a name, attributes, and operations.

Examples:-

  1. Here we can take Human Being as a class. Human Being, having body parts and performing various actions.
  2. The TextBox control, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.

What is Inheritance?

  • Inheritance is a concept where one class shares the structure and behavior defined in another class.

Examples:-

Considering HumanBeing a class, which has properties like hands, legs, eyes, etc, and functions like walk, talk, eat, see, etc. Male and Female are also classes, but most of the properties and functions are included in HumanBeing, hence they can inherit everything from class HumanBeing using the concept of Inheritance.

What is Object?

  • An object is an instance of a class.
  • It has its own state, behavior, and identity.

Examples:-

My name is Santosh , and I am an instance/object of class Male.

What is Encapsulation?

  • Encapsulation is defined as the process of enclosing one or more details from the outside world through access right.
  • Levels are Public, Protected, Private, Internal and Protected Internal.
  • Encapsulation is when a group of related methods, properties, and other members are treated as a single object.

Examples:

Our Legs are bound to help us walk. Our hands, help us hold things. This binding of the properties to functions is called Encapsulation.

What is an abstraction?

  • Abstraction allows us to expose limited data and functionality of objects publicly and hide the actual implementation
  • Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
  • Abstraction is hiding the information or providing only necessary details to the client.

Examples:-

  1. Human Being’s can talk, walk, hear, eat, but the details are hidden from the outside world.
  2. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.

What is Polymorphism?

  • Polymorphism can be defined as the ability to use the same name for doing different things.
  • More precisely we say it as ‘many forms of single entity’.
  • Dynamic polymorphism (runtime time). You can obtain this type through executing function overriding.
  • Static polymorphism (compile time). You can achieve static polymorphism through function overloading and operator overloading.

Example:-

  1. If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called Overloading.
    And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called Overriding.
  2. You are ordering the pizza for that you have two options for paying one is an online payment(compile time) and another one is payment at delivering (run time).

What is the difference between a virtual method and an abstract method? 

A virtual method must have a body whereas an abstract method should not have a body.

Define a constructor.

  • A constructor is a method used to initialize the state of an object.
  • It gets invoked at the time of object creation.
  • Usually used to initialize the data members of the new object.
  • Constructor Name should be the same as the class name.
  • A constructor must have no return type.

What will happen if you do not explicitly provide a constructor for a class?

If you do not provide a constructor explicitly for your class, C# will create one by default that instantiates the object and sets all the member variables to their default values.

Can we have constructors for static classes?

Yes, static classes can also have constructors.

Can a class have multiple constructors?

YES

Can a child class call the constructor of a base class?

YES

If a child class instance is created, which class constructor is called first – base class or child class?

The base class constructor is called before the child class constructor.

Can a class have a static constructor?

  • Yes, a class can have a static constructor.
  • It is called automatically before the first instance is created or any static members are referenced.
  • Static constructors are called before instance constructors

Can you mark static constructor with access modifiers?

NO

Can you have parameters for static constructors?

NO

Define Destructor.

  • Destructor is a special method called automatically during the destruction of an object / when the object is made of scope or destroyed.
  • The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
  • Destructor name is also same as class name but with the tilde symbol before the name

What is an Inline function?

What is a virtual function?

What is a friend function?

What is function overloading?

What is operator overloading?

What is an abstract class?

What is a ternary operator?

What is the use of the finalize method?

What are the different types of arguments?

What is the super keyword?

What is method overriding?

What is an interface?

  • It is similar to a class but without implementation & only declaration of properties, methods, delegates & events.
  • The interface actually enforces the class to have a standard contract to provide all implementation to the interface members.

Difference between overloading and overriding?

Difference between a class and an object?