OOPs-Top-30-Interview-Questions Pythons
OOPs-Top-30-Interview-Questions Pythons
Interview Question
Q 2. Why OOPs?
Ans :
The main advantage of OOP is better manageable code that covers the
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 the same.
3) The OOPs paradigm is mainly useful for relatively big software.
Q 3. What is a Class?
©Topperworld
©Topperworld
Q 4. What is an Object?
Eg. The code below shows is an example in C++ of how an instance of a class
(i.e an object ) of a class is created
#include <iostream>
using namespace std;
class Student{
private:
string name;
string surname;
int rollNo;
public:
Student(string studentName, string studentSurname, int
studentRollNo){
name = studentName;
surname = studentSurname;
rollNo = studentRollNo;
}
void getStudentDetails(){
cout <<"The name of the student is "<< name
©Topperworld
©Topperworld
Output :
Ans :The main feature of the OOPs, also known as 4 pillars or basic principles
of OOPs are as follows:
❖ Encapsulation
❖ Data Abstraction
❖ Polymorphism
❖ Inheritance
©Topperworld
©Topperworld
Q 6. What is Encapsulation?
Ans :Encapsulation is the binding of data and methods that manipulate them
into a single unit such that the sensitive data is hidden from the users
2) Bundling of data and methods together: Data and methods that operate
on that data are bundled
together.
For example, the data
members and member
methods that operate on them
are wrapped into a single unit
known as a class.
©Topperworld
©Topperworld
©Topperworld
©Topperworld
Q 7. What is Abstraction?
Q 8. What is Polymorphism?
©Topperworld
©Topperworld
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early
binding is the type of polymorphism where the binding of the call to its code
is done at the compile time.
Method overloading or operator overloading are examples of compile-time
polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime
polymorphism is the type of polymorphism where the actual implementation
of the function is determined during the runtime or execution. Method
overriding is an example of this method.
Ans : The idea of inheritance is simple, a class is derived from another class
and uses data and implementation of that other class.
The class which is derived is called child or derived or subclass and the class
from which the child class is derived is called parent or base or superclass.
The main purpose of Inheritance is to increase code reusability. It is also
used to achieve Runtime Polymorphism.
©Topperworld
©Topperworld
// an example of inheritance
class Student {
public void read() {
System.out.println("The student is reading");
}
}
class SchoolStudent extends Student {
public void read(String book) {
System.out.println("the student is reding "+
book);
}
}
Ans : Access specifiers are special types of keywords that are used to
specify or control the accessibility of entities like classes, methods, and so
on. Private, Public, and Protected are examples of access specifiers or access
modifiers.
The key components of OOPs, encapsulation and data hiding, are largely
achieved because of these access specifiers.
class User {
public String userName;
protected String userEmail;
private String password;
public void setPassword(String password) {
this.password = password;
}
}
©Topperworld
©Topperworld
Ans :
Fast to implement and easy to The length of the programs is much larger
©Topperworld
©Topperworld
©Topperworld
©Topperworld
©Topperworld
©Topperworld
Ans :
Structured Programming is a technique that is considered a precursor to OOP
and usually consists of well-structured and separated modules.
It is a subset of procedural programming.
The difference between OOPs and Structured Programming is as follows:
It follows a bottom-to-top
approach. It follows a Top-to-Down approach.
©Topperworld
©Topperworld
globally and code lines are making calls as per the need of code for
processed one by one i.e., Run a certain time.
sequentially.
Ans :
OOPs paradigm is one of the most popular programming paradigms.
It is widely used in many popular programming languages such as:
◆ C++
◆ Java
◆ Python
◆ Javascript
◆ C#
◆ Ruby
©Topperworld
©Topperworld
Ans : Polymorphism can be classified into two types based on the time when
the call to the object or function is resolved. They are as follows:
1) Compile Time Polymorphism
2) Runtime Polymorphism
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early
binding is the type of polymorphism where the binding of the call to its code
is done at the compile time.
Method overloading or operator overloading are examples of compile-time
polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime
polymorphism is the type of polymorphism where the actual implementation
of the function is determined during the runtime or execution.
Method overriding is an example of this method.
©Topperworld
©Topperworld
Ans :
Yes, there are more challenges when you have more authority. Although
inheritance is a very strong OOPs feature, it also has significant drawbacks.
⚫ As it must pass through several classes to be implemented, inheritance
takes longer to process.
⚫ The base class and the child class, which are both engaged in inheritance,
are also closely related to one another (called tightly coupled).
Therefore, if changes need to be made, they may need to be made in
both classes at the same time.
⚫ Implementing inheritance might be difficult as well. Therefore, if not
implemented correctly, this could result in unforeseen mistakes or
inaccurate outputs.
©Topperworld
©Topperworld
Ans :
Inheritance can be classified into 5 types which are as follows:
1) Single Inheritance: Child class derived directly from the base class
2) Multiple Inheritance: Child class derived from multiple base classes.
3) Multilevel Inheritance: Child class derived from the class which is also
derived from another base class.
4) Hierarchical Inheritance: Multiple child classes derived from a single
base class.
5) Hybrid Inheritance: Inheritance consisting of multiple inheritance
types of the above specified.
©Topperworld
©Topperworld
Ans : A unique class type known as an interface contains methods but not
their definitions.
Inside an interface, only method declaration is permitted.
You cannot make objects using an interface. Instead, you must put that
interface into use and specify the procedures for doing so.
Ans :Both abstract classes and interfaces are special types of classes that
just include the declaration of the methods, not their implementation.
An abstract class is completely distinct from an interface, though.
Following are some major differences between an abstract class and an
interface.
An abstract class can have final, non- The interface has only static and
©Topperworld
©Topperworld
Ans :
No. If the base class includes non-static methods, an object must be
constructed.
But no objects need to be generated if the class includes static methods.
In this instance, you can use the class name to directly call those static
methods.
©Topperworld
©Topperworld
Ans :
The structure is also a user-defined datatype in C++ similar to the class with
the following differences:
⚫ The major difference between a structure and a class is that in a
structure, the members are set to public by default while in a class,
members are private by default.
⚫ The other difference is that we use struct for declaring structure
and class for declaring a class in C++.
Ans :
A constructor is a block of code that initializes the newly created object.
A constructor resembles an instance method but it’s not a method as it
doesn’t have a return type.
It generally is the method having the same name as the class but in some
languages, it might differ.
For example:
In python, a constructor is named __init__.
In C++ and Java, the constructor is named the same as the class name.
class Student {
String name;
int rollNo;
Student()
{
System.out.println("contructor is called");
}
}
©Topperworld
©Topperworld
Ans :
The most common classification of constructors includes:
1) Default Constructor
2) Parameterized Constructor
3) Copy Constructor
1. Default Constructor
• The default constructor is a constructor that doesn’t take any
arguments. It is a non-parameterized constructor that is automatically
defined by the compiler when no explicit constructor definition is
provided.
• It initializes the data members to their default values.
2. Parameterized Constructor
• It is a user-defined constructor having arguments or parameters.
3. Copy Constructor
• A copy constructor is a member function that initializes an object using
another object of the same class.
• In Python, we do not have built-in copy constructors like Java and C++
but we can make a workaround using different methods.
©Topperworld
©Topperworld
Ans :
No. A destructor cannot be overloaded in a class. The can only be one
destructor present in a class.
©Topperworld
©Topperworld
Ans :
A pure virtual function, also known as an abstract function is a member
function that doesn’t contain any statements.
This function is defined in the derived class if needed.
ABOUT US
➢ Our Vision
❖ Our vision is to create a world where every college student can easily
access high-quality educational content, connect with peers, and achieve
their academic goals.
❖ We believe that education should be accessible, affordable, and engaging,
and that's exactly what we strive to offer through our platform.
©Topperworld
©Topperworld
❖ Education is not just about textbooks and lectures; it's also about forming
connections and growing together.
❖ TopperWorld encourages you to engage with your fellow students, ask
questions, and share your knowledge.
❖ We believe that collaborative learning is the key to academic success.
©Topperworld
“Unlock Your
Potential”
With- Topper World
Explore More
topperworld.in
Follow Us On
E-mail
[email protected]