0% found this document useful (0 votes)
2 views11 pages

Oopj Int Ques

The document provides a comprehensive list of interview questions and answers related to Object-Oriented Programming (OOP) in Java, covering key concepts such as JDK, JRE, JVM, constructors, inheritance, encapsulation, polymorphism, and exception handling. It explains fundamental OOP principles, including abstraction, encapsulation, inheritance, and polymorphism, along with their advantages and limitations. Additionally, it differentiates between various Java features like interfaces and abstract classes, and discusses access modifiers and exception types.

Uploaded by

vivankapoor600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Oopj Int Ques

The document provides a comprehensive list of interview questions and answers related to Object-Oriented Programming (OOP) in Java, covering key concepts such as JDK, JRE, JVM, constructors, inheritance, encapsulation, polymorphism, and exception handling. It explains fundamental OOP principles, including abstraction, encapsulation, inheritance, and polymorphism, along with their advantages and limitations. Additionally, it differentiates between various Java features like interfaces and abstract classes, and discusses access modifiers and exception types.

Uploaded by

vivankapoor600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

OOP with Java Interview questions

1. Explain the JDK, JRE, and JVM.

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.

2. What is a constructor in Java?

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.

3. What is the "super" keyword?

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.

4. Differentiate between final, finally and finalize?

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+).

5. What are wrapper classes?


Answer: Wrapper classes convert the Java primitives into the reference types (objects). Every
primitive data type has a class dedicated to it. These are known as wrapper classes because they
"wrap" the primitive data type into an object of that class. Examples include Integer, Character,
Double etc.

6. Explain autoboxing and unboxing.


Answer: Autoboxing is the automatic conversion that the Java compiler makes between the
primitive types and their corresponding object wrapper classes. For example, converting an int to
an Integer, a double to a Double, etc. Unboxing is the reverse process, where the object is
converted back to a primitive type.
OOP with Java Interview questions

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.

8. What is the difference between a process and a thread?


Answer:

Process: A program in execution. Independent set of variables, own execution stack.


Thread: Subset of Process. Threads share the same process's variables and execution stack.
Question 44: What is multithreading?
Answer: Multithreading is a Java feature that allows concurrent execution of two or more parts of a
program for maximum utilization of CPU.

9. What is the "synchronized" keyword?


Answer: The synchronized keyword is used to control the access of multiple threads to any shared
resource. It can be used to define a method or a block of code that can only be accessed by one
thread at a time.

10. What are the states of a thread?


Answer: A thread can be in one of the following states:

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.

11. What is OOPs?

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

 OOPs main aim is to implement real-world entities like inheritance, hiding,


polymorphism etc. into programming.

12. Why do we need OOPs?

Ans: There are various reasons for defining the need for OOPs, but the important ones are:-

 OOPs make the development and maintenance of projects easier.


OOP with Java Interview questions

 OOPs provide the feature of data hiding that is good for security concern.

 We can provide the solution to real-world problems if we are using OOPs.

 Code can be reused through inheritance thereby reducing redundancy.

 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.

13. What are the features of OOPs?

Ans: The OOPs comprises of four main features that are:

 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.

14. What are the limitations of OOPs?

 Not suitable for solving small problems.

 Without the proper class documentation, it is hard to understand the code.

 Requires a lot of time to solve problems.

 Required intensive testing.

 The programmer should think of solving a problems in terms of objects.

15. State the differences between OOPs and Structural Programming?

 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

 A OOPs is more secure as data hiding is available where as a Structural Programming


less secure as data hiding is not available.

16. What is class in java?

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:

public class Dog {


// Fields (attributes)
private String name;
private int age;

// 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.

17. What is object in java?

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.

Creating an object (instance) of the Dog class:

Dog myDog = new Dog("Max", 3);

18. What is data abstraction?


OOP with Java Interview questions

 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:

 Avoid code duplication & increase reusability.

 Can change internal implementation of class independently.

19. How do you achieve abstraction in java?

Ans: There are 2 ways for achieving abstraction. We can achieve abstraction using abstract class or We
can achieve abstraction using interfaces.

20. Differentiate between an abstract class and an interface?

 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.

21. What is an Abstract Method?

 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).

22. What is encapsulation in java?

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.

Advantage of Encapsulation: Security, Enhancement becomes easier, maintainability, modularity etc.

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.

23. What are the differences between Abstraction and Encapsulation?

 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

24. What is inheritance in java?

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.

There are 5 types of inheritance concepts available.

1. Single Inheritance: Single child class extends single parent class.

2. Multi-level Inheritance: Single child class extend another child class which extends a
parent class.

3. Hierarchical Inheritance: Multiple child class extends single parent.

4. Hybrid Inheritance: Combination of above 4 inheritance.

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.

25. What is Polymorphism?

Polymorphism is considered one of the important features of Object-Oriented Programming and


polymorphism is nothing but one function is used to perform the multiple tasks.

26. What are the types of polymorphism? Define them?

Ans: There are two different types of Polymorphism

 Static or (Compile Time) Polymorphism

 Dynamic Binding or (Run Time) Polymorphism

Static or (Compile Time) Polymorphism:-

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.

 The parameters numbers must be different from other parameters.

 The type of parameter should be different.

Due to different parameters, each method has a different signature.

Dynamic Binding or (Run-Time) 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.

27. What is method overloading in java?


OOP with Java Interview questions

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…

 Changing the number of parameters.

 Changing the data type of parameters.

 Changing the sequence of the data type of parameters.

28. What is method overriding in Java?

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

 The arguments of both parent and child are the same.

 private, static, and final methods cannot be overridden.

 The abstract method of superclass should always be overridden

29. What is the difference between an interface and an abstract class?

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.

30. What is the role of access modifiers in OOPS?


Access modifiers control visibility of class members:
private – Accessible only within the class.
default – Accessible within the same package.
protected – Accessible in the same package and subclasses.
public – Accessible from anywhere.

31. Create an interface and implement it in multiple classes with different behaviors.
interface Animal {

void sound();
OOP with Java Interview questions

class Dog implements Animal {

public void sound() { System.out.println(“Dog barks”); }

class Cat implements Animal {

public void sound() { System.out.println(“Cat meows”); }

public class Test {

public static void main(String[] args) {

Animal a1 = new Dog();

Animal a2 = new Cat();

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).

33. What is the difference between throw and throws in Java?

Answer:

 throw is used to explicitly throw an exception from a method or block.


 throws is used in a method signature to declare that the method might throw one or
more exceptions, informing the caller to handle them.

34. Can a try block be used without a catch block?

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
}

35. What is the difference between Exception and Error in Java?

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).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy