Saturday, July 6, 2019

Oops Interview Questions and Answers

1. What is OOPS?

Answer:

OOPs stand for Object Oriented Programming System.

It is a technique of programming that focuses on the object.

Object means a real-world entity such as a pen, keyboard, chair, table, computer, Book etc. 

Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.

An Object can be defined as an instance of a class.

2. What are the main principles of OOPS?

Answer:

It's main principles include -

Abstraction is hiding the complexity of the program that simplifies the program.

Encapsulation is binding the code and data and protecting it from being tampered.

Inheritance refers reusability of the code where new object can inherit the property of parent object.

Polymorphism means having many forms.

3. What is a class?

Answer:

A class is a template from which objects are created. A class is a blueprint, or prototype which defines and describes the member attributes and member functions.

4. What is an object?

Answer:

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

Software objects are modeled after real-world objects such as a pen, keyboard, chair, table, computer, Book etc. 

Just as Real-world objects have its state and behavior; Software objects too have state and behavior. 

A software object maintains its state in variables and implements its behavior with methods.

5. What is Encapsulation?

Answer:

Encapsulation is a principle of wrapping data (variables) and code together as a single unit.

Encapsulation is like your bag in which you can keep your pen, book etc.

Encapsulation means hiding the internal details of an object

Encapsulation is a technique used to protect the information in an object from the other object

6. What is Polymorphism?

Answer:

The word polymorphism means having many forms. It is the ability of a message to be displayed in more than one form.

Like a man at a same time is a father, a husband, a employee. So, a same person posses different behavior in different situations. This is called polymorphism.

7. What is Inheritance?

Answer:

Inheritance facilitates reusability and is an important concept of OOPs. Just like a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class.

8. Define a constructor.

Answer:

A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation. 

A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object

9. Define Destructor.

Answer:

A destructor is a method which is automatically called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime

11. What is a virtual function?

Answer:

A virtual function is a member function which is declared within base class and is re-defined (Overriden) by derived class.

You use virtual functions when you want to override a certain behavior for your derived class at run-time.

12. What is a friend function?

Answer:

If a function is defined as a friend function then the protected and private data of a class can be accessed using the function.

13. What is function overloading?

Answer:

Function overloading is a feature where two or more functions can have the same name but different parameters.

The function can perform different operations and hence eliminates the use of different function names for the same kind of operations.

Example
void Show(int& a, int& b);
void Show(double& a, double& b);
void Show(struct bob& a, struct bob& b);

14. What is operator overloading?

Answer:

It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type.

15. What is an abstract class?

Answer:

Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does.

A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.

16. What is an interface?

Answer:

Interfaces specify what a class must do and not how. It is the blueprint of the class.

An interface can have methods and variables, but the methods declared in interface are by default abstract

It is used to achieve total abstraction.

17. Difference between overloading and overriding.

Answer:

Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.

Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.

The idea behind method overriding is to change the implementation of given method in a subclass. In other words you “override” the implementation of the parent’s class method using the same signature of the method (name, return types, parameters), but implement different functionality inside the overridden method.

18. Difference between a class and an object.

Answer:

An instance of a class is known as an object. A template or blueprint with which objects are created is known as a Class.

Object allocates memory when it is created. Class doesn't allocate memory when it is created.

Object is a physical entity. Class is a logical entity.

19. What is an abstraction?

Abstraction is an important feature of OOPS, and it shows only the necessary details to the client of an object. 

Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user. In other words, the user will have the information on what the object does instead of how it does.

20. What is early and late binding?

The compiler performs a process called binding when an object is assigned to an object variable. The early binding (static binding) refers to compile time binding and late binding (dynamic binding) refers to runtime binding.

While Early Binding, methods, functions and properties which are detected and checked during compile time and perform other optimizations before an application executes. The biggest advantage of using early binding is for performance and ease of development.

In Late binding functions, methods, variables and properties are detected and checked only at the run-time. It implies that the compiler does not know what kind of object or actual type of an object or which methods or properties an object contains until run time.

21. What is a copy constructor?

This is a special constructor for creating a new object as a copy of an existing object. There will always be only one copy constructor that can be either defined by the user or the system.

22. What is static and dynamic binding?

Connecting a method call to the method body is known as binding.

When compiler acknowledges all the information required to call a function or all the values of the variables during compile time, it is called “static binding”.

Calling a function or assigning a value to a variable, at run-time is called “Dynamic Binding”



No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...