OOPs Interview Questions (2022) – javatpoint

OOPs Interview Questions

Object-oriented programming ( OOPs ) is a programming paradigm that is based on the concept of objects rather than precisely functions and procedures. It is the most democratic methodology among developers .
Nowadays technical school giants demanding and hiring who has expertness in object-oriented approaches and patterns and conducting interviews for the same. The advantage of hiring such candidates is that they can besides learn other OOP languages easily as per organization requirements. Since, going through the department, you can increase your casual to get hire by companies if you have well prepared for OOPs interview questions .
In this segment, we have collected some normally asked OOPs interview questions for both fresher and experienced. It can help you to crack the interview to get your dream job .
OOPs Interview Questions

1) What do you understand by OOP?

OOP stands for object-oriented program. It is a programming paradigm that revolves around the object rather than function and procedure. In other words, it is an overture for developing applications that emphasize on objects. An object is a real word entity that contains data and code. It allows binding data and code together.

2) Name any seven widely used OOP languages.

There are diverse OOP languages but the most widely used are :

  • Python
  • Java
  • Go
  • Dart
  • C++
  • C#
  • Ruby

3) What is the purpose of using OOPs concepts?

The aim of OOP is to implement real-world entities like inheritance, hide, polymorphism in programming. The main determination of OOP is to bind together the data and the functions that operate on them then that no other part of the code can access this data except that function .

4) What are the four main features of OOPs?

The OOP has the following four features :

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Data Abstraction

OOPs Interview Questions

5) Why OOP is so popular?

OOPs, programming prototype is considered as a better style of programming. not only it helps in writing a building complex patch of code well, but it besides allows users to handle and maintain them easily deoxyadenosine monophosphate well. not only that, the independent pillar of OOPs – Data Abstraction, Encapsulation, Inheritance, and Polymorphism, makes it easy for programmers to solve complex scenarios. As a leave of these, OOPs is then democratic .

6) What are the advantages and disadvantages of OOP?

Advantages of OOP

  • It follows a bottom-up approach.
  • It models the real word well.
  • It allows us the reusability of code.
  • Avoids unnecessary data exposure to the user by using the abstraction.
  • OOP forces the designers to have a long and extensive design phase that results in better design and fewer flaws.
  • Decompose a complex problem into smaller chunks.
  • Programmer are able to reach their goals faster.
  • Minimizes the complexity.
  • Easy redesign and extension of code that does not affect the other functionality.

Disadvantages of OOP

  • Proper planning is required.
  • Program design is tricky.
  • Programmer should be well skilled.
  • Classes tend to be overly generalized.

7) What are the limitations of OOPs?

  • Requires intensive testing processes.
  • Solving problems takes more time as compared to Procedure Oriented Programming.
  • The size of the programs created using this approach may become larger than the programs written using the procedure-oriented programming approach.
  • Software developed using this approach requires a substantial amount of pre-work and planning.
  • OOP code is difficult to understand if you do not have the corresponding class documentation.
  • In certain scenarios, these programs can consume a large amount of memory.
  • Not suitable for small problems.
  • Takes more time to solve problems.

8) What are the differences between object-oriented programming and structural programming?

Object-oriented Programming Structural Programming
It follows a bottom-up approach. It follows a top-down approach.
It provides data hiding. Data hiding is not allowed.
It is used to solve complex problems. It is used to solve moderate problems.
It allows reusability of code that reduces redundancy of code. Reusability of code is not allowed.
It is based on objects rather than functions and procedures. It provides a logical structure to a program in which the program is divided into functions.
It provides more security as it has a data hiding feature. It provides less security as it does not support the data hiding feature.
More abstraction more flexibility. Less abstraction less flexibility.
It focuses on data. It focuses on the process or logical structure.

9) What do you understand by pure object-oriented language? Why Java is not a pure object-oriented programming language?

The scheduling lyric is called pure object-oriented language that treats everything inside the broadcast as an object. The crude types are not supported by the arrant OOPs speech. There are some early features that must satisfy by a pure object-oriented lyric :

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • All predefined types are objects
  • All user-defined types are objects
  • All operations performed on objects must be only through methods exposed to the objects.

Java is not a pure object-oriented programming language because pre-defined data types in Java are not treated as objects. Hence, it is not an object-oriented linguistic process .

10) What do you understand by class and object? Also, give example.

Class: A class is a blueprint or template of an object. It is a user-defined data type. Inside a course, we define variables, constants, member functions, and other functionality. It does not consume memory at run time. Note that classes are not considered as a datum structure. It is a coherent entity. It is the best exemplar of data oblige .
Object: An object is a real-world entity that has attributes, demeanor, and properties. It is referred to as an exemplify of the classify. It contains penis functions, variables that we have defined in the class. It occupies space in the memory. different objects have different states or attributes, and behaviors .
The follow figure best illustrates the class and object .
OOPs Interview Questions

11) What are the differences between class and object?

Class Object
It is a logical entity. It is a real-world entity.
It is conceptual. It is real.
It binds data and methods together into a single unit. It is just like a variable of a class.
It does not occupy space in the memory. It occupies space in the memory.
It is a data type that represents the blueprint of an object. It is an instance of the class.
It is declared once. Multiple objects can be declared as and when required.
It uses the keyword class when declared. It uses the new keyword to create an object.
A class can exist without any object. Objects cannot exist without a class.

12) What are the key differences between class and structure?

Class Structure
Class is a group of common objects that shares common properties. The structure is a collection of different data types.
It deals with data members and member functions. It deals with data members only.
It supports inheritance. It does not support inheritance.
Member variables cannot be initialized directly. Member variables can be initialized directly.
It is of type reference. It is of a type value.
It’s members are private by default. It’s members are public by default.
The keyword class defines a class. The keyword struct defines a structure.
An instance of a class is an object. An instance of a structure is a structure variable.
Useful while dealing with the complex data structure. Useful while dealing with the small data structure.

13) What is the concept of access specifiers when should we use these?

In OOPs speech, access specifiers are reserved keyword that is used to set the handiness of the classes, methods and early members of the class. It is besides known as access modifiers. It includes public, private, and protected. There is some other access specifier that is language-specific. such as Java has another access specifier default. These access specifiers play a vital function in achieving one of the major functions of OOP, i.e. encapsulation. The following mesa depicts the approachability .
OOPs Interview Questions

14) What are the manipulators in OOP and how it works?

Manipulators are helping functions. It is used to manipulate or modify the input or output pour. The alteration is possible by using the insertion ( < < ) and extraction ( > > ) operators. note that the alteration of input or output stream does not mean to change the values of variables. There are two types of manipulators with arguments or without arguments.
The case of manipulators that do not have arguments is endl, ws, flush, etc. Manipulators with arguments are setw(val), setfill(c), setbase(val), setiosflags(flag). Some other manipulators are showpos, fixed, scientific, hex, dec, oct, etc .

15) What are the rules for creating a constructor?

  • It cannot have a return type.
  • It must have the same name as the Class name.
  • It cannot be marked as static.
  • It cannot be marked as abstract.
  • It cannot be overridden.
  • It cannot be final.

16) What are the differences between the constructor and the method in Java?

Constructor Method
Constructor has the same name as the class name. The method name and class name are not the same.
It is a special type of method that is used to initialize an object of its class. It is a set of instructions that can be invoked at any point in a program.
It creates an instance of a class. It is used to execute Java code.
It is invoked implicitly when we create an object of the class. It gets executed when we explicitly called it.
It cannot be inherited by the subclass. It can be inherited by the subclass.
It does not have any return type. It must have a return type.
It cannot be overridden in Java. It can be overridden in Java.
It cannot be declared as static. It can be declared as static.
Java compiler automatically provides a default constructor. Java compiler does not provide any method by default.

17) How does procedural programming be different from OOP differ?

Procedural Oriented Programming Object-Oriented Programming
It is based on functions. It is based on real-world objects.
It follows a top-down approach. It follows a bottom-up approach.
It is less secure because there is no proper way to hide data. It provides more security.
Data is visible to the whole program. It encapsulates the data.
Reuse of code is not allowed. The code can be reused.
Modification and extension of code are not easy. We can easily modify and extend code.
Examples of POP are C, VB, FORTRAN, Pascal, etc. Examples of OOPs are C++, Java, C#, .NET, etc.

18) What are the differences between error and exception?

Basis of Comparison Exception Error
Recoverable/ Irrecoverable Exception can be recovered by using the try-catch block. An error cannot be recovered.
Type It can be classified into two categories i.e. checked and unchecked. All errors in Java are unchecked.
Occurrence It occurs at compile time or run time. It occurs at run time.
Package It belongs to java.lang.Exception package. It belongs to java.lang.Error package.
Known or unknown Only checked exceptions are known to the compiler. Errors will not be known to the compiler.
Causes It is mainly caused by the application itself. It is mostly caused by the environment in which the application is running.
Example Checked Exceptions: SQLException, IOException
Unchecked Exceptions: ArrayIndexOutOfBoundException, NullPointerException, ArithmaticException
Java.lang.StackOverFlow, java.lang.OutOfMemoryError

19) What are the characteristics of an abstract class?

An abstract class is a course that is declared as abstract. It can not be instantiated and is constantly used as a basal classify. The characteristics of an abstract class are as follows :

  • Instantiation of an abstract class is not allowed. It must be inherited.
  • An abstract class can have both abstract and non-abstract methods.
  • An abstract class must have at least one abstract method.
  • You must declare at least one abstract method in the abstract class.
  • It is always public.
  • It is declared using the abstract

The function of an abstract class is to provide a common definition of the base class that multiple derive classes can share .

20) Is it possible for a class to inherit the constructor of its base class?

No, a class can not inherit the builder of its base class .

21) Identify which OOPs concept should be used in the following scenario?

A group of 5 friends, one boy never gives any contribution when the group goes for the outing. Suddenly a beautiful girl joins the same group. The boy who never contributes is now spending a lot of money for the group.
Runtime polymorphism

22) What is composition?

constitution is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in case variables. It allows us to model a has-a association between objects. We can find such relationships in the real global. For example, a car has an locomotive. the surveil figure depicts the like
OOPs Interview Questions
The chief benefits of composition are :

  • Reuse existing code
  • Design clean APIs
  • Change the implementation of a class used in a composition without adapting any external clients.

23) What are the differences between copy constructor and assignment operator?

The copy builder and the assignment operator ( = ) both are used to initialize one object using another object. The main dispute between the two is that the imitate builder allocates separate memory to both objects i.e. existing object and newly created aim while the assignment operator does not allocate newly memory for the newly created aim. It uses the reference variable star that points to the former memory parry ( where an old aim is located ) .
Syntax of Copy Constructor

Syntax of Assignment Operator

Copy Constructor Assignment Operator
It is an overloaded constructor. It is an operator.
It creates a new object as a copy of an existing object. It assigns the value of one object to another object both of which already exist.
The copy constructor is used when a new object is created with some existing object. It is used when we want to assign an existing object to a new object.
Both the objects use separate memory locations. Both objects share the same memory but use the two different reference variables that point to the same location.
If no copy constructor is defined in the class, the compiler provides one. If the assignment operator is not overloaded then the bitwise copy will be made.

24) What is the difference between Composition and Inheritance?

inheritance means an object inherit reclaimable properties of the base class. Compositions mean that an object holds early objects. In Inheritance, there is only one object in memory ( derived object ) whereas, in Composition, the rear object holds references of all compose objects. From a design position, inheritance is “ is a ” kinship among objects whereas Composition is “ has a ” kinship among objects .
OOPs Interview Questions

25) What is constructor chaining?

In OOPs, builder chain is a sequence of invoking constructors ( of the same class ) upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an example. In other words, if a class has more than one builder ( overloaded ) and one of them tries to invoke another builder, this serve is known as builder chain. In C++, it is known as builder deputation and it is deliver from C++ 11 .
OOPs Interview Questions

26) What are the limitations of inheritance?

  • The main disadvantage of using inheritance is two classes get tightly coupled. That means one cannot be used independently of the other. If a method or aggregate is deleted in the Super Class, we have to refactor using that method in SubClass.
  • Inherited functions work slower compared to normal functions.
  • Need careful implementation otherwise leads to improper solutions.

27) What are the differences between Inheritance and Polymorphism?

Inheritance Polymorphism
Inheritance is one in which a derived class inherits the already existing class’s features. Polymorphism is one that you can define in different forms.
It refers to using the structure and behavior of a superclass in a subclass. It refers to changing the behavior of a superclass in the subclass.
It is required in order to achieve polymorphism. In order to achieve polymorphism, inherence is not required.
It is applied to classes. It is applied to functions and methods.
It can be single, hybrid, multiple, hierarchical, multipath, and multilevel inheritance. There are two types of polymorphism compile time and run time.
It supports code reusability and reduces lines of code. It allows the object to decide which form of the function to be invoked at run-time (overriding) and compile-time (overloading).

28) What is Coupling in OOP and why it is helpful?

In program, interval of concerns is known as coupling. It means that an object can not directly change or modify the state of matter or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose pair, and tight coupling .
Objects that are autonomous of one another and do not directly modify the state of other objects is called loosely coupled. free coupling makes the code more flexible, mutable, and easier to work with .
Objects that depend on other objects and can modify the states of early objects are called tightly coupled. It creates conditions where modifying the code of one object besides requires changing the code of other objects. The recycle of code is difficult in rigorous pair because we can not separate the code .
Since using loose coupling is always a full habit .

29) Name the operators that cannot be overload.

  1. Scope Resolution Operator (::)
  2. Ternary Operator (? 🙂
  3. Member Access or Dot Operator (.)
  4. Pointer to Member Operator (.*)
  5. sizeof operator

30) What is the difference between new and override?

The raw modifier instructs the compiler to use the new implementation alternatively of the free-base course function. Whereas, Override modifier helps to override the base class function .
virtual: indicates that a method acting may be overridden by an heir
override: Overrides the functionality of a virtual method in a base class, providing different functionality.

new: Hides the original method ( which does n’t have to be virtual ), providing different functionality. This should entirely be used where it is absolutely necessity .
When you hide a method acting, you can placid access the master method acting by upcasting to the base class. This is utilitarian in some scenarios, but dangerous .

31) Explain overloading and overriding with example?

Overloading
Overloading is a concept in OOP when two or more methods in a class with the like diagnose but the method touch is unlike. It is besides known as compile-time polymorphism. For model, in the follow code snip, the method add() is an clog method .

OOPs Interview Questions
Overriding
If a method acting with the like method signature is presented in both child and parent class is known as method overriding. The methods must have the lapp count of parameters and the same character of parameter. It overrides the value of the rear class method acting. It is besides known as runtime polymorphism. For model, consider the surveil program .

32) What is Cohesion in OOP?

In OOP, cohesion refers to the degree to which the elements inside a module belong together. It measures the military capability of the kinship between the module and datum. In short, coherence represents the clearness of the responsibilities of a faculty. It is much contrasted with match .
It focuses on a how single faculty or class is intended. Higher the cohesiveness of the faculty or class, better is the object-oriented design .
OOPs Interview Questions
There are two types of cohesion, i.e. High and Low .

  • High cohesion is associated with several required qualities of software including robustness, reliability, and understandability.
  • Low cohesion is associated with unwanted qualities such as being difficult to maintain, test, reuse, or even understand.

high cohesion much associates with free coupling and vice versa .

33) Give a real-world example of polymorphism?

The general think of of Polymorphism is one that has different forms. The best real-world example of polymorphism is a person that plays different roles at different palaces or situations .

  • At home a person can play the role of father, husband, and son.
  • At the office the same person plays the role of boss or employee.
  • In public transport, he plays the role of passenger.
  • In the hospital, he can play the role of doctor or patient.
  • At the shop, he plays the role of customer.

OOPs Interview Questions
Hence, the same person possesses unlike behavior in different situations. It is called polymorphism .

34) What is the difference between a base class and a superclass?

The base class is the solution class- the most popularize classify. At the like time, the superclass is the immediate parent class from which the other class inherits .

35) What is data abstraction and how can we achieve data abstraction?

It is one of the most important features of OOP. It allows us to show alone essential data or information to the user and hides the implementation details from the user. A real-world example of abstractedness is driving a car. When we drive a car, we do not need to know how the engine works ( execution ) we only know how ECG works .
There are two ways to achieve data abstractedness

  • Abstract class
  • Abstract method

36) What are the levels of data abstraction?

There are three levels of data abstraction :

  • Physical Level: It is the lowest level of data abstraction. It shows how the data is actually stored in memory.
  • Logical Level: It includes the information that is actually stored in the database in the form of tables. It also stores the relationship among the data entities in relatively simple structures. At this level, the information available to the user at the view level is unknown.
  • View Level: It is the highest level of data abstraction. The actual database is visible to the user. It exists to ease the availability of the database by an individual user.
  • OOPs Interview Questions

37) What are the types of variables in OOP?

There are three types of variables :
OOPs Interview Questions
Instance Variable: It is an object-level variable. It should be declared inside a class but must be outside a method, freeze, and builder. It is created when an object is created by using the new keyword. It can be accessed directly by calling the variable identify inside the class .
Static Variable: It is a class-level variable. It is declared with keyword static inside a class but must be outside of the method, block, and builder. It stores in inactive memory. Its visibility is the lapp as the exemplify variable star. The default option value of a static variable is the like as the case variable. It can be accessed by calling the class_name.variable_name .
Local Variable: It is a method-level variable. It can be declared in method, builder, or stuff. note that the use of an access modifier is not allowed with local variables. It is visible only to the method, freeze, and builder in which it is declared. internally, it is implemented at the stack charge. It must be declared and initialized before use .
Another type of variable is used in object-oriented program is the reference variable .
Reference Variable: It is a variable that points to an aim of the class. It points to the placement of the object that is stored in the memory .

38) Is it possible to overload a constructor?

Yes, the constructors can be overloaded by changing the number of arguments accepted by the builder or by changing the data type of the parameters. For model :

39) Can we overload the main() method in Java also give an example?

Yes, we can besides overload the main ( ) method in Java. Any number of main ( ) methods can be defined in the classify, but the method signature must be different. Consider the postdate code .

40) Consider the following scenario:

If a class Demo has a static block and a main() method. A print statement is presented in both. The question is which one will first execute, static block or the main() method, and why?
JVM first executes the static block on a precedence footing. It means JVM first goes to static block even before it looks for the main ( ) method in the program. After that main ( ) method will be executed .

Interview Tips Job/HR Interview Questions
Company Interview Questions & Procedure JavaScript Interview Questions
Java Basics Interview Questions Java OOPs Interview Questions
Servlet Interview Questions JSP Interview Questions
Spring Interview Questions Hibernate Interview Questions
PL/SQL Interview Questions SQL Interview Questions

Oracle Interview Questions Android Interview Questions
jQuery Interview Questions MySQL Interview Questions
reference : https://gauday.com
Category : interview

We will be happy to hear your thoughts

Leave a reply

GauDay Crypto news and market tracking in real time
Logo
Enable registration in settings - general