OOPS Interview Questions And Answers for Beginners/Experienced professionals from Codingcompiler. These OOPS interview questions were asked in various interviews conducted by top multinational companies across the globe. We hope that these interview questions on Java OOPS will help you in cracking your next job interview. All the best and happy learning.
Oops Interview Questions and Answers
- What is Oops?
- What is the use of OOPs?
- What is class and object in OOPs?
- What is polymorphism in OOPs?
- What are the main features of OOPs?
- What is data hiding in OOPs?
- Why do we need OOPs?
- What is an object?
- What is a class?
- What is Encapsulation?
OOPS Interview Questions and Answers
1) What is Oops?
Oops: Object-oriented programming system is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields, and code, in the form of procedures. A feature of objects is an object’s procedures that can access and often modify the data fields of the object with which they are associated.
2) What is the use of OOPs?
OOP provides a clear modular structure for programs which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface. OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
3) What is class and object in OOPs?
In object-oriented programming , a class is a template definition of the method s and variable s in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables. … A class can have subclasses that can inherit all or some of the characteristics of the class.
4) What is polymorphism in OOPs?
Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. A language that features polymorphism allows developers to program in the general rather than program in the specific.
5) What are the main features of OOPs?
- Inheritance
- Encapsulation
- Polymorphism
- Data Abstraction
6) What is data hiding in OOPs?
Data hiding is a software development technique specifically used in object-oriented programming (OOP) to hide internal object details (data members). Data hiding ensures exclusive data access to class members and protects object integrity by preventing unintended or intended changes.
7) Why do we need OOPs?
It helps to reduce the complexity and also improves the maintainability of the system. When combined with the concepts of the Encapsulation and Polymorphism, Abstraction gives more power to the Object oriented programming languages.
8) 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.
9) 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.
10) What is Encapsulation?
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of bundling data and methods that work on that data within one unit, e.g., a class in Java.
This concept is also often used to hide the internal representation, or state, of an object from the outside. This is called information hiding. The general idea of this mechanism is simple. If you have an attribute that is not visible from the outside of an object, and bundle it with methods that provide read or write access to it, then you can hide specific information and control access to the internal state of the object.
(Or)
Encapsulation is referred to one of the following two notions.
1) Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are bundled together.
Java OOPS Interview Questions And Answers
11) 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
12) Why is OOP better than procedural?
Procedural programming does not have any proper way for hiding data so it is less secure. Object oriented programming provides data hiding so it is more secure. In procedural programming, function is more important than data. In object oriented programming, data is more important than function.
13) What is the difference between class and structure?
Class: A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit.
- Classes are of reference types.
- All the reference types are allocated on heap memory.
- Allocation of large reference type is cheaper than allocation of large value type.
- Class is generally used in large programs.
- Classes can contain constructor or destructor.
- Classes used new keyword for creating instances.
- A Class can inherit from another class.
- The data member of a class can be protected.
- Function member of the class can be virtual or abstract.
- Two variable of class can contain the reference of the same object and any operation on one variable can affect another variable.
Structure: A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.
- Structs are of value types.
- All the value types are allocated on stack memory
- Allocation and de-allocation is cheaper in value type as compare to reference type.
- Struct are used in small programs.
- Structure does not contain constructor or destructor.
- Struct can create an instance, without new keyword.
- A Struct is not allowed to inherit from another struct or class.
- The data member of struct can’t be protected.
- Function member of the struct cannot be virtual or abstract.
- Each variable in struct contains its own copy of data(except in ref and out parameter variable) and any operation on one variable can not effect another variable.
14) What is the difference between classes and objects?
Classes | Objects |
A class is a blueprint from which you can create the instance, i.e., objects. | An object is the instance of the class, which helps programmers to use variables and methods from inside the class. |
A class is used to bind data as well as methods together as a single unit. | object acts as a variable of the class. |
Classes have logical existence. | Objects have a physical existence |
A class doesn’t take any memory spaces when a programmer creates one. | An object takes memory when a programmer creates one. |
The class has to be declared only once. | Objects can be declared several times depending on the requirement |
15) Can struct inherit?
There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.
16) How can struct be instantiated?
When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
17) Can a struct be null?
A struct can implement interfaces. A struct cannot be null , and a struct variable cannot be assigned null unless the variable is declared as a nullable value type.
18) Can struct have inheritance?
There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.
19) Can struct have Parameterless constructor?
Structs cannot contain explicit parameterless constructors. Struct members are automatically initialized to their default values. A default (parameter-less) constructor for a struct could set different values than the all-zeroed state which would be unexpected behavior.
20) 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
21) 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.
22) What are the different types of inheritance?
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
23) What is hybrid inheritance?
Hybrid inheritance is a combination of multiple and multi-level inheritance.
24) What is multiple inheritance?
A child class inheriting states and behaviors from multiple parent classes is known as multiple inheritance.
25) What is the diamond problem in inheritance?
In case of multiple inheritance, suppose class A has two subclasses B and C, and a class D has two super classes B and C.If a method present in A is overridden by both B and C but not by D then from which class D will inherit that method B or C? This problem is known as diamond problem.
Frequently Asked OOPS Interview Questions And Answers
26) Why Java does not support multiple inheritance?
Java was designed to be a simple language and multiple inheritance introduces complexities like diamond problem. Inheriting states or behaviors from two different type of classes is a case which in reality very rare and it can be achieved easily through an object association.
27) 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.
28) What is subclass?
A Java subclass is a class which inherits a method or methods from a Java superclass. A Java class may be either a subclass, a superclass, both, or neither! The Cat class in the following example is the subclass and the Animal class is the superclass.
29) Which class Cannot be a subclass in Java?
3 Answers. Even Superclass isn’t really needed here, as any class other than java.lang.Object is a subclass – either of java.lang.Object or of some (potentially indirect) subclass of java.lang.Object . A parent class can be a sub class of other class. B can inherit A and C can inherit B.
30) Can we override static method?
Answer is, No, you can not override static method in Java, though you can declare method with same signature in sub class. … As per Java coding convention, static methods should be accessed by class name rather than object. In short Static method can be overloaded, but can not be overridden in Java.
31) What is super () in Java?
super and this keywords in Java. super keyword is used to access methods of the parent class while this is used to access methods of the current class. This is used to refer current-class’s instance as well as static members.
32) How many types of inheritance are there in Java?
In Java there are three types of inheritance available.
On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only.
33) Can constructor be static?
Constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor. Java does not permit to declare a constructor as static. A constructor always belongs to some object. If a constructor is static, an object of subclass cannot access.
34) 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.
35) What is dynamic polymorphism?
Dynamic (run time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not understand which method is called at compilation time. Only JVM decides which method is called at run-time. Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.
36) What is Abstraction?
The first thing with which one is confronted when writing programs is the problem. Typically we are confronted with “real-life” problems and we want to make life easier by providing a program for the problem. However, real-life problems are nebulous and the first thing we have to do is to try to understand the problem to separate necessary from unnecessary details: We try to obtain our own abstract view, or model, of the problem. This process of modeling is called abstraction.
37) What is Static Binding?
Static Binding: The binding which can be resolved at compile time by compiler is known as static or early binding. Binding of all the static, private and final methods is done at compile-time.
38) What is dynamic binding in Java?
Dynamic binding is slower than static binding because it occurs in runtime and spends some time to find out actual method to be called. That’s all on difference between static and dynamic binding in java. bottom line is static binding is a compile time operation while dynamic binding is a runtime.
39) What is Aggregation?
Aggregation is also known as “HAS-A” relationship. When class Car has a member reference variable of type Wheel then the relationship between the classes Car and Wheel is known as Aggregation. Aggregation can be understood as “whole to its parts” relationship.
Car is the whole and Wheel is part. Wheel can exist without the Car. Aggregation is a weak association.
40) What is Association?
Association is a relationship between two objects with multiplicity.
Top Java OOPS Interview Questions and Answers
41) What is Composition?
Composition is a special form of Aggregation where the part cannot exist without the whole. Composition is a strong Association. Composition relationship is represented like aggregation with one difference that the diamond shape is filled.
42) What is a constructor in java?
Constructor is a method used to initialize the state of an object, and it gets invoked at the time of object creation. Rules forconstructor are:
Constructor Name should be same asclass name.
Constructor must have no return type.
43) What are manipulators?
Manipulators are the functions which can be used in conjunction with the insertion (<<) and extraction (>>) operators on an object. Examples are endl and setw.
44) Define Destructor?
Destructor is a method which is automatically called when the object ismade ofscope or destroyed. Destructor name is also same asclass name but with the tilde symbol before the name.
45) What is operator overloading?
Operator overloading is a function where different operators are applied and depends on the arguments. Operator,-,* can be used to pass through the function , and it has their own precedence to execute.
46) Can you explain what Access Modifiers are?
Access modifiers are used to figure out the scope of the method or variables accessible from other various objects or classes.
Access modifiers can be of five types:
• Private
• Public
• Protected
• Friend
• Protected Friend
47) What is an Abstract class in OOP?
An abstract class is used to define at least one abstract method but an object cannot be created from it. Classes created using abstract classes are called derived classes. An abstract class will not contain implementation code in its base class. If an abstract class is forced to instantiate an object out of it, a compilation error will be thrown.
48) What is exception handling in OOP?
Answer:
This is the frequently asked OOP Interview Questions which is asked in an interview. Exception handling is the feature available in most of the object-oriented programming languages which are defined as the process of handling the exceptions during the execution of program flow. The flow of execution should be altered based on the outcome of the exception of aroused. The general blocks of exception handling include try, catch and throw for the most of programming languages like C++, Java etc. In try block the code needs to be executed will be placed and catch block will handle the exception and throw block will return the type of exception and error if it can’t be handled. This is the safest way of handling applications to safeguard the flow of the working application.
49) What is early and late binding?
The compiler performs a process where it matches the function call with the function definition. This is termed as Binding. It can take place during compile time or at runtime. Early binding is also called as Static binding. It means assigning the value to a variable during compile time. Late binding is also called as Dynamic binding; the values to the variables are assigned during runtime.
50) What is a virtual function?
A virtual function is a member function declared within a base class which can be overridden by the derived class. You can call a virtual function for a derived class object when you refer to it using a pointer or a reference to the base class and then execute the function version of the derived class.
The virtual function makes certain the correct function is called for an object irrespective of the kind of pointer/reference used for the function call. The keyword virtual can be used to implement this function.
Advanced Java OOPS Interview Questions and Answers For Experienced
51) What is a friend function?
A friend function is a friend of a class that is given access to that class’s public, private and protected data. It cannot access the data if it is defined outside that class. It can be defined anywhere in the class declaration and access control keywords like public, private or protected cannot affect it.
52) What is a ternary operator?
In several programming languages, the ternary operator is also called a conditional operator. It is an operator which takes three arguments: the first argument is a comparison argument, the second argument is the result of a true comparison and the last argument is the result of a false comparison. It is like a short way of writing an if-else statement.
53) What is a token?
Token, in programming, is a basic component of a code that is recognized by the compiler. Eg. Identifiers, String Literals, Constants & Operators.
55) What are access modifiers?
Access modifiers are keywords that determine the accessibility of the methods, classes and other members.
56) List out the 5 access modifiers
The 5 types of access modifiers are
- Private
- Protected
- Public
- Friend &
- Protected friend.
RELATED INTERVIEW QUESTIONS
- Core Java Interview Questions
- JSF Interview Questions
- JSP Interview Questions
- JPA Interview Questions
- Spring Framework Interview Questions
- Spring Boot Interview Questions
- Core Java Multiple Choice Questions
- 60 Java MCQ Questions And Answers
- Aricent Java Interview Questions
- Accenture Java Interview Questions
- Advanced Java Interview Questions For 5 8 10 Years Experienced
- Core Java Interview Questions For Experienced
- GIT Interview Questions And Answers
- Network Security Interview Questions
- CheckPoint Interview Questions
- Page Object Model Interview Questions
- Apache Pig Interview Questions
- Python Interview Questions And Answers
- Peoplesoft Integration Broker Interview Questions
- PeopleSoft Application Engine Interview Questions
- RSA enVision Interview Questions
- RSA SecurID Interview Questions
- Archer GRC Interview Questions
- RSA Archer Interview Questions
- Blockchain Interview Questions
- Commvault Interview Questions
- Peoplesoft Admin Interview Questions
- ZooKeeper Interview Questions
- Apache Kafka Interview Questions
- Couchbase Interview Questions