Following Object-Oriented Programming questions can be used by both freshers preparing for an interview & experienced professionals revising their basics.
- What is Object Oriented Programming?
Object-Oriented Programming (OOP) is a software programming technique where the focus is on objects rather than procedures. In OOP, an instance of a class is termed as Object.
- What are the basic concepts of OOPS?
The basic concepts of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism.
- What are Manipulators?
These are the functions that be used on an object in conjunction with the operators’ insertion (<<) and extraction (>>) . For eg. endl and setw.
- What is a class?
A class is the basic representation of an object. It contains the details of an object.
- What is an object?
An object refers to the specific instance of a class where the object can be a mixture of data structures, functions and variables.
- Difference between class and an object?
- An Object can contain information while a class cannot hold any information.
- It is possible for a class to have subclasses whereas an Object cannot have sub-objects.
- What is the difference between structure and a class?
- A class groups data and methods while a structure groups only data
- By default, the structure access type is public while that of a class is private
- Structures do not require strict validation while class requires strict validation as it encapsulates and inherits data.
- What is encapsulation?
Encapsulation can refer to either Data hiding or Bundling of data & methods together.
- Data hiding: This is a language feature to confine access only to the object members. Eg. private & protected members in C++.
- Bundling of data and methods together: This refers to how methods and data that function on that particular data are bundled together.
- What is Polymorphism? How is it supported by C++?
According to Polymorphism, in different contexts, some objects/ code/operations behave differently.
These features support polymorphism in C++
- Compile Time Polymorphism: According to Compile time polymorphism, when a polymorphic call is made the compiler should know which operation is to be executed. Supporting attributes in C++ like templates, function overloading and default arguments enables it to supports compile time polymorphism
- Run Time Polymorphism: Virtual functions support this polymorphism. They are called according to the kind of object referred or pointed, not by kind of type of pointer or reference. In short, virtual functions are determined late, at runtime.
- What is Inheritance? What is the purpose?
- The basic purpose of Inheritance is Code Reuse. According to this principle, a class is constructed on another class and the utilizes the data and implementation of the latter.
- What is early and late binding?
The compiler performs a process where it matches the function call with the function definition. This is termed as Binding. It can take place during compile time or at runtime. Early binding is also called as Static binding. It means assigning the value to a variable during compile time. Late binding is also called as Dynamic binding; the values to the variables are assigned during runtime.
- What is Abstraction?
The first step in writing a program is to define a problem. These problems are derived from real-world issues that the programmer wants to find a solution to. It is necessary to sift relevant details from irrelevant ones when it comes to real-world issues. In this process, the programmer comes up with his own abstract model of the problem. This process is termed as Abstraction.
- Define a constructor?
The constructor is a technique used to initialize the state of an object which gets invoked during object creation.
- What are the rules for a Constructor?
- The name of the constructor should be the same as that of the class
- It should not have a return type
- Define Destructor?
A Destructor is a function that is called automatically when the object is destroyed or when its scope ends.
- What is an Inline function?
An inline function is an enhancement feature in C++ that is used to increase the program’s execution time.
- What is a virtual function?
A virtual function is a member function declared within a base class which can be overridden by the derived class. You can call a virtual function for a derived class object when you refer to it using a pointer or a reference to the base class and then execute the function version of the derived class.
The virtual function makes certain the correct function is called for an object irrespective of the kind of pointer/reference used for the function call. The keyword virtual can be used to implement this function.
- What is a friend function?
A friend function is a friend of a class that is given access to that class’s public, private and protected data. It cannot access the data if it is defined outside that class. It can be defined anywhere in the class declaration and access control keywords like public, private or protected cannot affect it.
- What is function overloading?
Function overloading is a function that enables the programmers to create multiple functions with the same name but different implementation. In other words, these methods with the same name differ from each other by the kind of input and output of the function.
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);
- What is operator overloading?
Operator overloading is also called as ad-hoc polymorphism. It is a function where all the operators have different implementation depending on its argument type.
Operator overloading is usually defined by the programming language, programmer or both.
- What is an abstract class?
An abstract class is a class which can contain only abstract methods. This class also cannot be instantiated and requires subclasses to give implementation to the abstract methods.
- What is a ternary operator?
In several programming languages, the ternary operator is also called a conditional operator. It is an operator which takes three arguments: the first argument is a comparison argument, the second argument is the result of a true comparison and the last argument is the result of a false comparison. It is like a short way of writing an if-else statement.
- What is the use of finalize method?
Finalize method is a technique that enables the programmer to clean up the resources which are not being used currently. This method is protected and hence accessible only to that particular class or by a derived class.
- What is a token?
Token, in programming, is a basic component of a code that is recognized by the compiler. Eg. Identifiers, String Literals, Constants & Operators.
- What are access modifiers?
Access modifiers are keywords that determine the accessibility of the methods, classes and other members.
- List out the five access modifiers
The 5 types of access modifiers are Private, Protected, Public, Friend & Protected friend.
- What are sealed modifiers?
Sealed modifiers are the access modifiers that cannot be inherited and are concrete. They cannot be applied to static members and can be applied to instance methods, events, indexes, and properties.
- How can we call the base method without creating an instance?
It is possible
- if it is a static method
- By inheriting from that particular class
- Using basic keyword from a derived class
- What is the difference between new and override?
While the new modifier tells the compiler to use the new implementation rather than the base class function, Override modifier helps to override base class function.
- What are the various types of constructors?
There are three types of constructors. They are:
- Default Constructor
- Parametric Constructor
- Copy Constructor