OOPs Interview Questions and Answers

OOPs Interview Questions and Answers


Class
It is a collection of objects of the same type.
It is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind.
Eg. Human


Object
It is a real-world entity or instance of a class.
Eg. Men and women


Abstraction
An act of representing the task to be done in a system in such a way that anyone can use the system without having any internal knowledge of the workings of the system.
Eg: We use readLine() without knowing how it takes input from the Keyboard.


Encapsulation
The wrapping of code and data into a single unit.
It acts as a protective layer by ensuring that access to wrapped data is not possible by any code defined outside the class.
Eg: Acess Specifier, getter, setter.


Polymorphism
It is a process that allows one thing to have many forms.
It is the process that allows classes to respond to messages in different ways.
Eg:
  • Function Overloading: Process having more than one function with the same name but with different numbers or datatype parameters.
  • Function Overriding: Process of redefining any function in a derived class. The redefined function in the derived class should have the same signature (parameter) in a base class.

Inheritance
A process by which one object acquires the property of another object.
Eg:
  • Single Inheritance: From one base class only one derived class is created. Eg. Fan <= Cooler
  • Hierarchical Inheritance: From one base class more than one derived class is created. Eg. Person <= Student, Player
  • Multilevel Inheritance: From one base class one derived class is created and this derived class will act as a base class from one more derived class. Eg. A <= B <= C
  • Multiple Inheritance: A class inherits more than one class. Eg. Parent1, Parent2 <= Child
  • Hybrid Inheritance: It is a combination of multiple and multilevel inheritances.

Abstract Class and Method
An abstract class can be considered a blueprint of another class.
An abstract method is a method that has a declaration but does not have an implementation.


RealWorld Example of OOPs
  • Abstraction: TV Remote - We press a button and the channel changes, we don't know how it internally works.
  • Encapsulation: Car - There are multiple parts like steering, wheels, etc. All are bound together to drive a car.
  • Inheritance: Parent-Child - Child inherits parents properties.
  • Polymorphism: Person - One behaves differently in front of elders and shows different behaviour in front of friends.
Want to check about the Latest Electronic Devices: CompareKarein

super, this, self
super keyword is used to refer to the immediate parent class object.

this keyword stores the reference of the object on which the function was invoked.
this keyword is used to access instance variables when the name of the instance variable is the same as any local or formal variable of the function.

self is a reference to an object


Compiler / Interpreter / ByteCode
A compiler reads the program, checks for syntax errors and creates an executable file that can be executed at any time. There is no need to recompile every time to execute the program.

An interpreter reads the program line by line, checks for errors and executes immediately. Source code has to be interpreted each time to execute the program.

Bytecode is a machine instruction given to JVM.

Java has the advantage of both Compiler and Interpreter.
Python has only an Interpreter.


ClassLoader
It is a subsystem of JVM used to load class files.


PEP-8 (Python Enhancement Proposal)
It is a set of rules used to increase Python code reusability.


Lambda Function
It is used when the function is of one line and has no return type and no iteration problem.


Shallow and Deep Copy
A shallow copy passes a reference to an object.
Deep copy creates new objects and copies.

Post a Comment

0 Comments