This blog will cover the Q&As of Python for Data Science (AI/ML) and Data Engineer Training covering Python Basic Concepts of OOPs & Error and Exceptions Handling Q&A: Day 5 Live Session Review. This blog will help you to clear your concepts with Basic Concepts of OOPs & errors and Exceptions Handling.
We also covered hands-on Lab 16, Lab 17, Lab 18 out of our 25+ extensive labs.
So, here are some of the Q & As asked during the Live session from Module 5: Basic Concepts of OOPs & Module 6: Error and Exceptions Handling.
Classes and Objects
An object is a real-world entity that is the basic unit of OOPs for example chair, cat, dog, etc. Different objects have different states or attributes, and behaviors.
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 to the objects present within that class.
Q1: What is the difference between a class and an object?
Ans: Difference between Classes and Objects are:
Objects | Classes |
1. A real-world entity is an instance of a class. | 1. A class is basically a template or a blueprint within which objects can be created. |
2. An object acts like a variable of the class. | 2. Binds methods and data together into a single unit. |
3. An object is a physical entity. | 3. A class is a logical entity. |
4. Objects take memory space when they are created. | 4. A class does not take memory space when created. |
5. Objects can be declared as and when required. | 5. Classes are declared just once. |
Read More: About Data Science Career Path.
Basic Concepts of OOPs
An object-oriented paradigm is to design the program using classes and objects. The object is related to real-world entities such as books, houses, pencils, etc. The oops concept focuses on writing the reusable code. It is a widespread technique to solve the problem by creating objects.
There are four fundamental concepts of Object-oriented programming –
Q2: What is Inheritance?
Ans: Inheritance is an important aspect of the object-oriented paradigm. Inheritance provides code reusability to the program because we can use an existing class to create a new class instead of creating it from scratch.
In python, a derived class can inherit the base class by just mentioning the base in the bracket after the derived class name. Consider the following syntax to inherit a base class into the derived class.
class derived-class(base class): <class-suite>
Q3: What are the different types of Inheritance?
Ans: There are mainly five types of Inheritance:
- Single inheritance
- Multiple inheritances
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Q4: What is Encapsulation?
Ans: Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.
Also Check: Our blog post on Python loops.
Q5: What are ‘access specifiers’?
Ans: Access specifiers or access modifiers are keywords that determine the accessibility of methods, classes, etc in OOPs. These access specifiers allow the implementation of encapsulation. The most common access specifiers are public, private, and protected. However, there are a few more that are specific to the programming languages.
Q6: What is Data Abstraction?
Ans: Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts. It is one of the most important concepts of OOPs.
Q7: Differentiate between Data abstraction and Encapsulation?
Ans:
Data Abstraction | Encapsulation |
1. Solves the problem at the design level | 1. Solves the problem at the implementation level |
2. Allows showing important aspects while hiding implementation details | 2. Binds code and data together into a single unit and hides it from the world |
3. It deals with ideas rather than events. | 3. The idea is to protect the data from the outside world. |
4. Achieved via encapsulation. | 4. Achieved once the members of a class are designated as ‘private’. |
5. Data abstraction can be performed using Interface and an abstract class. | 5. Data encapsulation can be performed using the different access modifiers like protected, private, and packages. |
Q8: What is Polymorphism?
Ans: Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class.
Q9: Differentiate between overloading and overriding?
Ans:
Overloading | Overriding |
1. Two or more methods having the same name but different parameters or signature | 1. Child class redefining methods present in the base class with the same parameters/ signature |
2. Resolved during compile-time | 2. Resolved during runtime |
3. It is performed within a class | 3. Occurs in two classes having IS-A (inheritance) relationship. |
4. Static binding is being used for method overloading. | 4. Dynamic binding is being used for method overriding. |
5. Better performance is given by method overloading. | 5. Lesser performance compared to method overloading. |
Check Out: Our blog post on Python Data Types.
Errors and Exceptions in Python
Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program.
Two types of Error occurs in python.
- Syntax errors
- Logical errors (Exceptions)
Q10: What is Syntax errors?
Ans: When the proper syntax of the language is not followed then a syntax error is thrown.
amount = 10000 if(amount>2999) print("You are eligible to purchase Dsa Self Paced")
It returns a syntax error message because after the if statement a colon: is missing. We can fix this by writing the correct syntax.
Q11: What is Exception or Logical Error?
Ans: When in the runtime an error occurs after passing the syntax test is called exception or logical type. For example, when we divide any number by zero then the ZeroDivisionError exception is raised, or when we import a module that does not exist then ImportError is raised.
marks = 10000 a = marks / 0 print(a)
In the above example the ZeroDivisionError as we are trying to divide a number by 0
Q12: Do runtime errors exist in Python? Give an example?
Ans: Yes, runtime errors exist in Python. For example, if you are duck typing and things look like a duck, then it is considered as a duck even if that is just a flag or stamp or any other thing. The code, in this case, would be A Run-time error.
For example, Print “Hello World”, then the runtime error would be the missing parenthesis that is required by print ( ).
Also Read: Our blog post on Python Data Visualization.
Q13: What is Error Handling?
Ans: When an error and an exception is raised then we handle that with the help of the Handling method.
- Handling Exceptions with Try/Except/Finally
We can handle errors by the Try/Except/Finally method. We write unsafe code in the try, fall back code in except and final code in finally block.
try: print("code start") print(1 / 0) except: print("an error occures") finally: print("K21 Academy")
- Raising exceptions for a predefined condition
When we want to code for the limitation of certain conditions then we can raise an exception.
try: amount = 199 if amount < 297: raise ValueError "please add money in your account") else: print("You are eligible to purchase Python Self Paced course") except ValueError as e: print(e)
Q14: Can we write only try block without a catch and finally blocks?
Ans: No, it shows a compilation error. The try block must be followed by either catch or finally block. You can remove either catch block or finally block but not both.
Q15: Does finally block get executed if either try or catch blocks are returning the control?
Ans: Yes, finally block will always be executed no matter whether try or catch blocks are returning the control or not.
Q16: What is the difference between catch block and finally block?
Ans: The difference between catch block and finally block is as follows:
Catch block | Finally block |
1. Catch block is used to handle an exception thrown by try block. | 1. Finally the block is used to execute important code such as closing connection |
2. Catch block is executed only when an exception occurs in the try block otherwise it is skipped. | 2. Finally the block is always executed whether an exception occurs or not. |
3. We can use multiple catch blocks for a single try block. | 3. Only one finally block can be used for a single try block. |
Related References
- Python Loops and Control Statements
- Python Methods and Functions
- Python For Data Science: Why, How & Libraries Used
- Natural Language Processing with Python
- Python For Beginners: Overview, Features & Career Opportunities
Next Task For You…
Python’s growth is very promising in the near future. Gaining the right skills through the right platform will get you to the perfect job.
We are launching our course Python For Data Science (AI/ML) & Data Engineers (Python For Beginners) which will you help and guide you towards your first steps to Python. Join our Waitlist for the FREE CLASS now.
Leave a Reply