Oopj Int Ques
Oopj Int Ques
JVM (Java Virtual Machine): The JVM is an engine that provides a runtime environment to drive the
Java Code or applications. It converts Java bytecode into machine language.
JRE (Java Runtime Environment): JRE is a part of software that is designed to run other software. It
contains the set of libraries + other files that JVM uses at runtime.
JDK (Java Development Kit): The JDK is a software development environment used for developing
Java applications and applets. It physically exists. It contains JRE + development tools.
A constructor in Java is a block of code similar to a method that's called when an instance of an object
is created. Unlike methods, constructors have no explicit return type and have the same name as the
class itself.
Answer: The super keyword in Java is a reference variable that is used to refer to parent class objects.
The keyword can be used to call superclass methods and to access the superclass constructor.
Answer:
final: A keyword used to declare constants, prevent method overriding, and inheritance (e.g., final
int x = 10;).
finally: A block used in exception handling to execute important code (like clean up), always runs
whether or not an exception occurs.
finalize(): A method in the Object class, called by the garbage collector before object destruction
(now deprecated in Java 9+).
7. What is a thread?
Answer: A thread is a thread of execution in a program. The Java Virtual Machine allows an
application to have multiple threads of execution running concurrently.
New: The thread is in a new state if you create an instance of the Thread class but before the
invocation of the start() method.
Runnable: The thread is ready to run, but it's not running.
Blocked: The thread is waiting for a monitor lock.
Waiting: The thread is waiting indefinitely for another thread to perform a particular action.
Timed Waiting: The thread is waiting for another thread to perform a specific action for up to a
specified waiting time.
Terminated: A thread that has exited is in this state.
Ans: OOPs stands for Object-Oriented Programming Systems. It’s a type of programming that is based
on objects and classes rather than functions and stored procedures.
Object is an instance of a class consisting of properties that makes data useful whereas
classes are a representation of an object describing its details and used to organize the
data
Ans: There are various reasons for defining the need for OOPs, but the important ones are:-
OOPs provide the feature of data hiding that is good for security concern.
With the help of encapsulation data and code can be bound together thus increasing
security.
The concept of Polymorphism gives flexibility to the program by allowing the entities to
have multiple forms.
Inheritance — Inheritance is the concept in which one class inherits the property of the
other class.
Encapsulation — It wraps up the data and the code working together in a single unit.
Polymorphism — It’s the ability to exist in multiple forms. It lets users perform a single
task in various ways.
Data Abstraction — It helps hide the important information from the users and shows
only necessary details.
OOPs are based on objects and classes rather than functions and stored procedures where
as Structural Programming is divided into functions and provides a logical structure to the
program.
OOP with Java Interview questions
Ans: A class in Java is a blueprint from which objects are created. It defines the structure and behavior
of objects that belong to the same type. Classes consist of fields (variables) and methods (functions).
Fields represent the state or attributes of the object, while methods represent the actions that the object
can perform.
Syntax:-
class < class_name > {
field;
method;
}
Example of a simple class:
// Constructor
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
Note: The class name should begin with the initial letter capitalized by convention.
Ans: An object is an instance of a class, created using the blueprint provided by the class. Objects have
their own state (attributes) and behavior (methods), which are defined in the class. You can create
multiple objects from the same class, each having its own set of data.
Basically in program we don’t really want to know what exactly a function is doing, so
we can achieve this with the help of abstraction.
So we can give the user just the function that they need to execute and they don’t need to
worry about how it is implemented and which are the different packages which are the
different classes that are used inside of the specific function, with the help of this we can
achieve abstraction.
Eg: car, so in case of car only relevant parts are shown like break, clutch, steering, horn etc. because
they are necessary for driving. But the driver, he don’t need to know how there are functioning
internally.
Thus showing relevant data to the user and hiding implementation or details from the user is called
abstraction.
Advantage:
Ans: There are 2 ways for achieving abstraction. We can achieve abstraction using abstract class or We
can achieve abstraction using interfaces.
Abstract keyword is used to declare abstract class, Interface keyword is used to declare
interface class.
Abstract class does not supports multiple inheritance, Interface supports multiple
inheritance.
Abstract class consists of both abstract and non abstract methods, Interface consists of
both static, default and abstract methods.
A method declared without a body (no implementation) within an abstract class is know
as abstract method.
If a class is using an abstract method, the class must be declared abstract. But the opposite
cannot be true which means abstract class doesn’t necessarily have an abstract method
OOP with Java Interview questions
(which means abstract class may contain abstract method or it may contain concrete
method).
Ans: The process of grouping/binding data members(variables) and corresponding methods into a
single unit is called Encapsulation. If any component follows data hiding and abstraction, then the
component is said to be encapsulated. The encapsulation principle says hiding the data behind a
method. Every Java class is an example of encapsulation.
i) Data Hiding: An outside person can not access out internal data directly, this concept of OOPS is
called data hiding. After successful validation/authentication, an outside person can access our internal
data. The main advantage is Security.
ii) Abstraction: Hiding Internal implementation, just highlighting the set of services that we are going
to offer is the concept of abstraction. By using the interface and abstract classes we can implement
abstraction.
Disadvantage of Encapsulation: It increases the line of code due to getter, setter method, validation
logic etc, and hence it becomes time-consuming, performance becomes down.
Abstraction is used to hide the unwanted information or hiding the implementation from
the user, Encapsulation is used hide the data in a single entity along with a method to
protect information from outside.
We can achieve the abstraction or we can hide the implementation by using abstract
classes and interfaces, While in Encapsulation, the data is hidden by making the variable
as private and by providing the getters setters method.
OOP with Java Interview questions
Ans: Inheritance is also known as the IS-A relationship, says all the methods and variables available
in the parent class are available in child class but not vice-versa. By using extends keyword we can
implement inheritance. Example: In Java, Object Class is the parent class of all other classes.
2. Multi-level Inheritance: Single child class extend another child class which extends a
parent class.
5. Multiple Inheritance: Single child class extends multiple parent class. This is not
supported by Java because if two class contains two methods with the same name, then
when the child class extends both parent class following multiple in-inheritance an
ambiguity arises which class method to pick. Hence, Java doesn’t support multiple
inheritances and hybrid inheritance in class. This problem is also called as diamond
access problem. However, in the case of interface, multiple inheritance is supported in
java because in the case of inheritance each class just contains the declaration and the
corresponding implemented class only implements the method, hence there is no chance
of ambiguity.
Advantage of Inheritance: The main advantage is code re-usability. Always write the common
methods for all other child classes in the parent class. Inside child class only write those methods which
are specific to that child class.
OOP with Java Interview questions
Limitations of Inheritance: The child and parent class are linked tightly. In case any change in the
program is required we need to change parent and child classes. It increases execution time and
implementation time.
Static Polymorphism occurs at the compile-time and is achieved through method overloading. i.e.
Compiler decides what value has to be taken by the body in the picture. There are certain conditions for
static polymorphism.
Run time Polymorphism as the name suggests occurs at run time. Method overriding is an example of
dynamic polymorphism. There are certain conditions for static polymorphism.
With the help of pointers and virtual functions, we can achieve method overriding.
When a derived class has the same functions as that of the base class, that base class
method is overridden.
Method Overloading is a concept in which two or more methods can have the same name with
different arguments(signatures). Overloading is related to compile-time polymorphism and can be
achieved in three ways…
Method Overriding is a concept of OOPs. It means if a subclass and superclass have the same
arguments, then the argument of the subclass overrides the argument/method of the superclass. Method
Overriding is related to Run-Time Polymorphism. The method overriding can be done when
An interface defines a contract that classes must follow. It contains only abstract methods (before Java
8) and allows default and static methods (from Java 8). Interfaces support multiple inheritance since a
class can implement multiple interfaces.
An abstract class can have both abstract and concrete methods. It can include constructors and instance
variables. Unlike interfaces, abstract classes can have method implementations but do not support
multiple inheritance.
31. Create an interface and implement it in multiple classes with different behaviors.
interface Animal {
void sound();
OOP with Java Interview questions
a1.sound();
a2.sound();
32. What is the difference between checked and unchecked exceptions in Java?
Answer:
OOP with Java Interview questions
Checked exceptions are exceptions that must be either caught or declared in the
method signature using throws. They are checked at compile-time (e.g.,
IOException, SQLException).
Unchecked exceptions (also called runtime exceptions) are not required to be caught
or declared. They occur at runtime and usually indicate programming errors (e.g.,
NullPointerException, ArithmeticException).
Answer:
Answer:
Yes, a try block can be used without a catch block only if it is followed by a finally block. The finally
block will execute regardless of whether an exception occurs.
try {
// code that may throw exception
} finally {
// cleanup code
}
Answer:
Exception represents conditions that a reasonable application might want to catch and
handle (e.g., IOException, NullPointerException).
Error represents serious problems that are generally outside the control of the
application and are not meant to be caught (e.g., OutOfMemoryError,
StackOverflowError).