Top 50 2022 Interview Questions Flashcards
What is the difference between OOP and SOP?
Object-Oriented Programming
Object-Oriented Programming is a type of programming which is based on objects rather than just functions and procedures
Bottom-up approach, provides data hiding
Can solve problems of any complexity
Code can be reused thereby reducing redundancy
Structural Programming
Provides logical structure to a program where programs are divided functions
Top-down approach, does not provide data hiding
Can solve moderate problems, does not support code reusability
What is Object Oriented Programming?
Object-Oriented Programming(OOPs) is a type of programming that is based on objects rather than just functions and procedures. Individual objects are grouped into classes. OOPs implements real-world entities like inheritance, polymorphism, hiding, etc into programming. It also allows binding data and code together.
Why use OOPs?
OOPs allows clarity in programming thereby allowing simplicity in solving complex problems
Code can be reused through inheritance thereby reducing redundancy
Data and code are bound together by encapsulation
OOPs allows data hiding, therefore, private data is kept confidential
Problems can be divided into different parts making it simple to solve
The concept of polymorphism gives flexibility to the program by allowing the entities to have multiple forms
What are the main features of OOPs?
Inheritance
Encapsulation
Polymorphism
Data Abstraction
What is an object?
An object is a real-world entity which is the basic unit of OOPs for example chair, cat, dog, etc. Different objects have different states or attributes, and behaviors.
What is a class?
A class is a prototype that consists of objects in different states and with different behaviors. It has a number of methods that are common the objects present within that class.
What is the difference between a class and a structure?
Class: User-defined blueprint from which objects are created. It consists of methods or set of instructions that are to be performed on the objects.
Structure: A structure is basically a user-defined collection of variables which are of different data types.
Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if:
It is a static method The base class is inherited by some other subclass
What is the difference between a class and an object?
Object
A real-world entity which is an instance of a class
Acts like a variable of the class
An object is a physical entity
Objects take memory space when they are created
Objects can be declared as and when required
Class
A class is basically a template or a blueprint within which objects can be created
Binds methods and data together into a single unit
A class is a logical entity
A class does not take memory space when created
Classes are declared just once
What is inheritance?
Inheritance is a feature of OOPs which allows classes inherit common properties from other classes. For example, if there is a class such as ‘vehicle’, other classes like ‘car’, ‘bike’, etc can inherit common properties from the vehicle class. This property helps you get rid of redundant code thereby reducing the overall size of the code.
What are the different types of inheritance?
Single inheritance Multiple inheritance Multilevel inheritance Hierarchical inheritance Hybrid inheritance
What is the difference between multiple and multilevel inheritance?
Multiple Inheritance Multiple inheritance comes into picture when a class inherits more than one base class Example: A class defining a child inherits from two base classes Mother and Father
Multilevel Inheritance Multilevel inheritance means a class inherits from another class which itself is a subclass of some other base class Example: A class describing a sports car will inherit from a base class Car which inturn inherits another class Vehicle
What is hybrid inheritance?
Hybrid inheritance is a combination of multiple and multi-level inheritance.
What is hierarchical inheritance?
Hierarchical inheritance refers to inheritance where one base class has more than one subclasses. For example, the vehicle class can have ‘car’, ‘bike’, etc as its subclasses.
What are the limitations of inheritance?
Increases the time and effort required to execute a program as it requires jumping back and forth between different classes The parent class and the child class get tightly coupled Any modifications to the program would require changes both in the parent as well as the child class Needs careful implementation else would lead to incorrect results
What is a superclass?
A superclass or base class is a class that acts as a parent to some other class or classes. For example, the Vehicle class is a superclass of class Car.
What is a subclass?
A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a derived of Vehicle class.
What is polymorphism?
Polymorphism refers to the ability to exist in multiple forms. Multiple definitions can be given to a single interface. For example, if you have a class named Vehicle, it can have a method named speed but you cannot define it because different vehicles have different speed. This method will be defined in the subclasses with different definitions for different vehicles.
What is static polymorphism?
Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of compile-time polymorphism is method overloading.