Saturday, January 4, 2020

OOPs Interview Question

1. What is Object Oriented Programming? Object Oriented Programming (OOP) is a programming paradigm where the complete software operates as a bunch of objects talking to each other. An object is a collection of data and methods that operate on its data.

2. Why OOP?
The main advantage of OOP is better manageable code that covers following.

1) The overall understanding of the software is increased as the distance between the language spoken by developers and that spoken by users.

2) Object orientation eases maintenance by the use of encapsulation.   One can easily change the underlying representation by keeping the methods same. OOP paradigm is mainly useful for relatively big software.
3. What are main features of OOP?
Encapsulation
Polymorphism
Inheritance


4. What is encapsulation?
Encapsulation is referred to one of the following two notions.
1) Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.


5. What is Polymorphism? How is it supported by C++?
Polymorphism means that some code or operations or objects behave differently in different contexts. In C++,  following features support polymorphism.

Compile Time Polymorphism: Compile time polymorphism means compiler knows which function should be called when a polymorphic call is made.  C++ supports compiler time polymorphism by supporting features like templates, function overloading and default arguments.
Run Time Polymorphism: Run time polymorphism is supported by virtual functions. The idea is, virtual functions are called according to the type of object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime.


6. What is Inheritance? What is the purpose?
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class.
The purpose of inheritance is Code Reuse.


7. What is Abstraction?
The first thing with which one is confronted when writing programs is the problem. Typically we are confronted with “real-life” problems and we want to make life easier by providing a program for the problem. However, real-life problems are nebulous and the first thing we have to do is to try to understand the problem to separate necessary from unnecessary details: We try to obtain our own abstract view, or model, of the problem. This process of modeling is called abstraction.

8. What is a class?
A class is simply a representation of a type of object. It is the blueprint/plan/template that describes the details of an object.

9. What is an Object?
An object is an instance of a class. It has its own state, behavior, and identity.
 
10. What are manipulators?
Manipulators are the functions which can be used in conjunction with the insertion (<<) and extraction (>>) operators on an object. Examples are endl and setw.
 
11. Explain the term constructor
A constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation. Rules for constructor are:

  • Constructor Name should be the same as a class name.
  • A constructor must have no return type.
12. Define Destructor?
A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.

13. What is an Inline function?
An inline function is a technique used by the compilers and instructs to insert complete body of the function wherever that function is used in the program source code.

14. What is a virtual function?
A virtual function is a member function of a class, and its functionality can be overridden in its derived class. This function can be implemented by using a keyword called virtual, and it can be given during function declaration.
A virtual function can be declared using a token(virtual) in C++. It can be achieved in C/Python Language by using function pointers or pointers to function.

15.  What is a friend function?
A friend function is a friend of a class that is allowed to access to Public, private, or protected data in that same class. If the function is defined outside the class cannot access such information.
A friend can be declared anywhere in the class declaration, and it cannot be affected by access control keywords like private, public, or protected.

16. What is function overloading?
Function overloading is a regular function, but it can perform different tasks. It allows the creation of several methods with the same name which differ from each other by the type of input and output of the function.

17. What is operator overloading?
Operator overloading is a function where different operators are applied and depends on the arguments. Operator,-,* can be used to pass through the function, and it has its own precedence to execute

18. What is an abstract class?
An abstract class is a class which cannot be instantiated. Creation of an object is not possible with an abstract class, but it can be inherited. An abstract class can contain only an Abstract method. Java allows only abstract method in abstract class while other languages allow non-abstract method as well.

19. What are the different types of arguments?
A parameter is a variable used during the declaration of the function or subroutine, and arguments are passed to the function body, and it should match with the parameter defined. There are two types of Arguments.

  • Call by Value – Value passed will get modified only inside the function, and it returns the same value whatever it is passed into the function.
  • Call by Reference – Value passed will get modified in both inside and outside the functions and it returns the same or different value.
20. What is the super keyword?
The super keyword is used to invoke the overridden method, which overrides one of its superclass methods. This keyword allows to access overridden methods and also to access hidden members of the superclass.
It also forwards a call from a constructor, to a constructor in the superclass.

21. What is method overriding?
Method overriding is a feature that allows a subclass to provide the implementation of a method that overrides in the main class. It will override the implementation in the superclass by providing the same method name, same parameter, and same return type.

22. What is an interface?
An interface is a collection of an abstract method. If the class implements an interface, it thereby inherits all the abstract methods of an interface.
Java uses Interface to implement multiple inheritances.

23. What is exception handling?
An exception is an event that occurs during the execution of a program. Exceptions can be of any type – Runtime exception, Error exceptions. Those exceptions are adequately handled through exception handling mechanism like try, catch, and throw keywords.

24.  What is the main difference between overloading and overriding?
Overloading is static Binding, whereas Overriding is dynamic Binding. Overloading is nothing but the same method with different arguments, and it may or may not return the equal value in the same class itself.
Overriding is the same method names with the same arguments and return types associated with the class and its child class.

25. What is the main difference between a class and an object?
An object is an instance of a class. Objects hold multiple information, but classes don’t have any information. Definition of properties and functions can be done in class and can be used by the object.
A class can have sub-classes, while an object doesn’t have sub-objects.

26. What are the access modifiers?
Access modifiers determine the scope of the method or variables that can be accessed from other various objects or classes. There are five types of access modifiers, and they are as follows:

  • Private
  • Protected
  • Public
  • Friend
  • Protected Friend
27. What are the various types of constructors? There are three types of constructors:
–  Default Constructor – With no parameters.
–  Parametric Constructor – With Parameters. Create a new instance of a class and also passing arguments simultaneously.
–  Copy Constructor – Which creates a new object as a copy of an existing object.

28. What is ‘this’ pointer?
THIS pointer refers to the current object of a class. THIS keyword is used as a pointer which differentiates between the current object with the global object. It refers to the current object.

29. What is a pure virtual function?
A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.

30. What are all the operators that cannot be overloaded?
Following are the operators that cannot be overloaded -.

  1. Scope Resolution (::)
  2. Member Selection (.)
  3. Member selection through a pointer to function (.*)

 31. 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.
 

No comments:

Post a Comment