0% found this document useful (0 votes)
14 views149 pages

Experience 1

The document discusses object-oriented programming (OOP) in Python, highlighting its key concepts such as encapsulation, abstraction, inheritance, and polymorphism. It also provides an overview of Java, including its features, differences from other platforms, and essential interview questions related to Java programming. Additionally, it explains the importance of constructors in Java for object initialization and encapsulation.

Uploaded by

Sandhiya
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)
14 views149 pages

Experience 1

The document discusses object-oriented programming (OOP) in Python, highlighting its key concepts such as encapsulation, abstraction, inheritance, and polymorphism. It also provides an overview of Java, including its features, differences from other platforms, and essential interview questions related to Java programming. Additionally, it explains the importance of constructors in Java for object initialization and encapsulation.

Uploaded by

Sandhiya
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/ 149

Experience 1 :

1. Oops?
In Python object-oriented Programming (OOPs) is a programming
paradigm that uses objects and classes in programming. It aims to
implement real-world entities like inheritance, polymorphisms,
encapsulation, etc. in the programming. The main concept of
object-oriented Programming (OOPs) or oops concepts in Python is
to bind the data and the functions that work together as a single unit
so that no other part of the code can access this data.

2. Why Oops?
The main concept of object-oriented Programming (OOPs) or oops
concepts in Python is to bind the data and the functions that work
together as a single unit so that no other part of the code can access
this data.

3.What are the 4 pillars of OOP Python?


The 4 pillars of object-oriented programming (OOP) in Python
(and generally in programming) are:
 Encapsulation: Bundling data (attributes) and methods (functions)
that operate on the data into a single unit (class).
 Abstraction: Hiding complex implementation details and providing
a simplified interface.
 Inheritance: Allowing a class to inherit attributes and methods
from another class, promoting code reuse.
 Polymorphism: Using a single interface to represent different data
types or objects.
Top 100+ Java Interview Questions And Answers (2025)
Revise all the important Java interview questions with this
exhaustive list and prepare yourself well for your next job
interview.
88 mins read
Developed by James Gosling, Java is without a doubt one of the
most popular programming languages for landing a rewarding
career. Java is used in the development of software on more than 3
billion devices. It is a quick, dependable, and secure programming
language and is utilized in every tiny detail, from desktops to
online applications, scientific supercomputers to gaming consoles,
and cell phones to the Internet.
Java developers can choose from a wide range of work options. So,
here are some of the most common Java interview questions and
answers that will help you land a job or, at the very least, improve
your knowledge. These Java interview questions and answers will
assist you in acing major interview processes and will put your
skills and knowledge to the ultimate test.
Java interview questions and answers
Following are some important Java interview questions that one
must revise before your technical job interview.
Q1. What do you understand by Java?
Java is a popular high-level programming language developed by
Sun Microsystems (now owned by Oracle) in the mid-1990s. It was
designed to be platform-independent, meaning that the same code
could be executed on different operating systems without any
modification.
Java is an object-oriented language, which means that it focuses on
objects and their interactions rather than just functions and
procedures. It is also statically typed, which means that the data
types of variables must be declared before they are used.
Java is widely used in various fields such as software development,
mobile app development, web development, and scientific
computing. It has a rich standard library that provides many useful
functionalities, and there is a vast ecosystem of third-party libraries
and tools available for Java development.
Q2. What is the difference between JDK and JRE?

Java Development Kit (JDK) is an acronym for Java Development


Kit. It includes the tools and libraries needed to create Java
programs. JRE stands for Java Runtime Environment, and it
includes compilers and debuggers for compiling Java programs.
This is a feature of JDK. JRE is a set of libraries and a virtual
machine (JVM) that are necessary to run a Java program.
Here's a table summarizing the differences between JDK and JRE:

JRE (Java
JDK (Java
Runtime
Developme
Environmen
nt Kit)
t)

To develop
and deploy To run Java
Purpose
Java applications
applications

Includes the
Includes the
JRE,
Java Virtual
compilers,
Componen Machine
debuggers,
ts (JVM) and
and other
class
developmen
libraries
t tools

Used by
Used by end-
developers
users to run
Usage to create
Java
Java
applications
applications

Size Larger in Smaller in


size as it size
contains compared to
JRE (Java
JDK (Java
Runtime
Developme
Environmen
nt Kit)
t)

developmen
JDK
t tools

Must be
Optional, but
installed to
required to
Installation develop and
run Java
deploy Java
applications
applications

Used to Used to run


create and Java-based
Example
compile software or
Java code applets

Q3. What are the features of the Java Programming language?

Here are some of the key features of the Java programming


language:
1. Object-oriented: Java is a fully object-oriented language, which
means it focuses on objects and their interactions rather than just
functions and procedures. It supports the concepts of encapsulation,
inheritance, and polymorphism, which enable developers to create
reusable and modular code.
2. Platform-independent: Java is designed to be platform-
independent, which means that the same code can run on different
operating systems without any modifications. This is possible due
to the use of a virtual machine, which provides an abstraction layer
between the code and the underlying hardware.
3. Robust: Java is known for its robustness, which means it has a
strong emphasis on reliability and error-free programming. It
includes features such as automatic memory management,
exception handling, and type checking to prevent errors and ensure
stability.
4. Multithreaded: Java supports multithreading, which means that
multiple threads can run concurrently within a single program. This
allows developers to create highly responsive and interactive
applications.
5. Secure: Java is designed with security in mind and includes
features such as a security manager, which helps prevent
unauthorized access to system resources. It also includes a sandbox
model for running untrusted code, which can help protect against
malicious code.
6. Portable: Java is highly portable, which means that code written in
Java can be easily moved from one system to another. This is
possible due to the use of bytecode, which is a platform-
independent format that can be executed on any system that has a
Java Virtual Machine (JVM).
7. Rich standard library: Java includes a rich standard library that
provides many useful functionalities, such as I/O operations,
networking, database access, and GUI programming. This makes it
easier for developers to write complex applications without having
to reinvent the wheel.
4. What are the different types of memory areas allocated by
JVM?

The JVM (Java Virtual Machine) allocates memory for various


purposes during the execution of a Java program. Here are the
different types of memory areas allocated by the JVM:
1. Heap memory: Heap memory is the runtime data area in which
objects are allocated. When a new object is created, the JVM
allocates memory from the heap. The heap is divided into two
parts: the young generation and the old generation.
2. Stack memory: Stack memory is used to store local variables and
method call frames. Each thread of execution in a Java program has
its own stack, which is created when the thread is started. The stack
is used to keep track of method calls and to store the local variables
of each method.
3. Method area: The method area stores class structures, method
code, and static variables. It is shared among all threads and is
created when the JVM starts up.
4. PC (Program Counter) registers: Each thread in a Java program
has its own PC register, which stores the address of the currently
executing instruction.
5. Native method stacks: The native method stack is used to store
data and information when native methods are called. Native
methods are written in other programming languages, such as C or
C++, and are accessed using the Java Native Interface (JNI).
These memory areas are managed by the JVM, which performs
various memory management tasks, such as garbage collection, to
ensure efficient use of memory and prevent memory leaks.
Q5. How Java platform is different from other platforms?
The Java platform is different from other platforms in several ways.
Here are some of the key differences:
1. Platform independence: Java is designed to be platform-
independent, which means that Java code can run on any system
with a Java Virtual Machine (JVM) installed, regardless of the
underlying hardware and operating system. This makes Java
programs highly portable and eliminates the need for developers to
write separate code for different platforms.
2. Security: Java includes a number of built-in security features that
make it more secure than other programming languages. For
example, Java code runs in a sandbox environment that restricts
access to system resources, and Java's class loader ensures that
code is only loaded from trusted sources.
3. Automatic memory management: Java includes automatic
memory management, which means that developers do not have to
manually allocate and deallocate memory. Instead, the JVM
automatically manages memory allocation and garbage collection,
which makes Java programs more stable and less prone to memory-
related errors.
4. Large standard library: Java includes a large standard library that
provides a wide range of useful classes and functions for tasks such
as I/O operations, networking, and database access. This makes it
easier for developers to write complex applications without having
to reinvent the wheel.
5. Object-oriented programming: Java is a fully object-oriented
programming language, which means that everything in Java is an
object. This approach encourages the development of reusable and
modular code and makes it easier to write large-scale applications.
6. Multithreading: Java supports multithreading, which means that
multiple threads can run concurrently within a single program. This
makes it easier to write complex, interactive applications that can
handle multiple tasks simultaneously.
Q6. Can we write the main method as public void static instead
of the public static void?
No, the correct syntax for the main method in Java is . The order of
the keywords and cannot be interchanged, as they each have a
specific meaning in Java.
is an access modifier that makes the method accessible to all other
classes in the Java program, while is a keyword that makes the
method belong to the class rather than an instance of the class.
The keyword indicates that the method does not return any value,
and is the name of the method. Therefore, if you write the main
method as , you will get a syntax error, as the order of the access
modifier and the static keyword is incorrect.
Q7. What is Core Java?
Core Java refers to the fundamental components of the Java
programming language, which includes the basic concepts, syntax,
and libraries that are used to develop Java applications. It is the
foundation upon which all other Java-related technologies and
frameworks are built.
The core Java components include the following:
1. Language basics: This includes the syntax, data types, operators,
control structures, and exception-handling mechanisms of the Java
language.
2. Object-oriented programming: Java is a fully object-oriented
programming language, which means that everything in Java is an
object. The core Java components include concepts such as classes,
objects, inheritance, encapsulation, and polymorphism.
3. Java libraries: The core Java libraries include the Java Standard
Library, which provides a wide range of classes and interfaces for
common programming tasks, such as input/output operations,
networking, and data manipulation.
4. Multithreading: Java supports multithreading, which means that
multiple threads can run concurrently within a single program.
Core Java includes concepts such as thread synchronization, locks,
and concurrency control.
5. Collections: Java provides a rich set of collection classes, such as
lists, sets, maps, and queues, that allow for efficient manipulation
of data.
Overall, Core Java is essential for anyone who wants to learn Java
programming and develop Java applications. It provides a solid
foundation upon which developers can build more complex and
advanced applications.
Q8. What is an array in Java?
A container object that stores a fixed number of values of a single
type is called an array. The length of an array is determined at the
time of its creation. Its length is set when it is created.
In Java, an array is an object that stores a fixed-size sequential
collection of elements of the same type. The elements in an array
are accessed using an index, which is an integer value that
represents the position of an element in the array.
An array is declared by specifying the type of elements it contains,
followed by square brackets [], and then the name of the array. For
example, to declare an array of integers named "numbers" that can
hold 5 elements, you would write:
int[] numbers = new int[5];
In this example, we declare an array of integers, and use the
keyword to allocate memory for 5 integer values. The elements in
the array are initialized to their default value (which is 0 for
integers) when the array is created.
To assign values to elements in an array, we use the index notation.
For example:
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
This code assigns the values 10, 20, 30, 40, and 50 to the first five
elements of the array, respectively.
Arrays in Java have a fixed size, which means that once an array is
created, its size cannot be changed. However, you can create a new
array with a different size and copy the elements from the original
array to the new array if you need to resize an array.
Q9. What is the difference between byte and char data types in
Java?
In Java, and are both primitive data types, but they have different
characteristics and uses.
1. Size: The data type is an 8-bit signed two's complement integer,
which means that it can represent integer values in the range of -
128 to 127. On the other hand, the data type is a 16-bit Unicode
character, which means that it can represent a single character from
the Unicode character set.
2. Usage: is mainly used to store small integer values, such as flags,
or to read/write binary data from files or streams. The data type is
used to represent characters, and is commonly used in string
manipulation and in graphical user interface (GUI) programming.
3. Representation: values are represented in 2's complement binary
form, whereas values are represented in Unicode characters using a
16-bit representation.
4. Default Value: has a default value of 0, while has a default value
of '\u0000' (null character).
Here's an example that demonstrates the differences between and :
byte b = 65; // Assigns the ASCII value for 'A'
char c = 'A'; // Assigns the character 'A'

System.out.println(b); // Prints 65
System.out.println(c); // Prints A
In this example, the variable is assigned the value 65, which is the
ASCII value for the character 'A'. The variable is directly assigned
the character 'A', which is represented using Unicode. When we
print , it prints the integer value 65. When we print , it prints the
character 'A'.
Q10. Why do we need a constructor in Java?
Constructors are an essential part of the Java programming
language and serve several purposes. Here are a few reasons why
constructors are needed in Java:
1. Object Initialization: Constructors are used to initialize the
object's state, i.e., the instance variables of the object. When a new
object is created using the keyword, the constructor is
automatically called to initialize the object's state. Without a
constructor, the object's state would be undefined, and the program
would likely crash.
2. Provide Default Values: Constructors can provide default values
for instance variables if no values are specified during object
creation. This can help avoid errors or incorrect behavior in the
program.
3. Encapsulation: Constructors can be used to enforce encapsulation
by ensuring that the object's state is properly initialized and
validated before any other methods are called on the object.
4. Inheritance: Constructors play a critical role in inheritance. When
a subclass is created, the constructor of its superclass is
automatically called first to initialize the inherited state. This
process continues up the inheritance hierarchy until the root class's
constructor is called.
5. Polymorphism: Constructors can be used to create objects of
different types and classes, allowing for polymorphic behavior in
the program. For example, a constructor can be defined in an
abstract class to create objects of its concrete subclasses.
Overall, constructors are a critical part of the Java programming
language and are necessary for proper object initialization,
encapsulation, inheritance, and polymorphism.
Q11. How many types of constructors does Java support?
Java supports two types of constructors:
1. Default Constructor: A default constructor is a constructor that is
automatically generated by the compiler if no constructor is
explicitly defined in the class. It has no arguments and does not
perform any initialization. It is used to create an object with default
values for all its instance variables.
2. Parameterized Constructor: A parameterized constructor is a
constructor that takes one or more parameters and is explicitly
defined in the class. It is used to create an object with specific
values for its instance variables. When a parameterized constructor
is defined, the default constructor is not generated by the compiler.
Here is an example of a class with a default constructor and a
parameterized constructor:
public class Person {
String name;
int age;

// Default constructor
public Person() {
// No initialization performed
}

// Parameterized constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
n this example, the class has a default constructor that does not
perform any initialization and a parameterized constructor that
takes two parameters ( and ) and initializes the instance variables
with the passed values using the keyword.
Q12. Why constructors cannot be final, static, or abstract in
Java?
In Java, constructors cannot be declared as final, static, or abstract
for several reasons:
1. Final Constructors: Constructors are used to initialize objects and
the primary purpose of a final keyword is to prevent further
modifications to a variable, method or class. A final constructor
would defeat the purpose of a constructor because if a constructor
were declared final, it would not be possible to create a subclass
and override the constructor. Therefore, final constructors are not
allowed in Java.
2. Static Constructors: Constructors are called when an object is
created, and they initialize instance variables of the object. Since
static members are associated with the class and not with objects, it
does not make sense to have a static constructor. Static initializers,
on the other hand, are used to initialize static variables and are run
once when the class is loaded, not when objects are created.
3. Abstract Constructors: An abstract class is a class that cannot be
instantiated, so if a constructor was declared abstract, it would
never be used. The abstract keyword is used to specify that a class
or method should be implemented by subclasses, but since
constructors are always implemented by the class that defines them,
it is not possible to declare a constructor as abstract.
In summary, constructors cannot be final, static, or abstract because
they have a specific purpose and behavior that does not fit with the
semantics of those keywords.
Q13. How to implement constructor chaining using the Java
super keyword?
Constructor chaining in Java is a technique that allows one
constructor to call another constructor in the same class or in a
parent class using the keyword. This technique is useful when you
want to reuse code from one constructor in another constructor or
when you want to avoid duplicating code. Here's how you can
implement constructor chaining using the keyword in Java:
1. Calling a Parent Class Constructor: You can call a constructor of
a parent class using the keyword followed by parentheses that
contain the arguments passed to the constructor. Here's an example:
public class Parent {
public Parent(int arg1, int arg2) {
// constructor code
}
}

public class Child extends Parent {


public Child(int arg1, int arg2, int arg3) {
super(arg1, arg2); // call Parent constructor
// constructor code
}
}
In this example, the class extends the class and calls the parent
constructor using the keyword with and arguments.
2. Calling Another Constructor in the Same Class: You can also
call another constructor in the same class using the keyword
followed by parentheses that contain the arguments passed to the
constructor. Here's an example:
3. public class Parent {
4. public Parent(int arg1, int arg2) {
5. // constructor code
6. }
7. }
8.
9. public class Child extends Parent {
10. public Child(int arg1, int arg2, int arg3) {
11. super(arg1, arg2); // call Parent constructor
12. // constructor code
13. }
}
14. How aggregation and composition are different concepts?
Aggregation and composition are two concepts used in object-
oriented programming to describe relationships between objects.
While they are similar in some ways, there are important
differences between the two concepts.
Aggregation is a type of relationship between objects where one
object is composed of one or more other objects. In an aggregation
relationship, the objects can exist independently of each other. In
other words, the objects can be created and destroyed
independently, without affecting the other object in the relationship.
An example of aggregation could be a university that has multiple
departments. The departments can exist independently of the
university and can be created or destroyed without affecting the
university.
Composition, on the other hand, is a more specific type of
aggregation where one object is composed of one or more other
objects, and the composed objects cannot exist independently of the
main object. In other words, if the main object is destroyed, the
composed objects are also destroyed. Composition is often used to
model parts and whole relationships. An example of composition
could be a car that is composed of an engine, wheels, and other
parts. If the car is destroyed, the engine, wheels, and other parts are
also destroyed.
Here are some key differences between aggregation and
composition:
 In aggregation, the objects can exist independently of each other,
while in composition, the composed objects cannot exist
independently of the main object.
 In aggregation, the objects can be created and destroyed
independently of each other, while in composition, the composed
objects are created and destroyed with the main object.
 Aggregation is often used to model relationships between similar
objects, while composition is often used to model parts and whole
relationships.
Overall, aggregation and composition are both important concepts
in object-oriented programming, and understanding the differences
between them is essential for designing effective object-oriented
systems.
Q15. What is an association?

In object-oriented programming, an association is a relationship


between two or more objects that reflects a meaningful connection
or interaction between them. It is a way to describe how objects in
a system relate to each other and interact with each other.
Associations are often represented in UML diagrams as a line
between two classes, with optional symbols indicating the nature of
the association. The symbols can include arrows, multiplicity
indicators, and labels to describe the nature of the relationship.
There are several types of associations, including:
 One-to-one: where one object is associated with one other object.
 One-to-many: where one object is associated with multiple other
objects.
 Many-to-many: where multiple objects are associated with multiple
other objects.
 Aggregation: where one object is part of another object.
 Composition: where one object is composed of other objects.
An association can also have additional attributes or behaviors
associated with it, such as navigability, visibility, and constraints.
Overall, associations are an important concept in object-oriented
programming as they help to model the relationships between
objects in a system, and can help to make the system more flexible
and adaptable to changing requirements.
Q 16. If there are no pointers in Java, then why do we get
NullPointerException?
While Java does not have explicit pointers like other programming
languages, it still has a concept of references. In Java, objects are
always referred to by references, which are essentially variables
that hold the memory address of the object. These references can be
null, which means they don't refer to any object in memory.
A occurs when you try to access or dereference a null reference.
For example, if you try to call a method on an object that is null,
you will get a . This is because the null reference does not refer to
any object in memory, and therefore cannot be used to call a
method or access an instance variable.
Here's an example that demonstrates how a can occur:
String s = null;
int length = s.length(); // Throws NullPointerException because s is
null
In this example, we declare a variable of type and initialize it to
null. We then try to access the method of the string, but since is
null, we get a .
In summary, while Java does not have explicit pointers, it still has
the concept of references and a occurs when you try to access or
dereference a null reference.
Q17. What is object cloning in Java?
In Java, object cloning is the process of creating a copy of an
existing object. The copy, or clone, is a new object with the same
state as the original object. Object cloning can be useful in a variety
of situations, such as when you need to create a backup copy of an
object, or when you want to create a copy of an object to be used in
a multi-threaded environment.
Java provides a mechanism for object cloning through the interface
and the method. The interface is a marker interface, which means it
doesn't have any methods or fields, but it tells the JVM that the
object implementing this interface can be cloned. The method is
used to create the clone of the object.
To make a class cloneable, you need to implement the interface and
override the method. The method should create a new object and
copy the state of the current object to the new object.
Here is an example of a class that implements the interface and
overrides the method:
public class MyClass implements Cloneable {
private int value;

public MyClass(int value) {


this.value = value;
}

public void setValue(int value) {


this.value = value;
}

public int getValue() {


return value;
}

@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // Shallow copy
}
}

Q18. Is it possible to use this() and super() both in the same


constructor?
No, it is not possible to use both and in the same constructor. The
reason for this is that and must be the first statement in a
constructor, and you can only have one first statement.
The constructor call is used to invoke another constructor in the
same class, while the constructor call is used to invoke a
constructor in the superclass. Since both of these calls must be the
first statement in a constructor, it is not possible to use them both in
the same constructor.
However, you can use or in a chain of constructor calls. For
example, you can have one constructor that calls another
constructor in the same class using , and that constructor in turn can
call a constructor in the superclass using . Here is an example:
class Main {
int empId;
String empName;
public Main(int id, String name) {
this.empId = id;
this.empName = name;
}

//Copy constructor
public Main(Main emp) {
empId = emp.empId;
empName = emp.empName;
}

public void displayDetails() {


System.out.println("Employee ID:" + empId);
System.out.println("Employee Name:" + empName);
}

public static void main(String[] args) {


Main e1 = new Main(85, "Saurav");
//Create a copy of another object
Main e2 = new Main(e1);
e1.displayDetails();
e2.displayDetails();
}
}

Q19. What is the difference between this() and super() in Java?


Below are the key differences between this and super.

This Super

1. The current class 1. The current instance


object is of a parent/base class is
represented by
represented by super().
this().

2. used to call the 2. To invoke the


same class's default parent/base class's
constructor default constructor

3. Used to access
3. Used to access parent
the current class's
class method.
methods.

4. Used for
4. Used for pointing the
pointing the current
superclass instance
class instance

5. Must be the first 5. Must be the first line


line of a block of a block

Q20. What is the difference between break and continue


statements?
Below are the key differences between break and continue.

Break Continue

Switch and loop


(for, while, do Only works with loop
while) statements statements.
are supported.

When it is It does not end the loop


executed, it causes
but instead causes it to
the switch or loop
jump to the next
statements to
iteration.
terminate.

It immediately When a continue is


terminates the used within a loop that
innermost is nested with a switch
enclosing loop or statement, the next loop
switch. iteration is executed.

Q21. What is a copy constructor in Java?


A member function called copy constructor is used to initialize an
object with another object of the same class. Because all objects in
Java are passed by reference, there is no need for a copy
constructor. Furthermore, Java does not provide automated pass-by-
value pass-through.
class Main {
int empId;
String empName;
public Main(int id, String name) {
this.empId = id;
this.empName = name;
}

//Copy constructor
public Main(Main emp) {
empId = emp.empId;
empName = emp.empName;
}
public void displayDetails() {
System.out.println("Employee ID:" + empId);
System.out.println("Employee Name:" + empName);
}

public static void main(String[] args) {


Main e1 = new Main(85, "Saurav");
//Create a copy of another object
Main e2 = new Main(e1);
e1.displayDetails();
e2.displayDetails();
}
}
 Employee ID:85
 Employee Name:Saurav
 Employee ID:85
 Employee Name:Saurav
Q22. Is it possible to use a non-static variable in a static
context?
In Java, it is not possible to use a non-static variable in a static
context. This is because non-static variables are associated with a
specific instance of a class, while static variables are associated
with the class itself. A static context is a context where there is no
instance of the class available, such as in a static method or a static
block.
Attempting to use a non-static variable in a static context will result
in a compilation error. For example, consider the following code:
public class MyClass {

private int value;


public static void myStaticMethod() {

System.out.println(value); // Compilation error

}
In this code, we are attempting to use the non-static variable in a
static method . This will result in a compilation error, since is not
associated with the class itself, but with instances of the class.
Q23. Why do we mark the main method as static in Java?
In Java, we mark the method as static because it allows the method
to be called without creating an instance of the class. When a Java
program is run, the JVM looks for a method with a specific
signature () to begin executing the program. Since the method is the
entry point of the program, it must be accessible without creating
an object of the class.
Marking the method as static ensures that the method belongs to the
class itself, and not to a specific instance of the class. This allows
the JVM to call the method without creating an instance of the class
first. If the method were not static, the JVM would have to create
an instance of the class to call the method, which would be
unnecessary since the method typically does not require access to
instance variables or methods.
Q24. What is Inheritance?
Inheritance is a fundamental concept in object-oriented
programming (OOP) that allows a class to inherit properties and
behavior from a parent class. In Java, inheritance is implemented
using the keyword.
When a class inherits from a parent class, it automatically includes
all the public and protected members of the parent class in its own
definition. This includes fields, methods, and nested classes. The
child class can also define its own fields and methods, and can
override the behavior of the parent class methods using the
annotation.
Inheritance creates a hierarchical relationship between classes,
where each child class is more specialized than its parent class.
This allows for code reuse and modularization, since common
functionality can be defined in a parent class and inherited by
multiple child classes.
Q25. What are the different types of inheritance in Java?
In Java, there are five types of inheritance:
1. Single inheritance: This is the most common type of inheritance,
where a subclass extends a single parent class. The subclass inherits
all the fields and methods of the parent class, and can also override
the parent class methods.
2. Multilevel inheritance: This is where a subclass inherits from a
parent class, and then another subclass inherits from that subclass.
In this way, a chain of inheritance is created, where each subclass
inherits from its immediate parent class.
3. Hierarchical inheritance: This is where multiple subclasses
inherit from a single parent class. In other words, a single-parent
class is the superclass of multiple subclasses.
4. Multiple inheritance (through interfaces): This is where a
subclass implements multiple interfaces, each of which defines a
set of methods that the subclass must implement. This allows the
subclass to inherit behavior from multiple sources, without the
potential conflicts that can arise with multiple inheritance of
implementation.
5. Hybrid inheritance: This is a combination of any two or more
types of inheritance. For example, a subclass might use single
inheritance to inherit from a parent class, and then use multiple
inheritance through interfaces to inherit from multiple interfaces.
It's important to note that Java does not support multiple
inheritance of implementation (i.e., where a subclass inherits from
multiple parent classes that define the same method with different
implementations). However, it does allow multiple inheritance
through interfaces, as described above.
Q26. Why is multiple inheritances not supported in Java?
Multiple inheritances occur when a child class inherits properties
from several other classes. Extending several classes in Java is not
possible.
Multiple inheritances have the drawback of making it difficult for
the compiler to decide which method from the child class to
execute at runtime if multiple parent classes have the same method
name.
As a result, multiple inheritances in Java are not supported. The
Diamond Problem is a well-known name for the issue. If you have
any difficulties answering these Java interview questions, please
leave a remark below.
import java.io.*;

class classP {
public void display() {
System.out.println("Class P");
}
}
class classQ {
public void display() {
System.out.println("Class Q");
}
}
class Main extends classP, classQ {
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}
Q27. How will you implement method overloading in Java?
Method overloading is a feature in Java that allows a class to have
multiple methods with the same name, but different parameter lists.
This allows a class to provide different methods that perform
similar operations, but with different inputs.
To implement method overloading in Java, you can define multiple
methods with the same name in a class, but with different
parameters. The parameters can differ in their number, type, or
order. When you call the method, Java determines which method to
call based on the number and types of the arguments passed to the
method.
Here is an example of method overloading in Java:
public class MathUtils {
public static int add(int x, int y) {
return x + y;
}
public static double add(double x, double y) {
return x + y;
}

public static int add(int x, int y, int z) {


return x + y + z;
}
}

In this example, the class defines three methods with the same
name, , but with different parameter lists. The first method takes
two parameters and returns an result. The second method takes two
parameters and returns a result. The third method takes three
parameters and returns an result.
When you call the method, Java determines which method to call
based on the types of the arguments you pass to it. For example, if
you call , Java will call the first method, which takes two
parameters. If you call , Java will call the second method, which
takes two parameters. And if you call , Java will call the third
method, which takes three parameters.
By overloading the method with different parameter lists, the class
provides a flexible and convenient way to perform addition with
different types and numbers of inputs.
Q28. What is Polymorphism?
"One interface, many implementations" is a precise description of
polymorphism. Polymorphism is the ability to ascribe a distinct
meaning or usage to something in different situations - especially,
the ability to have many forms for an item such as a variable, a
function, or an object. There are two types of polymorphism
Compile-Time Polymorphism: Method overloading is compile-
time polymorphism
class Calculator

int add(int a, int b) {

return a + b;

int add(int a, int b, int c) {

return a + b + c;

public class Main

public static void main(String args[]) {

Calculator obj = new Calculator();

System.out.println(obj.add(15, 20));

System.out.println(obj.add(14, 36, 30));

}
}
 35
 80
Run time polymorphism or Dynamic
Polymorphism: Inheritance and interfaces are used to implement
runtime time polymorphism.
Q29. Can we change the scope of the overridden methods in the
subclass?
No, we cannot change the scope of the overridden methods in the
subclass. In Java, when a subclass overrides a method of its parent
class, it must provide the same method signature (i.e., same method
name, return type, and parameter list) as the parent class method.
The only thing that can be changed in the overridden method is its
implementation.
The scope of a method (i.e., its access modifier) is part of its
method signature, and changing the scope of an overridden method
in the subclass would change its signature, which would break the
inheritance contract.
For example, if a method in the superclass is declared as , the same
method in the subclass cannot be overridden with a narrower scope,
such as or . Similarly, if a method in the superclass is declared as ,
the same method in the subclass cannot be overridden with a wider
scope, such as .
However, it is possible to provide a new method in the subclass
with a different scope that has the same name as the method in the
superclass. This is not overriding, but rather method hiding.
Method hiding occurs when a subclass defines a static method with
the same name and signature as a static method in the superclass. In
this case, the subclass method "hides" the superclass method, and
the scope of the subclass method can be different from that of the
superclass method.
Q30. What is the difference between method overloading and
method overriding in Java?
In Method Overloading, method signature of the same class share
the same name but each method must have a different number of
parameters or parameters having different types and order.
Method Overloading is to “add” or “extend” more to the method’s
behavior.
 It is a compile-time polymorphism.
 The methods must have a different signature.
 It may or may not need inheritance in Method Overloading.
In Method Overriding, the subclass has the same method with the
same name and exactly the same number and type of parameters,
and the same return type as a superclass.
Method Overriding is to “Change” the existing behavior of the
method.
 It is a run time polymorphism.
 The methods must have the same signature.
 It always requires inheritance in Method Overriding.
Q31. What is encapsulation in Java?

Encapsulation in Java is the concept of bundling data (i.e.,


variables) and methods that operate on that data within a single
unit, typically a class. It is one of the fundamental principles of
object-oriented programming (OOP) and is often described as "data
hiding".
Encapsulation provides several benefits in Java programming,
including:
1. Improved maintainability: Encapsulating data and methods
within a class helps to reduce code complexity and makes it easier
to maintain and modify the code.
2. Improved security: By hiding implementation details and
providing controlled access to data through methods, encapsulation
helps to prevent unintended modifications to the data.
3. Improved reusability: Encapsulated classes can be easily reused
in different programs and systems without the risk of affecting
other parts of the code.
In Java, encapsulation is achieved through the use of access
modifiers, such as , , and , to control the visibility of the data and
methods within a class. Typically, instance variables are declared as
private, and public getter and setter methods are provided to access
and modify the data.
Q32. What is an abstraction in Java?

Abstraction in Java is the process of hiding implementation details


while showing only the necessary features of an object to the user.
It is one of the fundamental concepts of object-oriented
programming (OOP) and is used to create simplified and reusable
models of complex systems.
In Java, abstraction is achieved through the use of abstract classes
and interfaces. Abstract classes are classes that cannot be
instantiated directly but can be subclassed to provide concrete
implementations of their abstract methods. Abstract methods are
methods that are declared in the abstract class but have no
implementation. Subclasses of the abstract class must provide
concrete implementations of all abstract methods.
Interfaces, on the other hand, are like abstract classes in that they
cannot be instantiated directly, but they only contain method
signatures and constant values. Interfaces define a set of methods
that a class must implement in order to be considered an
implementation of the interface.
The main benefit of abstraction is that it helps to manage
complexity by hiding implementation details and providing a
simplified view of an object or system to the user. Abstraction also
makes it easier to modify and maintain code, since changes to the
implementation of an abstract class or interface can be made
without affecting the rest of the system.
Q33. How to create a singleton class in Java?
A singleton class is one for which only one object can be created.
You can make a singleton class by making its constructor private,
which restricts the object's creation. Provide a static method for
getting an object instance, so you may handle object creation
entirely within the class. We're using a static block to create an
object in this example.
public class Main {

private static Main myObj;


static{
myObj = new Main();
}
private Main(){

}
public static Main getInstance(){
return myObj;
}

public void test(){


System.out.println("Hello");
}

public static void main(String a[]){


Main ms = getInstance();
ms.test();
}
}
Q34. Can you override a private or static method in Java?
In Java, it is not possible to override a private method in a subclass
because private methods are not visible to subclasses. Private
methods are only accessible within the class where they are
declared, and they cannot be accessed or overridden by any other
class, including subclasses.
Similarly, it is not possible to override a static method in a subclass
because static methods are associated with a class, not an instance
of a class. When a subclass defines a static method with the same
signature as a static method in the superclass, it simply hides the
superclass method instead of overriding it.
However, it is possible to provide a new implementation for a static
method in a subclass by declaring a static method with the same
name and signature in the subclass. In this case, the static method
in the subclass will be called instead of the static method in the
superclass when the method is invoked on the subclass.
In summary, it is not possible to override a private or static method
in Java. Private methods are not visible to subclasses, and static
methods are associated with a class, not an instance of a class.
However, it is possible to provide a new implementation for a static
method in a subclass by declaring a static method with the same
name and signature in the subclass.
Q35. Explain the difference between static and dynamic
binding

In Java, binding is the process of connecting a method call to the


method code that implements it. There are two types of binding in
Java: static binding and dynamic binding.
Static binding, also known as early binding, is a type of binding
that occurs at compile time. In static binding, the method call is
bound to the method implementation based on the type of the
reference variable used to invoke the method. The method to be
called is determined at the time of compilation, and the same
method is called every time the program runs.
For example, consider the following code:
public class A {
public void method() {
System.out.println("Method in class A");
}
}

public class B extends A {


public void method() {
System.out.println("Method in class B");
}
}

public class Main {


public static void main(String[] args) {
A obj = new B();
obj.method();
}
}
In this example, the method call is statically bound to the
implementation in class at compile time because the reference
variable is of type . Therefore, the output of this program will be
"Method in class A", even though the object being referred to is of
type .
Dynamic binding, also known as late binding, is a type of binding
that occurs at runtime. In dynamic binding, the method call is
bound to the method implementation based on the actual type of the
object at runtime. The method to be called is determined at runtime,
and the same method may not be called every time the program
runs.
Q36. What do you mean by an interface in Java?

In Java, an interface is a collection of abstract methods and


constant variables that can be implemented by a class. An interface
specifies a set of behaviors that a class must implement if it claims
to implement that interface.
An interface can be declared using the keyword and can contain
method signatures, default methods, static methods, and constant
variables. A method signature in an interface is a method
declaration without an implementation, which means that the
method does not have a body. The class that implements the
interface must provide the implementation for all of the methods in
the interface.
Q37. Is it allowed to mark a method abstract as well as a final?
No, it is not possible to mark a method as both abstract and final in
Java.
An method is a method that does not have a body and is intended to
be overridden by the implementing class, while a method is a
method that cannot be overridden by any subclass. These two
modifiers have opposite meanings and cannot be used together in
the same method declaration.
When a method is marked as , it means that the method must be
implemented by the subclass, whereas marking a method as means
that it cannot be overridden by the subclass. Therefore, it would be
contradictory to mark a method as both and .
If a class has an method that needs to be implemented by the
subclass but should not be overridden by any further subclass, then
the method can be marked as in the subclass, but not in the abstract
superclass.
Q38. Is it allowed to mark an interface method as static?
Yes, it is allowed to mark an interface method as static in Java.
In Java 8 and later versions, interfaces can contain static methods.
A static method in an interface is declared using the keyword and
can have a body.
Here is an example of an interface with a static method:
public interface ExampleInterface {
void exampleMethod();
static void staticMethod() {
System.out.println("This is a static method in the interface.");
}
}
In this example, the interface contains an abstract method and a
static method .
Q39. What is the difference between abstract class and
interface in Java?
Below are the key differences between abstract class and interface

Abstract Class Interface

An abstract class can An interface can


include both complete only give the
default code and signature, not any
information that must
code.
be customized.

Non-abstract methods An Interface's


can exist in an abstract methods are all
class. abstract.

Several interfaces
A class can only extend
can be
one abstract class in the
implemented by a
case of an abstract class
single class.

Instance variables can Instance variables


exist in an abstract are not allowed in
class. an interface.

The abstract The interface


keyword is used to keyword is used to
declare an abstract declare the
class. interface.

Q40. What are the main uses of the 'this' keyword?


In Java, the keyword refers to the current instance of the class in
which it appears. Here are some of the main uses of the keyword:
1. To differentiate between instance variables and local
variables: If a method or constructor parameter has the same name
as an instance variable, the keyword can be used to refer to the
instance variable instead of the local variable. For example, refers
to the instance variable , whereas refers to the local variable.
2. To call another constructor in the same class: Using with
appropriate arguments can call another constructor in the same
class. This is called constructor chaining and is a useful technique
for avoiding code duplication and ensuring that all constructors
initialize the object consistently.
3. To pass the current object as a parameter: The keyword can be
used to pass the current object as a parameter to a method or
constructor. This can be useful in situations where the current
object needs to be used in the method or constructor, such as when
setting up listeners or callbacks.
4. To return the current object from a method: The keyword can
be used to return the current object from a method, which allows
for method chaining. Method chaining is a technique where
multiple method calls are chained together in a single statement,
which can improve code readability and reduce the amount of code
required.
Overall, the keyword is a powerful tool for working with Java
classes and instances, and is commonly used to differentiate
between local and instance variables, call other constructors, pass
the current object as a parameter, and return the current object from
a method.
Q41. Can a class be marked final in java?
In Java, a class can be tagged as final. A class that has been
declared as final cannot be extended or inherited. Trying to do so
will result in compilation error.
final class Vehicle {

public void speed() {

System.out.println("Default speed");

}
}

public class Car extends Vehicle{

public static void main(String[] args) {

The type Car cannot subclass the final class Vehicle


Q42. Is it allowed to declare the main method as final?
Yes, it is allowed to declare the method as in Java. However, it
doesn't have any practical use because the method is typically the
entry point for a Java program and is called by the Java Virtual
Machine (JVM) to start the program. Since the method is called by
the JVM and not by any other part of the program, there is no need
to override or prevent it from being overridden using the keyword.
In general, the keyword is used to prevent a method, class, or
variable from being modified or overridden. While it is allowed to
use the keyword with the method, it is not a common practice and
does not have any practical benefits in most cases.
Q43. What is the purpose of package in Java?
In Java, a package is a namespace that helps to organize related
classes and interfaces. The purpose of a package is to group related
classes, interfaces, and other resources together in a single, named
unit, which can be used to avoid naming conflicts and provide
better organization and modularity for the code.
Here are some of the main purposes of packages in Java:
1. Avoiding naming conflicts: Since package names are unique,
using packages can help to avoid naming conflicts between classes
and interfaces that have the same name in different packages. This
is particularly useful when integrating third-party libraries into a
project, as it can help to avoid naming conflicts between different
libraries.
2. Better organization and modularity: By grouping related classes
and interfaces together in a single package, it can be easier to
understand and navigate the codebase. This can also help to
improve modularity, as packages can be used to separate different
parts of the code and provide a clear separation of concerns.
3. Access control: Packages can be used to control access to classes
and interfaces by using access modifiers such as , , , and . This
allows you to restrict access to certain classes and interfaces to
specific packages or modules, which can help to improve security
and encapsulation.
4. Improved code reuse: By grouping related classes and interfaces
together in a package, it can be easier to reuse code across different
parts of the codebase. This is particularly useful when creating
libraries or frameworks that can be reused across different projects
or modules.
Overall, packages are an important concept in Java that help to
provide better organization and modularity for code, avoid naming
conflicts, control access to classes and interfaces, and improve code
reuse.
Q44. What is a static import in Java?
Static import is similar to normal import declaration. Normal
import allows us to import classes from packages without using
package qualifiers. A static import allows us to import static
members from a class without using a class qualifier.
import static java.lang.Math.*;
import static java.lang.System.*;
public class Main {
public static void main(String[] args) {
out.println(sqrt(16));
out.println(abs(6.9));
out.println(pow(5, 2));
}
}
 4.0

 6.9

 25.0

Q45. What is the purpose of the process of serialization?


Serialization is the process of converting an object into a byte
stream that can be saved to a file, sent over a network, or stored in a
database. The main purpose of serialization is to enable the state of
an object to be saved and restored at a later time, without losing
any of its data or functionality.
Here are some of the main purposes of serialization in Java:
1. Persistence: By serializing objects, you can save their state to a file
or database, and then reload the state at a later time. This is useful
for applications that need to store data between sessions or across
different instances of the application.
2. Network communication: By serializing objects, you can send
them over a network as a byte stream, and then deserialize them on
the other end to reconstruct the original object. This is useful for
client-server applications that need to exchange data over a
network.
3. Caching: By serializing objects, you can cache them in memory or
on disk, and then reload the serialized state when needed. This can
help to improve performance and reduce the amount of time needed
to recreate the object from scratch.
4. Deep cloning: By serializing and deserializing an object, you can
create a deep copy of the object, which can be useful for creating
copies of complex objects that would be difficult or time-
consuming to recreate from scratch.
Overall, the main purpose of serialization is to enable the state of
an object to be saved and restored at a later time, which can be
useful for persistence, network communication, caching, and deep
cloning.
Q46. What are the uses of Reflection in Java?

Reflection is a powerful feature in Java that allows you to inspect


and manipulate the runtime behavior of a program. Here are some
of the main uses of reflection in Java:
1. Dynamic loading: Reflection allows you to load classes and
invoke their methods dynamically, without knowing the names of
the classes or methods at compile time. This can be useful for
creating flexible, configurable applications that can load and
execute code at runtime.
2. Debugging: Reflection allows you to inspect the properties and
methods of objects at runtime, which can be useful for debugging
and troubleshooting applications.
3. Introspection: Reflection allows you to examine the structure and
properties of classes and objects at runtime, which can be useful for
building tools and frameworks that need to work with complex,
dynamic data structures.
4. Frameworks: Reflection is used extensively in many Java
frameworks, such as Spring and Hibernate, to provide flexible,
customizable behavior based on the runtime state of the
application.
5. Testing: Reflection can be used in testing frameworks to create and
manipulate objects at runtime, allowing you to test different
scenarios and edge cases that might be difficult to simulate
otherwise.
Q47. What is Garbage Collection in Java?
Garbage Collection in Java is the automatic process of reclaiming
the memory occupied by the objects that are no longer in use by the
program. In Java, the memory is allocated on the heap for the
objects created by the program. However, it is the responsibility of
the JVM to manage the memory on the heap and release it when it
is no longer needed. This is where Garbage Collection comes into
play.
The Garbage Collection process identifies the objects that are no
longer in use by the program and deallocates the memory
associated with them. This process is automatic and transparent to
the developer. The developer does not need to explicitly free the
memory occupied by the objects, as it is done by the Garbage
Collector.
The Garbage Collector works by periodically scanning the heap for
objects that are no longer in use. When it identifies such objects, it
marks them as "garbage" and releases the memory occupied by
them. The Garbage Collector uses different algorithms to identify
the objects that are no longer in use, such as reference counting and
tracing.
One of the main advantages of Garbage Collection in Java is that it
eliminates the need for manual memory management, which can be
error-prone and time-consuming. It also reduces the risk of memory
leaks and other memory-related issues, which can cause the
program to crash or behave unpredictably.
However, Garbage Collection also has some drawbacks, such as the
fact that it can affect the performance of the program by
introducing pauses during the Garbage Collection process. To
mitigate these issues, Java provides various options for tuning the
Garbage Collection process, such as setting the heap size and
choosing different Garbage Collector algorithms.
Q48. What are the different types of garbage collectors in Java?
Garbage collection is Java software that aids in the management of
implicit memory. Because the new keyword in Java allows you to
build dynamically formed objects, which will use memory once
they are created. When the operation is finished and there are no
more references to the object, Java uses garbage collection to delete
it and free up the memory it has taken up. Garbage collectors come
in four types in Java:
 Serial Garbage Collector
 Parallel Garbage Collector
 CMS Garbage Collector
 G1 Garbage Collector
Q49. Why do we use finalize() method in Java?
The method in Java is a method that is invoked by the garbage
collector before an object is reclaimed by the memory. The purpose
of the method is to provide an opportunity for an object to perform
any necessary cleanup actions before it is destroyed.
The method is called by the garbage collector when it determines
that there are no more references to the object. This can happen
when the object is no longer in use by the program or when the
program is terminated.
Some common uses of the method include releasing resources such
as file handles, database connections, or network connections. It
can also be used to perform any necessary cleanup actions that need
to be performed before an object is destroyed, such as closing
streams or freeing memory.
However, it is important to note that the method should be used
with caution. In general, it is not recommended to rely on the
method for important cleanup tasks, as it is not guaranteed to be
called in a timely manner or at all. Instead, it is recommended to
use other mechanisms such as try-finally blocks or the interface to
ensure that resources are properly released.
Furthermore, the use of the method can also negatively impact the
performance of the program, as it can cause delays in the garbage
collection process. Therefore, it is generally recommended to avoid
using the method unless it is absolutely necessary.
Q50. What is the difference between an Inner class and subclass
in Java?
In Java, an inner class and a subclass are two different concepts,
although they share some similarities.
An inner class is a class that is defined inside another class. It has
access to the variables and methods of the outer class, and can be
used to encapsulate related functionality within the outer class.
Inner classes are often used to implement callbacks or event
handlers, as well as to implement data structures such as linked lists
or trees.
On the other hand, a subclass is a class that is derived from another
class, called the superclass. The subclass inherits all the variables
and methods of the superclass, and can also add new variables and
methods or override the ones inherited from the superclass.
The main difference between an inner class and a subclass is that an
inner class is defined within the scope of another class, while a
subclass is defined in a separate file or in the same file as the
superclass but outside its scope.
Another important difference is that an inner class can access the
private variables and methods of its outer class, while a subclass
can only access the protected and public variables and methods of
its superclass.
In summary, an inner class is used to encapsulate related
functionality within an outer class, while a subclass is used to
extend and modify the behavior of a superclass.
Q51. What is Java String Pool?

The Java String Pool is a pool or a cache of String literals stored in


the heap memory. In other words, it is a collection of String objects
that are stored in a special memory area in the heap memory called
the String constant pool.
The String constant pool is a part of the JVM's memory where
String literals are stored. When a String literal is declared in a Java
program, it is stored in the String constant pool instead of the heap
memory. This is done to improve performance and conserve
memory.
When a new String object is created using a String literal, the JVM
first checks the String constant pool to see if the String object
already exists. If it does, the existing object is returned. If it doesn't
exist, a new String object is created and added to the pool.
The String constant pool is a special feature of Java and is used to
optimize the use of memory. Because String objects are used
frequently in Java programs, storing them in a separate memory
area allows for faster access and better performance.
It's important to note that the String constant pool is different from
the regular heap memory used to store other objects. While objects
in the regular heap can be garbage collected when they are no
longer needed, objects in the String constant pool remain there until
the program terminates.
Q52. Why is a string object considered immutable in Java?
A string object is considered immutable in Java because once it is
created, its state cannot be changed. This means that the value of
the private string object cannot be modified, and any attempt to
modify it will result in the creation of a new string object.
There are several reasons why private strings are made immutable
in Java:
1. Security: Because strings are used to store sensitive information
such as passwords or user credentials, making them immutable
ensures that their actual values cannot be changed accidentally or
intentionally, which helps to improve security.
2. Thread-safety: Because strings are immutable, they can be safely
shared among multiple threads without the risk of data corruption
or synchronization issues.
3. Performance: Immutable objects are often more efficient because
they can be cached and reused, which reduces memory usage and
improves performance.
4. Simplifies coding: Immutable strings simplify coding by
eliminating the need to worry about accidental modification or
synchronization issues, which makes code easier to write and
debug.
In summary, the immutability of string objects in Java provides
several benefits in terms of security, thread-safety, performance,
and simplicity, making them a valuable tool in Java programming.
Q53. What is bufferreader in Java?
BufferedReader is a class in Java that is used to read text from a
character-input stream, such as a file or network connection, and
buffer the input for efficient reading.
BufferedReader provides a readLine() method which reads a line of
text from the input stream and returns it as a String. It also provides
other methods for reading individual characters or byte arrays.
The advantage of using BufferedReader is that it reads data from a
file or input stream in a buffered way, which means that it reads
larger chunks of data at once, reducing the number of I/O
operations and improving performance.
Here is an example of using BufferedReader to read text from a file
in Java:
try (BufferedReader br = new BufferedReader(new
FileReader("file.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Q54. What is the basic difference between a String and
StringBuffer object?
The main difference between a String and a StringBuffer object in
Java is that a String object is immutable, while a StringBuffer
object is mutable.
This means that when you create a String object, you cannot
change its contents once it has been created. Any operation that
appears to modify the string actually creates a new String object.
On the other hand, when you create a StringBuffer object, you can
modify its contents without creating a new object. This is because
the StringBuffer class provides methods for appending, inserting,
deleting, and replacing characters in the buffer.
Another important difference is that String objects are thread-safe,
while StringBuffer objects are not. This means that if multiple
threads access a StringBuffer object concurrently, you may get
unexpected results.
Q55. What is the use of the toString() method in Java?
In Java, the method is used to get a string representation of an
object. By default, the method returns a string that includes the
class name, followed by an "at" sign (@), followed by the
hexadecimal representation of the object's hash code.
However, the method can be overridden by a class to provide a
more meaningful string representation of the object. For example, a
class might override to return a string that includes the person's
name and age.
The method is commonly used in Java to print objects to the
console or to concatenate objects into strings. Many Java classes,
such as and , override to provide a string representation of their
contents.
In addition, some Java libraries and frameworks, such as Jackson
and Gson, use to serialize Java objects to JSON or other formats.
By providing a custom method, a class can control how it is
represented in the serialized output.
56. What is the difference between String Buffer and
StringBuilder?
Below are the key differences between StringBuffer and
StringBuilder

No. StringBuffer StringBuilder

StringBuffer is StringBuilder is
thread-safe not thread-safe
since it is since it is not
synchronized. It synchronized. It
indicates that indicates that
1)
two threads two threads can
can't call the call
StringBuffer StringBuilder's
functions at the methods at the
same time. same time.

StringBuffer StringBuilder
is less is more
2)
efficient than efficient than
StringBuilder. StringBuffer.
In Java 1.0, the In Java 1.5, the
StringBuffer StringBuilder
3)
class was class was
introduced introduced.

57. What is the difference between an Error and an Exception?

Here's a table summarizing the key differences between and in


Java:

Error Exception

Extends the class Also extends the class

Indicates a severe
Indicates a less severe
system-level error
error that can be
that should not be
caught and potentially
caught or recovered
recovered from
from

Examples include , , Examples include , ,

Typically caused by Typically caused by


Error Exception

incorrect usage of a
circumstances
library or API, or
outside the control
incorrect program
of the program
logic

Generally indicates a
Generally indicates a
serious problem that
recoverable problem
may require
that can be handled by
intervention by a
the program itself
system administrator

Usually can be
handled or recovered
Usually cannot be from within the
handled or recovered program, either by
from within the catching the exception
program or allowing it to
propagate up the call
stack

In summary, indicates a severe system-level error that generally


cannot be handled within the program, while indicates a less severe
error that can often be caught and handled within the program.
58. In Java, what are the differences between a Checked and an
Unchecked Exception?
 Checked Exception: Checked exceptions are all classes that
extend the Throwable class except RuntimeException and Error.
Checked exceptions are checked during the compilation process.
IOException, SQLException, and other exceptions are examples.
 Unchecked Exception: Unchecked exceptions are the classes that
extend RuntimeException. At compilation time, unchecked
exceptions are not checked. ArithmeticException,
NullPointerException, and other exceptions are examples.
59. What are the differences between 'throw' and 'throws'?
Below are the key differences between throw and throws keyword:

throw
throws keyword
keyword

To explicitly
throw an For method declaration; to
exception, use declare an exception, use
the throw the throws keyword.
keyword.

Throwing only
will not Throws can be used to
propagate propagate checked
checked exceptions.
exceptions.

Throw is
Throws is followed by
followed by an
class.
instance.

Throw is used The method signature


within the includes the keyword
method. throws.

You cannot Multiple method


declaration- many
exceptions can be declared,
throw multiple for example, public void
exceptions. method() throws
IOException,
SQLException.

60. What is the use of finally block in Java?


A finally block is always performed in Java, according to the
standard, whether or not an error happens, or if an exception is
handled or not handled. It aids in the cleaning of transactions such
as rollbacks and closes. Make a connection, close a file, and so
forth.
61. What is the exception hierarchy in Java?
In Java, the class is the root of the exception hierarchy. The class
has two main subclasses:
1. : This class represents serious problems that a reasonable
application should not try to catch. Examples of include and .
2. : This class represents problems that can be caught and handled by
an application. The class has many subclasses that are used to
represent different types of exceptions, such as and .
The class is a subclass of and represents exceptions that can occur
during the normal operation of the JVM. Examples of include , ,
and .
62. Do we have to always put a catch block after a try block?
No, it's not always necessary to put a catch block after a try block
in Java. However, if a try block is used, then one of the following
must be included:
1. A catch block: This is used to handle any exceptions that may be
thrown within the try block. The catch block must specify the type
of exception it is catching, and can include code to handle the
exception.
2. A finally block: This is used to ensure that certain code is executed
regardless of whether an exception is thrown or not. The finally
block is always executed, even if an exception is thrown and caught
by a catch block.
It's also possible to use both a catch block and a finally block
together.
However, if neither a catch block nor a finally block is used after a
try block, then the code will not compile. In this case, the try block
is unnecessary and can be removed.
63. How Multi-threading works in Java?

In Java, multi-threading allows multiple threads of execution to run


concurrently within a single program. Each thread is an
independent path of execution that runs concurrently with other
threads in a program, allowing for better utilization of CPU
resources and improved application performance.
Java implements multi-threading using the class and the interface.
The class represents a single thread of execution, and the interface
defines the method that is executed when a thread is started.
To create a new thread in Java, you can either extend the class and
override its method, or you can implement the interface and pass an
instance of the implementation to a constructor. Once a thread is
created, it can be started by calling its method. This causes the
method to be executed on a separate thread of execution.
Java provides various methods for controlling the execution of
threads, such as , , and . These methods allow threads to
synchronize and communicate with each other, as well as pause and
resume their execution.
However, multi-threading can also introduce concurrency issues,
such as race conditions and deadlocks, if not properly managed. It's
important to use synchronization and other thread-safe
programming techniques to avoid these issues and ensure correct
program behavior.
64. What is a Thread? What are the two ways to create a
thread?
In Java, a thread is an independent path of execution within a
program. A thread represents a single sequential flow of control
within a program's execution.
There are two ways to create a thread in Java:
1. By extending the class: You can create a new thread by extending
the class and overriding its method to define the code that will be
executed by the thread. You can then create an instance of the
subclass and start the thread using the method.
2. public class MyThread extends Thread {
3. public void run() {
4. // code to be executed by the thread
5. }
6. }
7.
8. // create and start a new thread
9. MyThread thread = new MyThread();
thread.start();
10. By implementing the interface: You can also create a new
thread by implementing the interface and defining the code to be
executed by the thread in the method. You can then create an
instance of the implementation and pass it to a new object, which
can be started using the method.
11. class Animal {
12. public Animal getAnimal() {
13. return this;
14. }
15. }
16.
17. class Cat extends Animal {
18. public Cat getAnimal() {
19. return this;
20. }
}
Both methods are equally valid, but implementing the interface is
often preferred over extending the class, as it allows for greater
flexibility and better separation of concerns. Additionally, Java only
allows single inheritance, so extending the class may not be
possible in certain situations where you need to inherit from
another class.
65. What is a Thread’s priority and how it is used in
scheduling?
In Java, a thread's priority is an integer value that represents the
relative importance of the thread to the thread scheduler. Thread
priority values range from 1 (lowest priority) to 10 (highest
priority), and can be set using the method of the class.
When multiple threads are ready to execute, the thread scheduler
selects one thread to execute based on its priority and other factors
such as the availability of system resources. Generally, threads with
higher priority values are more likely to be selected for execution
before threads with lower priority values. However, the actual
behavior of thread scheduling is dependent on the underlying
operating system and JVM implementation and may vary.
It's important to note that thread priority is not a guarantee of
execution order or behavior, and should not be relied on to ensure
correct program behavior. In addition, setting thread priority to
maximum or minimum values may cause unexpected behavior or
performance issues.
In general, thread priority should be used judiciously and only
when necessary. In most cases, it is better to write code that is
independent of thread priority and can execute correctly regardless
of the scheduling order of threads.
66. Explain the thread lifecycle in Java?

In Java, a thread goes through several states during its lifecycle,


which are represented by the enumeration. The possible states are:
1. NEW: A thread is in this state when it has been created but not yet
started with the method.
2. RUNNABLE: A thread is in this state when it has been started and
is ready to run, but may or may not be currently executing
depending on the thread scheduler.
3. BLOCKED: A thread is in this state when it is waiting for a
monitor lock to be released by another thread.
4. WAITING: A thread is in this state when it is waiting indefinitely
for another thread to perform a specific action.
5. TIMED_WAITING: A thread is in this state when it is waiting for
a specific period of time for another thread to perform a specific
action.
6. TERMINATED: A thread is in this state when it has completed
execution either by reaching the end of its method or by being
terminated prematurely.
The transition between these states is controlled by the JVM and
the thread scheduler. When a thread is created with the keyword, it
is in the state. After calling the method, the thread transitions to the
state and is added to the thread scheduler's queue of ready threads.
When a thread is executing, it can transition back and forth
between the state and other states such as , , and , depending on the
actions of other threads and the availability of system resources.
Finally, when a thread completes its execution, it transitions to the
state and is removed from the thread scheduler's queue.
It's important to note that the exact behavior and timing of thread
state transitions is dependent on the underlying operating system
and JVM implementation, and may vary. In addition, thread
synchronization and communication mechanisms such as , , and
can affect the state transitions of threads.
67. What is synchronization?
In Java, synchronization refers to the coordination of multiple
threads to ensure that they access shared resources (such as
variables or objects) in a safe and predictable manner. When
multiple threads access a shared resource simultaneously, it can
lead to race conditions, where the result of the program depends on
the order in which the threads execute. Synchronization helps to
prevent race conditions by enforcing mutual exclusion: only one
thread can access the shared resource at a time.
Java provides several mechanisms for synchronization, including
the keyword, which can be used to create synchronized blocks or
methods. When a thread enters a synchronized block or method, it
acquires the lock associated with the object that the block or
method is synchronized on. Other threads that try to access the
same synchronized block or method are blocked until the lock is
released by the first thread.
Synchronization can help to ensure that a program is thread-safe,
meaning that it works correctly and predictably even when multiple
threads are executing concurrently. However, synchronization can
also introduce overhead and potential performance issues,
especially when multiple threads are contending for the same lock.
As a result, it is important to use synchronization judiciously and
only when necessary.
68. What is a collection class in Java? List down its methods
and interfaces
The collection is a framework in Java that works as an architecture
for storing and managing a collection of objects. You may use
collections to conduct a variety of operations like searching,
sorting, insertion, manipulation, and deletion. The following are
some of the features of the Java collection framework:
 Classes
 Methods
 Interfaces
The Java Collection hierarchy is depicted in the graphic below.

69. What is the difference between Collection and Collections


Framework in Java?
A Collection is a Java object that holds several elements of the
same type in one unit. A single Collection object can access all of
these aspects.
Collections Framework in Java is a library that provides a standard
architecture for building, updating, and accessing many sorts of
collections. There are standard ways for working on a Collection
object in the Collections framework that is often used by
developers.
70. What are the differences between a List and a Set collection
in Java?

List Set

The list The set


implementation implementation
allows us to add doesn't allow us
1.
the same or to add the same
duplicate or duplicate
elements. elements.

The insertion It doesn't


order is maintain the
2.
maintained by insertion order of
the List. elements.

List allows us to Set allows us to


3. add any number add at least one
of null values. null value in it.

4. The List The Set


implementation implementation
classes are
classes are
TreeSet,
LinkedList and
HashSet, and
ArrayList.
LinkedHashSet.

71. In Java, how will you decide when to use a List, Set, or a
Map collection

In Java, you would choose to use a , , or collection based on the


requirements of your specific use case. Here are some guidelines to
help you decide which collection to use:
 List: Use a when you need to maintain elements in a specific order
and allow duplicates. is an ordered collection that allows you to
access elements by their index. If you need to add, remove, or
access elements frequently by their position in the list, a is a good
choice. Examples of implementations include , , and .
 Set: Use a when you want to maintain a unique set of elements
without duplicates. does not allow duplicates and does not maintain
order. If you need to check whether an element is in the collection,
or if you need to add and remove elements frequently without
worrying about their order, a is a good choice. Examples of
implementations include , , and .
 Map: Use a when you need to associate keys with values, and you
want to look up values based on their keys. allows you to store key-
value pairs, and you can look up the value of a specific key quickly.
If you need to add, remove, or look up values frequently based on
their keys, a is a good choice. Examples of implementations
include , , and .
72. What is the difference between ArrayList and vector in
Java?
Below are the key differences between ArrayList and vector

ArrayList Vector

There is no
The vector is
synchronization in the
synchronized.
Array List.

Because it is non- Because Vector is


synchronized, the Array thread-safe, it is
List is quick. sluggish.

When an element is
By default, a
added to the Array List,
vector's array is
it increases its Array
doubled in size.
size by 50%.

The increment size is The increment size


not specified in the is defined by a
Array List. vector.

Array List can only use Vector can use both


an iterator for Enumeration and
traversing an Array Iterator for
List. traversing.
73. What are the differences between Comparable and
Comparator?
and are two interfaces in Java that are used to sort objects in a
collection based on a specific ordering. The main differences
between the two are as follows:
1. Implementation: The interface is implemented by the class whose
objects need to be sorted, while the interface is implemented by a
separate class that defines the ordering of objects.
2. Method: The interface has one method, , which compares the
current object with another object of the same class. The interface
has one method, , which takes two objects of any class and
compares them based on a specific ordering defined by the
comparator.
3. Natural ordering vs custom ordering: The interface provides a
natural ordering for the objects based on the class's internal
definition, while the interface provides a custom ordering that can
be defined by the programmer.
4. Flexibility: The interface provides more flexibility than because it
allows multiple ways of sorting objects, depending on the specific
needs of the program. For example, you can define multiple
comparators for the same class to sort objects in different ways.
74. What are the differences between a HashMap and a
Hashtable in Java?
Below are the key differences between HashMap and HashTable

HashMap Hashtable

HashMap is not a A Hashtable is a


synchronized synchronized
collection. If it is used collection. Not more
in a multi-thread than one thread can
environment, it may access a Hashtable
not provide thread at a given moment
safety. in time.

One null key and There is no such


numerous null values thing as a null key
are allowed in or value in a
HashMap hashtable.

HashMap is a new
Hashtable is
class introduced in
a legacy class.
JDK 1.2.

A HashMap
A TreeMap sorts the
implementation by
mappings based on
LinkedHashMap
the ascending order
maintains the insertion
of keys.
order of elements

Enumerator and
Iterator traverses the
Iterator traverse the
HashMap.
hashtable.

HashMap Hashtable
inherits AbstractMap inherits Dictionary
class. class.

75. What are the differences between a HashMap and a


TreeMap?
Below are the key differences between HashMap and TreeMap
HashMap TreeMap

The Java HashMap Java TreeMap is a


implementation of Map interface
the Map interface is implementation based
based on hashtables on a Tree structure.

HashMap TreeMap
implements Map, implements Navigabl
Cloneable, eMap, Cloneable,
and Serializable inte and Serializable inter
rfaces. faces.

TreeMap does not


A single null key and
allow null keys,
numerous null values
however, multiple
are allowed in
null values are
HashMap
allowed.

HashMap allows
TreeMap allows
heterogeneous
homogeneous actual
elements because it
values as a key
does not perform
because of sorting.
sorting on keys.

The HashMap class TreeMap internally


uses the hash table. uses a Red-
Black tree, which is a
self-balancing Binary
Search Tree.

A TreeMap uses
A HashMap uses
compareTo() method
equals() method to
for maintaining
compare keys.
natural ordering.

A HashMap gives
A TreeMap gives the
constant time
order of log(n) time
performance for
performance for get()
operations like get()
and put() methods.
and put().

76. What are Wrapper classes in Java?

Wrapper classes are used in Java to allow primitive types to be


accessed as objects. Boolean, Integer, Double, Float, and other
primitive types have corresponding Wrappers classes – Boolean,
Integer, Double, Float, and so on. The automatic conversion of
primitive data types into their equivalent Wrapper type is known as
boxing and the opposite operation is known as unboxing.
The java.lang. package contains many of these Wrapper classes.
In Java 5.0, the idea of Autoboxing and Unboxing for Wrapper
classes was introduced.

77. What is the purpose of the native method in Java?


In Java, the native method is a method that is implemented in a
platform-dependent manner, typically in a non-Java programming
language such as C or C++. The purpose of the native method is to
provide a way to access system-level resources or to perform
operations that are not possible or efficient in Java.
Java's platform-independent nature is one of its key features, but
there are certain situations where access to system-level resources
or native code is required. For example, a native method might be
used to interact with hardware devices, such as a printer or a
camera, or to perform complex mathematical calculations that are
not easily done in Java.
Native methods are declared using the keyword, and their
implementation is typically provided in a shared library or dynamic
link library (DLL) that is loaded at runtime. When a native method
is called, the Java Virtual Machine (JVM) transfers control to the
native implementation, which then executes the code using system-
level resources.
Native methods are often used in conjunction with the Java Native
Interface (JNI), which provides a way for Java code to interact with
code written in other programming languages. JNI allows Java to
call native methods and provides a way for native code to call Java
code.
78. What is the difference between Shallow Copy and Deep
Copy in Java?

In Java, shallow copy and deep copy are two different techniques to
copy objects. The main difference between shallow copy and deep
copy is that shallow copy only creates a new object that references
the original object's memory location, while deep copy creates a
new object with a new memory location that has the same values as
the original object.
In a shallow copy, the new object that is created shares the same
memory location as the original object, so any changes made to the
original object will also be reflected in the new object. Shallow
copy is performed using the clone() method or copy constructor,
which creates a new object and copies the references to the data
members of the original object.
In contrast, in a deep copy, a new object is created with its own
memory location and all the data members are copied to the new
object. Deep copy is performed by manually copying all the fields
of the original object into the new object or by implementing the
Serializable interface and using object serialization to create a deep
copy.
To summarize, the key difference between shallow copy and deep
copy in Java is that shallow copy creates a new object that
references the same memory location as the original object, while
deep copy creates a completely new object with its own memory
location and values that are equal to the original object's values to
maintain code reusability.
79. What is the difference between Collection and Collections
Framework in Java?
In Java, Collection and Collections Framework are related concepts
but have different meanings.
Collection is an interface in the Java programming language that
represents a group of objects, where each object is a separate entity
and can be accessed and manipulated independently of the others.
Examples of Collection classes in the Java Collections Framework
(JCF) include List, Set, and Queue.
On the other hand, the Collections Framework in Java refers to a
set of interfaces, classes, and algorithms provided by the Java
library to manipulate collections of objects. The Collections
Framework includes several core interfaces such as Collection,
List, Set, Queue, Map, and others, as well as their implementing
classes such as ArrayList, HashSet, and TreeMap.
80. Why Map interface does not extend the Collection interface
in the Java Collections Framework?
The Map interface in the Java Collections Framework (JCF) does
not extend the Collection interface because it represents a different
kind of data structure than the Collection interface.
A Collection represents a group of objects, where each object is a
separate entity and can be accessed and manipulated independently
of the others. Examples of Collection classes in the JCF include
List, Set, and Queue.
On the other hand, a Map represents a mapping between keys and
values, where each key is associated with a single value. The key-
value pairs in a Map are not considered separate entities that can be
accessed or manipulated independently of each other. Examples of
Map classes in the JCF include HashMap, TreeMap, and
LinkedHashMap.
Because Maps and Collections represent different kinds of data
structures with different characteristics and behaviors, it would not
make sense for Map to extend Collection or vice versa. However,
both interfaces do share some common methods, such as size() and
isEmpty(), which are inherited from the parent interface, Iterable.
81. What is the difference between an Iterator and a
ListIterator in Java?
Here is a table that summarizes the main differences between and
interfaces in Java:

Iterator ListIterator

Used to iterate over


Used to iterate over a
a collection of
list of objects.
objects.

Can only traverse a Can traverse a list in


collection in a both forward and
Iterator ListIterator

forward direction. backward directions.

Provides methods to
Provides methods check if the iteration
to check if the has more elements in
iteration has more both forward and
elements, to backward directions, to
retrieve the next retrieve the next and
element, and to previous elements, and
remove the current to add and remove
element. elements at the current
position.

Cannot modify the


Can modify the
underlying
underlying list during
collection during
iteration.
iteration.

Implemented by the
Implemented by
interface, which
the interface.
extends .

In summary, an is used to traverse any collection in a forward


direction and provides a limited set of methods to retrieve and
remove elements, while a is used specifically for traversing a list in
both directions and provides additional methods to modify the list
while iterating.
82. What is the difference between a Set and a Map in Java?
In Java, a Set and a Map are both collection interfaces, but they
have some fundamental differences in their usage and behavior.
A Set is a collection that contains unique elements. In other words,
it does not allow duplicate elements. The elements in a Set are not
ordered, which means they are not stored in any particular order.
The most commonly used implementation of the Set interface is the
HashSet, which uses a hash table to store the elements. Other
implementations of Set include TreeSet and LinkedHashSet.
On the other hand, a Map is a collection that stores key-value pairs.
It allows you to retrieve a value based on its corresponding key.
Unlike a Set, a Map can contain duplicate values, but it cannot
contain duplicate keys. The elements in a Map are not ordered by
default. The most commonly used implementation of the Map
interface is the HashMap, which uses a hash table to store the key-
value pairs. Other implementations of Map include TreeMap and
LinkedHashMap.
83. What is Hash Collision? How Java handles hash-collision in
HashMap?
Hash Collision occurs when two or more keys in a hash table get
the same hash value, resulting in a collision. Java's uses an array of
buckets to store key-value pairs. The hash function is used to map
each key to an index in the array. If two keys have the same hash
code, they will be stored in the same bucket.
To handle hash collisions in Java's , the bucket uses a linked list
data structure. When a collision occurs, the new key-value pair is
added to the bucket's linked list. When retrieving a value, the hash
code of the key is used to find the appropriate bucket, and the
linked list in that bucket is searched for the key.
In Java 8 and later, if a bucket's linked list grows too long, the list is
converted into a balanced tree to improve performance. This is
called treeification, and it reduces the search time for keys with
hash collisions from O(n) to O(log n).
84. What is the difference between Queue and Stack data
structures?

Queue and Stack are both data structures in Java, but they differ in
their ordering of elements and the methods used to access those
elements.
A Queue is a collection of elements in which an element is added to
the end of the queue and removed from the beginning of the queue.
It follows the "first-in, first-out" (FIFO) principle. This means that
the first element that is added to the queue will be the first one to be
removed. In Java, the Queue interface is implemented by classes
like LinkedList, PriorityQueue, and ArrayDeque.
On the other hand, a Stack is a collection of elements in which an
element is added and removed from the top of the stack. It follows
the "last-in, first-out" (LIFO) principle. This means that the last
element that is added to the stack will be the first one to be
removed. In Java, the Stack class is available in the java.util
package.
To summarize, the main difference between a Queue and a Stack is
the order in which elements are added and removed. A Queue
follows the FIFO principle, whereas a Stack follows the LIFO
principle.
85. What is a classloader?
In Java, a class loader is a part of the Java Runtime Environment
(JRE) that loads compiled Java classes into the Java Virtual
Machine (JVM). It is responsible for loading classes from their
bytecode representation and linking them into a running program.
The JVM has three built-in class loaders:
1. Bootstrap Class Loader: It is responsible for loading the core Java
classes, such as java.lang, java.util, etc. It is implemented in native
code and is a part of the JVM.
2. Extension Class Loader: It loads classes from the extension
directory, which is defined by the java.ext.dirs system property. It is
implemented in Java and is a child of the bootstrap class loader.
3. Application Class Loader: It loads classes from the classpath,
which is defined by the java.class.path system property. It is
implemented in Java and is a child of the extension class loader.
Class loaders provide a way to dynamically extend the functionality
of Java applications by allowing new classes to be loaded at
runtime. They also provide a level of security by separating the
classes of different applications or modules.
86. What is the covariant return type?
The covariant return type is a feature introduced in Java 5 that
allows a subclass method to return a subtype of the return type of
the overridden method in the superclass. In simple terms, it allows
a method in a subclass to return a more specific type than the
method it overrides in the superclass.
For example, consider a class hierarchy where is a superclass and is
a subclass. Suppose has a method called that returns an object. If
we override this method in the class and return a object instead,
then we are using covariant return types.
Here is an example:
class Animal {
public Animal getAnimal() {
return this;
}
}

class Cat extends Animal {


public Cat getAnimal() {
return this;
}
}
87. How to create an immutable class in Java?
To create an immutable class in Java, we need to follow the below
steps:
1. Make the class final so that it cannot be extended.
2. Make all the fields private and final so that they cannot be
modified.
3. Do not provide any setters for the fields.
4. If the class has mutable fields, then make sure to return a deep copy
of the fields in the getter methods.
5. Provide a public constructor to initialize the final fields.
6. Override the and methods to ensure that the object can be used in
collections such as HashSet or HashMap.
7. Do not provide any methods that can change the state of the object.
Example:
public final class ImmutablePerson {
private final String name;
private final int age;

public ImmutablePerson(String name, int age) {


this.name = name;
this.age = age;
}
public String getName() {
return name;
}

public int getAge() {


return age;
}

// Override equals() and hashCode() methods

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImmutablePerson that = (ImmutablePerson) o;
return age == that.age && Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
88. Can you have virtual functions in Java?
Java does not have a virtual keyword like C++ to indicate that a
method should be dynamically bound at runtime. However, in Java,
all non-private, non-static, and non-final methods are by default
virtual functions, which means that the actual method to be called
is determined at runtime based on the actual type of the object.
This is because Java uses dynamic method dispatch to resolve the
appropriate method to be executed at runtime. When a method is
invoked on an object, the JVM determines the actual type of the
object and then looks up the appropriate method implementation
for that type at runtime.
So, in Java, all non-private, non-static, and non-final methods can
be considered virtual functions, since their actual implementation is
determined at runtime based on the type of the object they are
called on.
89. Mention the uses of the synchronized block
In Java, the block is used to ensure that only one thread at a time
can execute a critical section of code. The block uses a monitor
lock to achieve this, which allows only one thread at a time to
acquire the lock and execute the code enclosed in the block.
Here are some of the common uses of the block in Java:
1. Thread safety: The block is commonly used to ensure that access
to shared resources is synchronized across multiple threads. This
helps prevent race conditions and other synchronization issues that
can occur when multiple threads access the same resource
concurrently.
2. Deadlock prevention: The block can also be used to prevent
deadlocks in multi-threaded applications. By ensuring that only one
thread at a time can acquire a lock on a shared resource, the
likelihood of two threads becoming deadlocked is reduced.
3. Performance optimization: In some cases, the block can be used
to optimize performance in multi-threaded applications. For
example, by synchronizing access to a resource only when
necessary, the overhead of locking and unlocking the resource can
be reduced, improving the overall performance of the application.
Overall, the block is a powerful tool for synchronizing access to
shared resources in multi-threaded applications, and is commonly
used in Java programs to ensure thread safety and prevent
synchronization issues.
90. Distinguish between static loading and dynamic class
loading?
In Java, class loading is the process of loading a Java class into
memory at runtime. There are two types of class loading: static
loading and dynamic loading.
1. Static loading: In static loading, classes are loaded at compile
time, and the Java compiler includes them in the byte code of the
application. This means that all the classes required by the
application are loaded into memory at startup, even if they are not
used during the execution of the program. Static loading is the
default behavior in Java.
2. Dynamic loading: In dynamic loading, classes are loaded at
runtime when they are needed. Dynamic loading is useful in
situations where classes are large or rarely used because it reduces
the memory footprint of the application. Dynamic loading is
achieved using the Java method or by creating an instance of the
class. When the method is called, the class is loaded into memory
and initialized. When the class is used, the class is loaded into
memory, but not initialized until it is used.
Overall, static loading and dynamic loading are two different
approaches to class loading in Java, and both have their own
advantages and disadvantages. Static loading is simpler and faster,
but requires more memory. Dynamic loading is more flexible and
efficient, but requires more complex programming techniques.
91. What are the different scenarios causing "Exception in
thread main"?
The following are some examples of frequent main thread
exceptions:
 Exception in thread main
java.lang.UnsupportedClassVersionError: This exception occurs
when you try to run a java class that was compiled with a different
JDK version.
 Exception in thread main
java.lang.NoClassDefFoundError: This exception comes in two
flavors. The first is where you give the whole name of the class,
including the.class extension. When Class is not identified, the
second situation occurs.
 Exception in thread main java.lang.NoSuchMethodError:
main: When you try to launch a class that doesn't have a main
function, you'll get this exception.
 Exception in thread "main"
java.lang.ArithmeticException: When an exception is thrown
from the main method, the exception is sent to the console. The
first section specifies that the main method throws clause an
custom exception, the second part publishes the custom exception
class name, and the third part displays the exception message after
a colon.
92. Explain the externalizable interface.

In Java, the interface is an extension of the interface that provides a


way to customize the serialization and deserialization process for
an object. Unlike the interface, which uses a default mechanism for
serialization, the interface requires that the class explicitly
implement the serialization and deserialization methods.
The interface defines two methods that must be implemented by the
class:
1. : This method is called during serialization and is responsible for
writing the object's state to the specified stream. The order in which
the object's state is written is important because it must match the
order in which the state is read during deserialization.
2. : This method is called during deserialization and is responsible for
reading the object's state from the specified stream. The order in
which the object's state is read must match the order in which the
state was written during serialization.
By implementing the interface, the class can provide custom logic
for serialization and deserialization, such as encrypting or
decrypting sensitive data or compressing or decompressing large
data objects.
It's important to note that objects that implement the interface must
have a public no-argument constructor, which is used during
deserialization to create a new instance of the object.
Overall, the interface provides a way to customize the serialization
and deserialization process for Java objects, giving developers
greater control over how their objects are stored and transmitted.
93. Can we execute any code, even before the main method?
Yes, we may run any code before calling the main function. When
constructing objects at class load time, we will use a static piece of
code in the class. Any statements included in this static block of
code block will be performed at the same time as the class is
loaded, even before any objects are created in the main function.
public class Main {
static int k;
static {
System.out.println("Inside static block");
k = 10;
}
public static void main(String[] args) {
System.out.println("Inside main method");
System.out.println("Value of k: " + k);
}
}
 Inside static block
 Inside main method
 Value of k: 10
94. What is the difference between transient and volatile
variables in Java?
 Transient: The transient modifier instructs the Java object
serialization subsystem to skip the field while serializing a class
instance. The field will be initialized to the default value when the
object is deserialized, which is null for a reference type and zero or
false for a primitive type.
 Volatile: The volatile variable modifier instructs the JVM that
updates to the field should always be synchronously flushed to
memory, while reads should always be read from memory. This
implies that fields defined as volatile in a multi-thread program can
be reliably accessed and modified without the need for native or
standard library-based synchronization.
95. What is a functional interface in Java?
In Java, a functional interface is an interface that contains exactly
one abstract method. Functional interfaces are also known as
"SAM" interfaces, which stands for Single Abstract
Method interfaces.
Functional interfaces are used to represent functions as objects in
Java. They can be used with lambda expressions or method
references to create compact, expressive code for functional
programming tasks, such as event handling or processing
collections of data.
Java 8 introduced the annotation, which can be used to mark an
interface as a functional interface. This annotation is not strictly
necessary, but it helps to make the intent of the interface clear and
enables the Java compiler to check that the interface satisfies the
requirements of a functional interface.
Functional interfaces can be found in many places in the Java API,
such as the package, which provides a set of commonly used
functional interfaces, such as , , and .
Overall, functional interfaces are an important part of modern Java
programming and enable developers to write concise and readable
code for functional programming tasks.
96. What are the uses of the Java super keyword?
In Java, the keyword is used to refer to the parent class or
superclass. It can be used in various ways to access or invoke
members of the parent class. Some of the common uses of the
keyword are:
1. Invoking a parent class constructor: The keyword can be used to
call a constructor in the parent class. This is useful when the
subclass needs to initialize some of the parent class's fields before
continuing with its own initialization.
2. Accessing parent class members: The keyword can be used to
access the methods and fields of the parent class that are not visible
to the subclass due to access static modifiers. This is useful when
the subclass needs to override a method or field from the parent
class but still needs to access the parent class's version of the
method or field.
3. Overriding parent class methods: The keyword can be used in a
subclass to call a method in the parent class that has been
overridden by the subclass. This is useful when the subclass needs
to extend or modify the behavior of the parent class's method.
4. Invoking parent class static members: The keyword can be used
to access static members of the parent class from a subclass. This is
useful when the subclass needs to reference a static method or field
of the parent class.
Overall, the keyword is a powerful tool in Java that allows
subclasses to access and modify the behavior of their parent
classes. It is commonly used in object-oriented programming to
implement inheritance and code reuse.
97. What is method overloading with type promotion?
Method overloading with type promotion means, when no
matching parameter type of method is found, it implicitly promotes
the data type to the next level and calls that corresponding method.
For example, if we call a method with a short parameter data type
and if there is no matching method with a short type but if Int is
present, then it calls the method with Int parameter datatype.

class Main{
void show(int a){
System.out.println("int method");
}
void show(String a){
System.out.println("string method");
}
public static void main(String[] args) {
Main s = new Main();
s.show('a');
}
}
98. What is the Java instanceOf operator?
Java instanceOf operator is a comparison operator that checks if the
object is an instance of some type of class. The return value is
either true or false. If there is no value, then it returns false. Here is
the code for the same:
99. What is the use of the System class and Runtime class?
The and classes in Java are used to interact with the operating
system and perform system-level tasks.
The class provides a set of static methods and variables that allow
you to access and modify system properties, environment variables,
and standard input/output streams. Some common uses of the class
include:
 Getting and setting system properties (e.g., and )
 Accessing environment variables (e.g., )
 Reading from and writing to the standard input/output streams
(e.g., and )
The class provides a way to execute external processes and manage
the Java Virtual Machine (JVM) environment. Some common uses
of the class include:
 Running external processes (e.g., )
 Terminating the JVM (e.g., )
 Getting the amount of free and total memory available to the JVM
(e.g., and )
Both the and classes are part of the Java core API and are widely
used in Java applications to perform system-level tasks.
100. What is the difference between abstraction and
encapsulation?

Abstraction Encapsulation

Abstraction is the
The technique of
process of hiding
encapsulating code
implementation
and data into a single
details from the user
unit is known as
and only displaying
encapsulation.
functionality.

Abstraction allows Encapsulation gives


you to concentrate on you control over your
the object's function data while also
rather than how it protecting it from
performs it. outside threats.

The problem at the


At the Design Level, Implementation
abstraction solves the Level is solved
problem. through
encapsulation.

Interfaces and Access Static


Abstract Classes are Modifiers are used to
implement
used to implement encapsulation
abstraction. (private, default,
protected, public)

Abstraction refers to
Encapsulation refers
the use of interfaces
to the use of setters
and abstract classes to
and getters to hide
hide implementation
data.
difficulties.

101. What are the different types of access specifiers in Java?

There are four different types of access specifiers in Java:


 public: Any class in any package can see it. For a class, variable,
or method, we can declare a public access type.
 protected: It can be accessed from any class in the same package
or from the subclass of the declared class. We can gain access to the
outside world through inheritance.
 default: The scope is contained within the package and does not
require the use of any keywords.
 private: It can only be accessible by members of the same class.
Java's easy-to-use syntax and built-in capabilities, as well as the
reliability it gives to applications, are the primary reasons for its
growing popularity in the software community. These 101
questions will cover the important topics of Java and will surely
help you in acing the java interview.
102. What is the difference between the equals () method and
the equality operator (==) in Java?
They are used for comparison, however, there are some
fundamental differences:
 Equals () method: This is used to compare the actual content of
the object and this type of method cannot be used with primitives.
This method can compare conflicting objects utilizing the equals ()
method and returns “false”. The equals () method can be
overridden.
 Equality operator (==): This can be used to compare the actual
objects and reference values. You can also employ this operator
with actual objects and non-primitive data types. However, this
operator cannot compare conflicting objects. Therefore, the time
compiler surrounds the compile-time error. This operator cannot be
overridden.
103. What is Stack Memory?
In Java, stack memory is a region of memory used by the JVM to
store method-specific variables and method call information. Each
thread in a Java application has its own stack, which is created
when the thread is started and destroyed when the thread
completes.
The stack memory is used to store local variables, method
arguments, and method call frames, which are used to keep track of
the current method call and its state. When a method is called, a
new stack frame is created on the stack to store the method's local
variables and arguments. When the method completes, the stack
frame is popped off the stack, and its memory is freed.
The size of the stack memory is determined at the time the JVM is
started and can be configured using the command-line option. By
default, the size of the stack memory is relatively small, typically a
few hundred kilobytes to a few megabytes, depending on the JVM
implementation and platform.
It's important to note that stack memory is different from heap
memory, which is used to store actual objects and other data that is
not method-specific. The stack memory is generally faster than the
heap memory because it uses a last-in first-out (LIFO) data
structure that is optimized for rapid allocation and deallocation of
memory. However, the stack memory is also more limited in size
and cannot be used to store large amounts of data.
104. What is Double Brace Initialisation?
Double brace initialization is a technique in Java that allows you to
create and initialize a collection in a single statement using nested
braces. The outer braces define an anonymous subclass of the
collection type, and the inner braces define an initializer block that
adds elements to the collection. Here's an example:
List<String> names = new ArrayList<String>() {{
add("Alice");
add("Bob");
add("Charlie");
}};
In this example, we create a new and add three objects to it using
the double brace initialization syntax. The outer braces create an
anonymous subclass of , and the inner braces define an initializer
block that calls the method three times to add the objects.
While double brace initialization can be useful for creating and
initializing collections in a concise way, it has some drawbacks.
One potential issue is that it creates a new subclass of the collection
type for each initialization, which can lead to increased memory
usage and slower performance. Additionally, the use of anonymous
subclasses can make the code more difficult to read and understand,
particularly for developers who are not familiar with the technique
hence decreasing the code reusability.
105. Give an instance of dependency injection.
Dependency injection (DI) is a technique used in software
engineering to reduce the coupling between different components
of a system. It involves injecting the dependencies of an object
rather than allowing the object to create them itself. Here's an
example of dependency injection in Java:
public class Car {
private Engine engine;

public Car(Engine engine) {


this.engine = engine;
}

public void start() {


engine.start();
}
}

public interface Engine {


void start();
}

public class GasEngine implements Engine {


public void start() {
System.out.println("Starting Gas Engine");
}
}

public class ElectricEngine implements Engine {


public void start() {
System.out.println("Starting Electric Engine");
}
}

public class Main {


public static void main(String[] args) {
// Create a GasEngine object
Engine engine = new GasEngine();

// Create a Car object and inject the GasEngine dependency


Car car = new Car(engine);

// Start the car


car.start();
}
}
Practicing these top 100+ Java interview questions and answers
will definitely sharpen your knowledge of the subject matter and
prep you well to deal with the curveballs your interviewer will
throw at you. If you wish to gain insights on newer topics, you
might want to check these out:
1. 8 Highest Paying Jobs For Computer Science Professionals In India
2. Test Plan vs Test Strategy: What Makes The Two Different?
3. The Difference Between Static And Dynamic Website Explained!
4. Difference Between Mealy Machine And Moore Machine [With
Comparison Table]
5. Difference Between Paging And Segmentation Explained!
List Of 50 Core Java Interview Questions With Answers (2022)
22 mins read
Did you know that Java is the third most popular programming
language in the world? The programming language is used by
developers to develop applications for mobile phones, computers,
data centers, game consoles, etc.
Java is a general-purpose, high-level, object-oriented
programming language. Developed by Sun Microsystems which
is a subsidiary of Oracle, Java is a secure, high-performance, and
multithreaded programming language. Because it has a runtime
environment and API, Java can also be considered a platform.
When we speak of core Java, the word 'core' describes the
fundamental concepts of Java programming. Hence, beginners need
to start with core Java and gradually proceed towards the advanced
level of Java.
Because of its important applications, questions from Java are
frequently asked in software developer or IT-related job interviews.
In this article, we shall cover the top 50 core Java interview
questions to help you prepare well. But first, let's understand the
difference between core Java and advanced Java.
Difference between core Java and advanced Java

Core Java Advanced Java

Covers the advanced


Covers the basic
topics and concepts
concepts of the Java
of the Java
programming
programming
language.
language.

Used for computing


Used for enterprise
or desktop
applications.
applications.

Covers some topics Covers some topics


like data types, OOP, like web services,
operators, exception database
handling, threading, connectivity, JSP,
etc. Servlets, EJB, etc.

It is the next step


It is the first step, to
after completing the
begin with, Java.
Core Java.

Based on single-tier Based on two-tier


architecture. It is thus architecture. It
known as a stand- requires a strong
alone application. client-server
architecture.

It comes under Java It comes under Java


SE. EE or J2EE.

Important Core Java Questions


Q1. Differentiate between C++ and Java

C++ Java

Platform-dependent. Platform-independent.

Mainly used for Mainly used for


system application
programming. programming.

Designed as a support
Designed for
network computing
systems and
with a goal of being
applications
easy to use and
programming. It is
accessible to a broader
an extension of C.
audience.

Doesn't support
Supports multiple
multiple inheritance
inheritance.
through class.

Supports operator Doesn't support


overloading. operator overloading.
Compiled and run
using the compiler
Java uses compiler and
which converts
interpreter both.
source code into
machine code.

Supports both call


Supports call by value
by value and call by
only.
reference.

Supports structures Supports structures and


and unions. unions.

Doesn't have built-


Has built-in thread
in support for
support.
threads.

Supports virtual Has no virtual


keywords. keyword.

Uses a single
Creates a new inheritance tree always
inheritance tree because all classes are
always. the child of an object
class in Java.

Interactive with Not so interactive with


hardware. hardware.

Q2. What is an abstract class in Java?


A class that is declared using the 'abstract' keyword is known as an
abstract class. It is a restricted class that cannot be used to create
objects. Hence, for accessing an abstract class, it must be inherited
from another class.
From the above class, it is not possible to create an object of the
Animal class.
Q3. What are the features of Java Programming language?
Following are the features in Java Programming Language:
 Easy to learn: Java is a simple language. The syntax of Java is
based on C++ which makes it easier.
 Object-Oriented Language: Java has an object-oriented paradigm
allowing us to maintain our code as a combination of different type
of objects. This incorporates both data and behavior.
 Platform Independent: Java is a platform-independent
programming language and comes with its platform on which its
code is executed. It doesn't need operating system to be executed.
 Secured: Java is secured as it doesn't make use of explicit pointers
and utilizes the concept of ByteCode and Exception handling.
 Portable: Java supports the read-once-write-anywhere approach
making it executable on any machine.
 High Performance: Java is faster than other traditional interpreted
programming languages. However, it is slower than a compiled
language such as C++
 Multithreaded: Java uses multi threads wherein we can write
programs that deal with many tasks at once.
 Robust: Java uses strong memory management, automatic garbage
collection, exception handling, etc. making it a strong programming
language.
 Distributed: Java is distributed which can make it access files by
calling the methods from any machine on the internet.
 Dynamic: Java is a dynamic language and thus classes in Java are
loaded on demand. It even supports functions from its native
languages such as C and C++.
Q4. What do you understand by Java virtual machine?
It is a virtual machine that enables the computer to run the Java
program. Abbreviated as JVM, Java Virtual Machine acts like a
run-time engine that calls the main method present in the Java
code. The Java code is compiled by JVM to be a Bytecode that is
machine independent and close to the native code.
Q5. How many types of memory areas are allocated by JVM?
JVM or Java Virtual machine has the following memory areas:
 Class(Method) Area: Stores per-class structures such as the
runtime constant pool, field, method data, code for methods, etc.
 Heap: Runtime data area in which the memory is allocated to the
objects
 Stack: Holds local variables and partial results, and plays a part in
method invocation and return.
 Program Counter Register: Contains the address of the current
Java Virtual Machine instruction being executed.
 Native Method Stack: Contains all the native methods used in the
application.
Q6. What is the difference between Heap and Stack Memory in
Java?

Stack Memory Heap Memory

Used to store items Allocated to store


that have a short life objects and JRE
like local variables classes.
or a reference
Stack Memory Heap Memory

variable of objects

Uses dynamic
allocation where there
Always reserved in a
is no fixed pattern for
LIFO (last in first
allocating and de-
out) order
allocating blocks in
memory

Can be used to Can increase or


increase stack decrease heap memory
memory size by size by using JVM
using the JVM option -Xms and -
parameter -XSS. Xmx

Variables are visible


It is visible to all
only to the owner
threads
thread

Q7. What is a JIT compiler?


JIT or Just-In-Time Compiler is actually a translator that is used
to improve the performance by compiling parts of the bytecode
having similar functionality at one time. This reduces the amount of
time needed for compilation. JIT translates the instruction set of a
Java virtual machine (JVM) to the instruction set of a specific
CPU.
Q8. What is the platform?
A platform is defined as the hardware or software environment
wherein a part of the software is executed. Platforms can be of 2
types: software-based and hardware-based. Java provides a
software-based platform.
Q9. Differentiate between the Java platform and other
platforms?
Following are the differences between the Java platform and other
platforms.
 Java is a software-based platform while other platforms may be

hardware platforms or software-based platforms.


 Java is executed on top of other hardware platforms. However,

other platforms can only have the hardware components.


Q10. What is a classloader in Java?
Classloader is a subsystem of JVM that can be used to load class
files. Any Java program is first loaded by the classloader. There are
three built-in Java classloader types:
1. Bootstrap ClassLoader: First classloader which is the superclass
of Extension classloader. It loads the rt.jar file that contains all
class files of Java Standard Edition like java. lang package classes,
java.net package classes, and java.util package classes, java.io
package classes, java.sql package classes, etc.
2. Extension ClassLoader: Child classloader of Bootstrap and parent
classloader of System classloader. It loads the jar files located
inside $JAVA_HOME/jre/lib/ext directory.
3. System/Application ClassLoader: Child classloader of Extension
classloader that loads the class files from the classpath.
Q11. Explain overloading and overriding in Java?
 Overloading is when a Java program has more than one method

with the same name in a single class but the arguments are
different.
 Overriding happens in inheritance. For example: when two

methods have the same name and same signature, one is parent
class and another is child class. Here the method of child class is
overridden to make sure that is any changes are done to the parent
class, the child class also gets changed accordingly.
Q12. What are the various access specifiers in Java?
Access specifiers are the keywords used to define the access scope
of the method, class, or variable in Java. There are four access
specifiers:
 Public: Can be accessed by any class or method.
 Protected: Can be accessed by the class of the same package, by
the sub-class of this class, or within the same class.
 Default: Are accessible within the package only. All the variables,
classes, and methods are of default scope.
 Private: Class, methods, or variables defined as private can be
accessed within the class only.
Q13. Differentiate between Array list and vector in Java.

ArrayList Vector

Not synchronized. Synchronized.

Is faster as it is Slow as it is thread-


non-synchronized. safe.

Does not define the Defines the increment


increment size. size.

Can only use an Can use both


iterator for enumeration and
traversing. iterator for traversing.

Q14. What is a class in Java?


Class is a set of objects which share common characteristics and
attributes. It is a group of variables of different data types and
group of methods. Class is not a real-world entity and is just a
prototype from which objects are created. It doesn't occupy
memory. A class in java may contain the following:
• Data member
• Method
• Constructor
• Nested class and
• Interface
Syntax:
On the other hand, an object is a member or an instance of a Java
class. Every object has an identity, behavior, and state. An object
can be created using a new keyword. When an instance of the class
or object is created, memory is allocated for the newly
created object and also returns the reference of that object to that
memory. The syntax for creating an object is:
ClassName object = new ClassName();
Q15. What is the difference between JDK, JRE and JVM?

JDK JRE JVM

Stands for
Stands for
Java Stands for
Java
Runtime Java Virtual
Developm
Environmen Machine.
ent Kit.
t.

It is a It is an It is a
software implementat platform-
developm ion of JVM independent
ent kit that
develops
applicatio and is a type
abstract
ns in Java. of software
machine that
Along package that
has three
with JRE, provides
notions in
the JDK class
the form of
also libraries of
specification
consists of Java, JVM,
s. It
various and various
describes the
developm other
requirement
ent tools components
of JVM
(Java for running
implementati
Debugger, Java
on.
JavaDoc, applications.
compilers,
etc.)

Assists in
executing
Creates an Providing all
codes. It
environment of the
primarily
for the implementati
functions
execution of ons to the
in
code. JRE.
developm
ent.

Platform-
Platform- Platform-
dependent
dependent. independent.
.
Q16. Define the term 'Double Brace Initialization' in Java
Double Brace Initialization is a term that refers to the combination
of two independent processes. It has two braces. The first brace
creates an anonymous inner class. The second brace is an
initialization block. When used together, it is known as Double
Brace Initialization.
Advantages:
 Has lesser lines of code compared to the native way of creation and
initialization.
 The code is more readable.
 Creation and Initialization are done in the same expression.
Disadvantages:
 It is obscure.
 Creates an extra class every time.
 Holds a hidden reference to the enclosing instance that may cause
memory leaks.
Q17. What are the advantages of packages in Java?
Packages in Java are used to group related classes. Following are
the advantages of packages in Java.
 Avoid name clashes.
 Provides easy access control.
 Makes it easier to locate the related classes.
Q18. Explain object-oriented paradigm
The object-oriented paradigm is a programming paradigm that aims
to incorporate the advantages of modularity and reusability. It is
based on objects having data and methods defined in the class to
which it belongs. Following are the features of the object-oriented
paradigm:
 Focus on data with methods to operate upon the data of the object.
 Follows the bottom-up approach in program design.
 Includes concepts like encapsulation and abstraction which hides
the complexities from the user and highlights only the functionality.
 Examples of the object-oriented paradigm are C++, Python, C#,
etc.
Q19. What is the major difference between an object-oriented
programming language and an object-based programming
language?
The major difference between an object-oriented programming
language and an object-based programming language is as follows:

Object-oriented Object-based
Programming Programming
Language Language

Characteristics of
object-oriented
All features of object-
programming such
oriented programming
as inheritance and
are supported.
polymorphism are
not supported.

Have built-in
Don't have a built-in
objects. Example:
object. Example: C+
JavaScript has a
+.
window object.

Java is an example of VB is an example of


object-oriented object-based
programing language language.
which supports
creating and inheriting
one class from
another.

Q20. Why is Java called as platform independent?


Java is called platform independent because its byte codes can run
on any system irrespective of its underlying operating system.
Hence, it is not dependent of any platform.
Q21. Define wrapper classes in Java
Wrapper classes convert the Java primitives into reference types
(objects). These are known as wrapper classes because they 'wrap'
the primitive data types (int, boolean, etc.) into an object of that
class. Hence, the wrapper class in Java provides a mechanism to
convert primitive into object and object into primitive.
Q22. Is Java 100% Object-oriented?
No, Java is not 100% object-oriented. This is because it makes use
of primitive data types such as boolean, byte, char, int, float,
double, long, and short which are not considered objects
Q23. Explain constructors in Java
Constructor refers to a block of code that is used to initialize an
object. It must have the same name as that of the class. It has no
return type and it is automatically called when an object is created.
Types of Constructor
 Default Constructor: A default constructor is the one that does not
take any inputs. These are no argument constructors which will be
created by default if no other constructor is defined by the user. Its
main purpose is to initialize the instance variables with the default
values.
 Parameterized Constructor: The parameterized constructor is the
constructor which is capable of initializing the instance variables
with the provided values. In other words, parameterized
constructors take arguments.
Q24. Define 'static' keyword in Java.
The purpose of adding static before any entity is to make that entity
a class entity. Hence, adding static before a method or a variable
makes it a class method or class variable respectively. Any static
member can be accessed before any objects of its class are created
and without reference to any object. The static keyword is used
mainly for memory management and can be applied with variables,
methods, blocks, and nested classes.
Q25. Explain the Static Void methods in Java
Speaking of the static void methods, these are static methods that
do not return anything. Static methods can be called directly by
class names. On the other hand, a void method is a method that
returns nothing but one needs to create a class object to call such a
method.
Q26. What is a static Variable in Java?
As explained above, if a variable is declared static, it is known as a
static variable. It can be used to refer to the common property of all
objects that is not unique for each object. For example, the
company name of employees, school names of students, etc. Static
variable gets memory only once in the class area at the time of class
loading.
Q27. What are static blocks?
When the static keyword is attached to a block of code, the block is
called a static block. Static blocks in Java are executed
automatically when the class is loaded in memory. These are
executed before the main() method and are executed only once as
the class file is loaded to memory.
Q28. Define abstraction in Object-oriented programming.
Abstraction in object-oriented programming is defined as hiding
unnecessary details from the user so as to focus on the essential
details or functionality. It increases efficiency and thus reduces
complexity. In Java, abstraction can be achieved using abstract
classes and methods.
Q29. What is an abstract method?
When a method is declared using the abstract keyword within an
abstract class and does not have a definition is called an abstract
method. An abstract method is also called subclass responsibility as
it doesn't have the implementation in the superclass. This means
that a subclass must override it to provide the method definition.
The syntax for abstract method:
abstract return_type method_name( [ argument-list ] );
Q30. What are string objects in Java?
Strings are data types used in Java programs. They come with
special support from the Java libraries. While these are described as
arrays in many programming languages, strings are defined as
objects called string objects in Java. String objects in Java are
immutable and thus can't be modified after being initialized.
Q31. What is a Try-Catch Block in Java?
The try-catch block in Java is used to handle exceptions and
prevents the abnormal termination of the program. The syntax of
this block is as follows:
While the try block includes the code that might generate an
exception, the catch block includes the code that is executed when
an exception occurs inside the try block.
Q32. Explain object cloning in Java
In Java, object cloning is a method to create an exact copy of an
object. The clone() method of Object class is used to clone an
object. (By default, no class in Java supports cloning but there is an
interface in Java called java.lang.Cloneable, which helps to make
a duplicate copy of the object. Here, java.lang.Cloneable is
implemented to avoid runtime exceptions).
The clone() method saves processing time for creating the exact
copy of an object. Generally, the clone() method of an object
creates a new instance of the same class, and copies all the fields to
the new instance, and returns it. This is called a shallow copy.
It is important to note that if the original object has any
references to other objects as fields, only the references of those
objects are copied into the clone object. Copy of those objects is
not created.
Q33. What is a method signature in Java?
The method signature is part of the method declaration in Java. It is
a combination of the method name and the parameter list. Here is
an example:
Output: 70
Q34. What is method overloading in Java?
If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading. It happens when
there are two methods with the same name but different
arguments. Overloaded methods are differentiated based on the
number and type of parameter passed as arguments to the methods.
Following is an example of method overloading:
In the above example, func() is overloaded.
Q35. Difference between static and non-static methods in Java.

Static Method Non-Static Method

Can access both static


Can access only
as well as non-static
static members.
members.

Utilizes compile- Uses run time binding


time binding or
early binding. or dynamic binding.

Cannot be
overridden by Can be overridden by
compile time dynamic binding.
binding.

Take less space and


Occupy more space.
memory allocation

Declared using a Doesn't require any


static keyword. special keyword.

Q36. How to create a thread in Java?


A thread can be created in two ways in Java:
1. By extending the thread class. The example is as follows:
2. By implementing a runnable interface. The example is as
follows:
Q37. What is the difference between equals() and == in Java?
Equals() method is defined in the object class in Java. It is used for
checking the equality of two objects defined by business logic.
“==” is called the equality operator in Java. It is a binary operator
used to compare primitives and objects.
Q38. What is a compile-time error in Java?
When a problem stops the program execution in Java, it is known
as an error. Errors can occur because of syntax issues while others
occur at the time of program execution. The errors with syntax
issues are known as compile-time errors. Examples of compile-time
errors are missing parenthesis or a missing semicolon. The other
type of programming error that occurs at runtime is called runtime
error.
Types of compile-time programming errors:
 Syntactical Errors
 Semantic Errors
 Lexical Errors
Q39. What is a throwable class in Java?
A throwable class in Java is a class that is the superclass of all
exceptions and errors that can occur in a Java program. A throwable
class extends the object class. Actually, a throwable class is itself a
subclass of the superclass of all Java classes and object classes.
Some important methods present in throwable class are:
 toString(): Returns information about an exception in the form of a
string.
 getMessage(): Returns a detailed message of an exception that has
recurred.
 printStackTrace(): Prints the stack trace of an exception.
 getClause(): Returns the exception that caused the occurrence of
the current exception.
Q40. Define constructor chaining in Java.
Constructor chaining is defined as the ability to call a constructor
inside another constructor. For constructor chaining in Java, the
keyword 'this' is used to call a constructor from within another
constructor in the same class. Constructor chaining makes a
program more readable and easy to understand for the reader.
Q41. How to achieve thread safety in Java?
Thread safety is a process used in Java to make programs safe for
use in a multithreaded environment. It happens when a thread is
working on an object while preventing another thread from
working on the same object. A thread-safe code handles shared data
structures in a way that ensures that all threads behave well and
fulfill their specifications without any unintended interaction.
Q42. What is a Boiler Plate code in Java?
By definition, boilerplate code refers to a pile of code blocks with
a fixed pattern. It can be widely applied to various program
modules. The name comes from the newspaper printing industry
where repetitive text was used in various contexts and pages.
Boilerplate codes can be helpful for developers to make more
standardized formats, improve the quality of codes, and increase
the coding speed.
Here is a typical boilerplate code:
Q43. What is a super keyword in Java?
A super keyword refers to the superclass objects or the parent
objects. It is thus a reference variable. The super keyword can be
used to:
 refer immediate parent class instance variable.

 invoke immediate parent class method.

 invoke immediate parent class constructor.

Q44. What is a derived class?


In the Java language, classes can be derived from other classes,
thereby inheriting fields and methods from those classes. A class
that is derived from another class is called a subclass (also
a derived class, extended class, or child class). The class from
which the subclass is derived is called a superclass (also a base
class or a parent class).
Q45. Name the types of classes in Java
There are seven types of classes in Java:
1. Static Class
2. Final Class
3. Abstract Class
4. Concrete Class
5. Singleton Class
6. POJO Class
7. Inner Class
Q46. What are the different types of inheritance in Java?
Inheritance in Java is a mechanism through which one object
acquires all the properties and behaviors of a parent object. The
original class is known as the base class/parent class while the new
class is called the derived class or the child class.
The types of inheritance in Java are:
 Single Inheritance

 Multi-level Inheritance

 Hierarchical Inheritance

 Hybrid Inheritance

Q47. Explain thread lifecycle in Java.


A thread in Java is the path or direction that is taken up while a
program is being executed. At any instance, a thread always exists
in any one of the following states.
1. NEW: The newly created thread that has not yet started the
execution
2. RUNNABLE/ACTIVE: Running or ready for execution but it's
waiting for resource allocation
3. BLOCKED or WAITING: Inactive for a particular span of time.
4. TIMED WAITING: Waiting for some other thread to perform a
specific action for a specified period.
5. TERMINATED: Has completed its execution.
Q48. Name the types of Loops on Java
loops are used to repeat a block of code. There are three kinds of
loops which are – the for loop, the while loop, and the do-while
loop. All these three loop constructs of Java execute a set of
repeated statements as long as a specified condition remains true.
This particular condition is generally known as loop control.
Q49. Define Serialization, Deserialization and Transient
Keyword In Java
In Java, serialization is a mechanism of writing the state of an
object into a byte stream (sequence of bytes). On the other
hand, deserialization is the process of converting a stream of bytes
back into a copy of the original object. Taking these two into view,
a transient keyword is used to indicate that a field should not be
serialized. This means that if at the time of serialization, the coder
does not want to save the value of a particular variable in the file,
they can make use of the transient keyword. Hence when Java
Virtual Machine ( the abstract machine that executes Java
programming) comes across a transient keyword, it ignores the
original value of the variable and saves the default value of that
variable data type.
Q50. What are the features of Java 8?
Java 8 version of Java was released by Oracle in March 2014. It is
considered a revolutionary software development platform. Some
interesting features of Java 8 are:
 Lambda expression: Adds functional processing capability to
Java.
 Date Time API: Improved date time API.
 Default and Static methods in Interfaces.
 Nashorn, JavaScript Engine: A Java-based engine to execute
JavaScript code.
 Method references: Referencing functions by their names instead
of invoking them directly.
 New tools: New compiler tools and utilities to figure out
dependencies.
 Stream API: New stream API to facilitate pipeline processing.
Tips for Preparing for Core Java
Now that you have browsed through the above questions, you
might have become aware of the important topics in core Java that
you must go through before facing a job interview. If you are a
beginner in Java, taking courses to understand the basics of
Java can be very helpful. Apart from this, here are some tips to help
you get a good grasp on this programming language:
 Start with the basics

 Practice Java coding through writing codes

 Interact with other beginners to discuss the tricky areas

 Analyze the algorithm carefully

 Read Java books for beginners for an in-depth understanding of

Java concepts.
More Reads:
1. Important Accenture Interview Questions That You Must Not
Ignore!
2. Most Common Programming Interview Questions With Answers
2022
3. Top 50 OOPs Interview Questions With Answers 2022
4. 50 Frequently Asked LinkedList Interview Questions With Answers
2022
Top 15+ Difference Between C++ And Java Explained!
(+Similarities)
There are multiple differences between C++ and Java that you
must understand when deciding which language to work with.
Some common points for Java Vs. C++ includes the syntax,
platform dependence/ independence, memory management, and
more.
17 mins read

In the sea of programming languages, C++ and Java both have a


wide range of applications across industries, making them popular
choices for many aspiring developers and professionals alike.
Discussing the popularity and use of programming languages often
leads to a comparison highlighting the differences and the pros and
cons of respective languages.
In this article, we will explore the difference between C++ and Java
to better understand how these languages fare when pitted against
each other. We will begin by discussing some basics of both
languages individually, then the differences between C++ and Java,
followed by some similarities. So let's get started!
Brief Introduction To C++
The C++ programming language is a powerful and versatile
language known for its high performance and wide range of
applications. It was originally developed as an extension of the C
language to support procedural and object-oriented programming
language. These additions made it suitable for developing
everything from system software to complex applications and
games.
Brief Introduction To Java
Java is a popular, high-level programming language renowned for
its platform independence and robustness. Launched by Sun
Microsystems in 1995, Java is widely used for developing cross-
platform applications, web servers, mobile apps, and enterprise
systems. With its simplicity, object-oriented nature, and extensive
libraries, Java has become a cornerstone in software development,
offering features like automatic memory management, multi-
threading, and strong security mechanisms.
Difference Between C++ and Java
Listed below are the key difference between C++ and Java:

Feature C++ Java

Memory Manual Automatic


Feature C++ Java

memory
manageme
nt with new memory
and delete management
operators. through
Requires automatic
explicit garbage
allocation collection
Manageme and feature.
nt deallocatio Memory is
n of reclaimed by
memory. the JVM
However, when objects
there is are no
allowance longer in
of dynamic use.
memory
allocation.

Platform C++ itself Platform-


Dependenc is platform- independent,
y independen i.e., the Java
t, as it's a byte code
standardize runs on any
d device with
programmi a compatible
ng JVM,
language. enabling
Feature C++ Java

However,
factors like
compilers,
operating cross-
system platform
APIs, compatibilit
hardware y without
architecture recompilatio
, and third- n. That is,
party the Java
libraries interpreter
can executes the
introduce bytecode at
platform runtime.
dependenci
es in C++
programs.

Syntax and Complex Simpler


Semantics syntax with syntax with
pointers, no pointers,
manual automatic
memory memory
manageme management
nt, and , and single
multiple inheritance
inheritance. with
interfaces
Feature C++ Java

for
achieving
abstraction.

Slightly
slower
Generally, execution
it has faster speed due to
execution bytecode
speed, is interpretatio
closer to n and
Performan hardware, garbage
ce and has collection
more overhead.
control JIT
over compilation
system helps narrow
resources. the
performance
gap.

Object- Supports Strictly


Oriented procedural, follows
Programm object- object-
ing oriented, oriented
and generic principles,
programmi with
ng everything
Feature C++ Java

being an
object
paradigms. except for
primitive
data types.

Exception
handling is Exception
available b handling is
ut not an integral
mandatory. part of the
This way, language.
the All
Exception community exceptions
Handling of must be
developers/ caught or
programme declared,
rs have promoting
more robust error-
control handling
over error practices.
handling.

Standard Rich Extensive


Libraries standard standard
library library (Java
(STL) with API)
a wide covering
Feature C++ Java

essential
functionaliti
es such as
range of
networking,
data
I/O
structures
operations,
and
concurrency,
algorithms.
and GUI
development
.

Compiled
directly Compiled
into into
machine- platform-
Compilatio
specific independent
n vs.
executable bytecode,
Interpretat
binaries, which is
ion
i.e., direct interpreted
machine by the JVM
code at runtime.
execution.

Pointers Supports Does not


pointers support
and manual pointers for
memory direct
manageme memory
Feature C++ Java

nt,
allowing manipulatio
direct n, enhancing
access to security and
memory stability.
addresses.

Typically
used with
Supports a IDEs like
wide range Eclipse,
of IDEs IntelliJ
Developme and text IDEA, or
nt editors, NetBeans,
Environme with offering
nt compilers integrated
available debugging,
for various profiling,
platforms. and
development
tools.

Primarily C++ is Java is


used for mainly mainly used
used for for
system application
programmi programmin
ng. g. It is
Feature C++ Java

widely used
in various
domains.

Java uses a
C++ single
always inheritance
Inheritanc creates a tree; all
e Tree new classes are
inheritance children of
tree. the Object
class.

Communit Active Strong and


y and developer active
Ecosystem community community
with a vast support with
ecosystem extensive
of third- documentati
party on, tutorials,
libraries and
and resources.
framework Rich
s catering ecosystem of
to diverse libraries
needs. and framewo
rks
supporting
Feature C++ Java

enterprise-
grade
applications
and web
development
.

Java
C++
supports the
Unsigned doesn't
>>>
Right Shift support the
operator for
>>> >>>
unsigned
operator.
right shift.

Java is less
C++ is
interactive
Hardware closer to
with
hardware.
hardware.

Java has no
C++
virtual
supports
keyword; all
Virtual the virtual
non-static
Keyword keyword fo
methods are
r method
virtual by
overriding.
default.

Overview & Features Of C++ Language


C++ comes with various features that make it a popular choice
amongst programmers and companies. These include-
 Object-Oriented: C++ supports the principles of object-oriented
programming (OOP), including classes, inheritance,
polymorphism, encapsulation, and abstraction.
 Middle-Level Language: Combining high-level features with low-
level programming functionality, C++ allows for efficient memory
manipulation and hardware access.
 Portable: C++ is designed to be portable, and writing platform-
independent code is feasible by adhering to standardized language
features and using platform abstraction layers, preprocessor
directives, or third-party libraries that provide cross-platform
support.
 Extensibility: C++ facilitates machine code reuse and scalability,
with the help of comprehensive libraries and support for modular
programming.
 Efficiency: C++ offers performance close to that of low-level
languages like C, making it suitable for resource-intensive
applications.
 Compatibility: C++ maintains backward compatibility with C,
allowing for seamless integration with existing C code.
 Standard Library: C++ comes with a rich standard library that
provides ready-to-use functions and data structures for common
tasks.
A Brief History Of C++
C++, a general-purpose programming language was developed by
Bjarne Stroustrup at Bell Labs in the early 1980s as an extension of
the C programming language. Its primary aim was to add support
for object-oriented programming through the concepts of objects
and classes. This boosted productivity and efficiency while
retaining the flexibility of C. C++ was first released commercially
in 1985, and it quickly gained popularity due to its versatility and
performance.
Since its creation, it has become one of the most widely used
languages for creating software today, especially for embedded
systems where memory usage is crucial. Many new features in
contemporary C++, like lambda functions and string literals,
improve software development efficiency.
Read More: A Detailed History Of C++ Explained With Timeline
Infographic
Applications Of C++
Some of the most common applications of C++ programming
language are:
1. System Software: Operating systems, device drivers, and
embedded systems often rely on C++ for its efficiency and low-
level capabilities.
2. Application Software: C++ is used to develop a wide range of
applications, including desktop software, space probes, video
games, game engines, and graphical user interfaces (GUIs).
3. Web Development: While less common than other languages, C++
can be used for web development, particularly in server-side
applications and high-performance web servers.
4. Scientific Computing: C++ is popular in scientific and
engineering fields for its ability to handle complex calculations and
simulations efficiently.
Advantages & Disadvantages of C++

Advantages Disadvantages

Performance: C++ Complexity: C++ can


offers high be challenging to learn
performance and and master due to its
efficiency, which complex syntax and
make it suitable for extensive feature set.
resource-intensive Memory
applications. Management: Manual
Control: With memory management
direct access to in C++ can lead to
memory and memory leaks and
hardware, C++ segmentation faults if
provides developers not handled properly.
with fine-grained Safety Concerns: C+
control over system + offers fewer built-in
resources. safety features
Flexibility: C++ compared to higher-
supports multiple level languages,
programming leaving room for errors
paradigms, such as buffer
including overflows and null
procedural, object- pointer dereferences.
oriented, and Slower
generic Development: Writing
programming. C++ code may take
Portability: C++ longer compared to
code can be higher-level languages
compiled to run on due to its lower level
various platforms of abstraction and
with minimal stricter syntax
modification, thanks requirements.
to its platform
independence.
Compatibility: C++
maintains
compatibility with
C, allowing for
seamless integration
with existing C
codebases.

Overview & Features of Java Language


Some of the most important features of Java include:
1. Platform Independence: Java code can run on any platform with
the Java Virtual Machine (JVM), making it highly portable.
2. Object-Oriented: Java follows the object-oriented programming
(OOP) paradigm, supporting concepts like classes, objects,
inheritance, polymorphism, encapsulation, and abstraction.
3. Automatic Memory Management: Java features automatic
garbage collection, relieving Java developers from manual memory
management tasks.
4. Robustness: Java incorporates strong type checking, exception
handling, and a comprehensive standard library, enhancing the
reliability and robustness of applications.
5. Multi-threading: Java provides built-in support for multi-
threading, enabling concurrent execution of multiple tasks within a
single program.
6. Security: Java's sandbox security model and built-in security
features protect against malicious code and unauthorized access to
system resources.
7. Rich Standard Library: Java offers a vast standard library (Java
API) that provides pre-built functionality for common tasks,
facilitating rapid application development.
A Brief History Of Java
Java was created by James Gosling and his team at Sun
Microsystems in the early 1990s. In reality, it was originally
designed for interactive television, but its platform independence
and other features made it ideal for internet-based applications.
 The language was initially named Oak but was later changed to

Java due to trademark issues.


 In 1995, Sun released a public implementation as a core component

of its Java platform (now known as Java SE). This allowed


developers to write applets that could run in web browsers on any
operating system with a JVM installed.
 Since then, numerous updates and versions have been released for

desktop applications and servers using this versatile programming


language.
Common Applications Of Java
Mentioned below are some important applications of Java that
make it a preferred choice for many:
1. Enterprise Applications: Java is widely used in enterprise
environments for developing large-scale, mission-critical
applications such as banking systems, customer relationship
management (CRM) software, and e-commerce platforms.
2. Web Development: Java is commonly used for server-side web
development, powering dynamic websites, web applications, and
web services.
3. Mobile Development: Java, along with the Android SDK, is the
primary language for developing Android mobile applications.
4. Desktop Applications: Java's rich GUI libraries (such as JavaFX
and Swing) make it suitable for developing desktop applications
with graphical user interfaces (GUIs).
5. Embedded Systems: Java's platform independence and robustness
make it suitable for developing embedded systems in various
industries, including automotive, healthcare, and industrial
automation.
Advantages And Disadvantages of Java
Here are some advantages and disadvantages of the Java
programming language that you must consider:

Advantages Disadvantages

Platform Performance
Independence: Java's Overhead: Java's
"write once, run platform
anywhere" principle independence and
allows for easy automatic memory
deployment on management come
different platforms with a performance
without modification. overhead compared
Object-Oriented: Jav to lower-level
a's OOP features languages like C++.
promote code Complexity: Java's
reusability, extensive feature set
modularity, and and ecosystem can
maintainability. lead to a steep
Automatic Memory learning curve for
Management: Java's beginners.
garbage collection Resource
mechanism reduces Consumption: Java
the risk of memory applications may
leaks and simplifies consume more
memory management. system resources
Robustness: Java's (such as memory
strong type system, and CPU) compared
exception handling,
and built-in security
features enhance to lightweight
application reliability languages.
and robustness. Limited Low-Level
Large Access: Java's
Ecosystem: Java abstraction layer
boasts a vast shields developers
ecosystem of libraries, from low-level
frameworks, tools, and system access,
community support, limiting performance
accelerating optimization in
development and certain scenarios.
solving complex
problems efficiently.

Read More: Advantages And Disadvantages of Java Programming


Language
Example of C++ and Java Program
C++ Program to Print Variable Values
Source Code Example:
#include <iostream>

using namespace std;

int main() {
// Define an integer variable 'i' with value 5
int i = 5;

// Print the current value of 'i', which is 5


cout << "Value is " << i << endl;
// Incrementing the value by 1, now it's 6
i++;

// Print out its new value: 6


cout << "The new value: " << i;

return 0;
}
Output:
Value is 5
The new value: 6
Code Explanation:
As mentioned in the code comment, we first create a variable called
i, of integer data type and assign a value of 5 to it. The C++
program then prints the value to the console using the cout
command. After that, we increment the value of the i by 1 (using
increment operator) and again print it to the console.
Java Program to Increment the Value of a Variable
Source Code Example:
public class IncrementDemo {

public static void main(String[] args) {


// Define an integer variable 'x' with value 5
int x = 5;

// Print the current value of 'x'


System.out.println("Value before increment: " + x);

// Increment the value of 'x' by 1


x++;
// Print the new value of 'x' after incrementing
System.out.println("Value after increment: " + x);
}
}
Output:
Value before increment: 5
Value after increment: 6
Code Explanation:
This code uses the increment operator to increase the value of an
integer, x, by one. The first println statement prints out the current
value of x (5), and then after it is increased using "x++," which
adds 1 to its value. Finally, another println statement prints out that
incremented number (6).
Key Difference Between C++ And Java Explained
In this section, we will elaborate on a few key differences for Java
Vs C++:
Memory Management:
 In C++, memory management is manual, requiring developers to
allocate and deallocate memory explicitly using new and delete
operators. This gives developers full control over memory usage
but increases the risk of memory leaks and segmentation faults if
not managed properly.
 Java, on the other hand, features automatic memory management
through garbage collection. The Java Virtual Machine (JVM)
automatically reclaims memory from objects that are no longer in
use, reducing the likelihood of memory-related errors.
Platform Independence:
 C++ code is platform-dependent and needs to be recompiled for
each target platform. Binaries generated for one platform may not
run on another platform without modification.
 Java code, however, is compiled into platform-independent
bytecode, which can run on any device with a compatible Java
Virtual Machine (JVM). This 'Write Once, Run Anywhere'
(WORA) capability makes Java ideal for cross-platform
development.
Thread Support:
 C++ relies on third-party libraries like pthreads for threading
support, leading to complexity and potential portability issues.
 Java has built-in support for threading with classes like Thread and
interfaces like Runnable, simplifying concurrent programming and
making it easier to take advantage of multi-core processors.
Syntax and Semantics:
 C++ has a more complex syntax compared to Java. It
includes features like pointers, manual memory management, and
support for multiple inheritance.
 Java emphasizes simplicity and strict adherence to object-oriented
principles. It does not support features like pointers and multiple
inheritance, promoting safer and more manageable code.
Performance:
 C++ generally offers faster execution speed compared to Java. C++
code is compiled directly into machine-specific binaries, allowing
for low-level optimizations and efficient use of system resources.
 Java, however, may have slightly slower execution speed due to
bytecode interpretation and garbage collection overhead. While
Just-In-Time (JIT) compilation helps narrow the performance gap,
C++ is still preferred for performance-critical applications.
Operator Overloading and Pointers:
 C++ supports operator overloading, allowing developers to define
custom behaviors for operators such as +, -, *, etc. It also supports
pointers, enabling direct manipulation of memory addresses.
 Java does not directly support operator overloading or pointers.
This simplifies the language and promotes safety and security by
preventing certain types of errors common in C++. Instead, Java
encourages the use of references, which are managed internally by
the JVM.
Salary:
 C++ developers may command higher salaries in certain industries,
such as game development, system programming, and finance, due
to the language's performance-critical nature and specialized
expertise required.
 Java developers, on the other hand, might see slightly higher
average salaries overall due to the widespread use of Java in
enterprise environments, web development, and Android app
development.
Ease of Learning:
 Java is often considered easier to learn compared to C++ due to its
simpler syntax, automatic memory management, and extensive
documentation.
 C++ can be more challenging for beginners due to its complex
language syntax, manual memory management, and lower-level
concepts such as pointers.
Similarities Between Java Vs. C++
There also exist several similarities between C++ Vs Java, such as:

Similarities Between C++


Criteria
and Java

Object- Both C++ and Java are


Oriented: object-oriented languages,
supporting classes,
inheritance, encapsulation,
Similarities Between C++
Criteria
and Java

and polymorphism.

C++ and Java enforce static


typing, requiring variable
Static Typing: types to be declared before
use, improving code
reliability.

Both languages are widely


used across various
domains, including software
Wide Usage: development, game
development, web
development, and enterprise
applications.

C++ and Java come with


extensive standard libraries
Standard offering a wide range of
Libraries: functionalities, reducing the
need for developers to
reinvent the wheel.

Multi- While C++ requires separate


Platform compilation for different
Support: platforms, both languages
support cross-platform
Similarities Between C++
Criteria
and Java

development using platform-


independent libraries and
frameworks.

Both languages share


similarities in their C-style
syntax, including the use of
curly braces, semicolons,
Syntax
and many keywords. This
makes it easier for
developers familiar with C
to transition to C++ or Java.

Both have memory


management features,
Memory
although they differ in their
Management
approach (manual in C++
and automatic in Java).

Both have mechanisms for


Exception
handling exceptions through
Handling
try-catch blocks.

Community Both C++ and Java have


Support: active communities of
developers providing
support, tutorials, forums,
Similarities Between C++
Criteria
and Java

and resources for learning


and troubleshooting.

Conclusion
Both C++ and Java are powerful and popular programming
languages with their own sets of advantages and trade-offs. C++
excels in performance-critical applications and systems
programming, offering unparalleled control over hardware
resources. On the other hand, Java's platform independence,
automatic memory management, and ease of learning make it well-
suited for enterprise applications, web development, and cross-
platform software.
Ultimately, the choice between C++ and Java depends on the
specific requirements of the project, the target platform, and the
developer's expertise and preferences. Understanding the
differences between C++ and Java, which we have outlined in this
article, can help developers make informed decisions when
selecting the appropriate language for their next endeavour.
Frequently Asked Questions
Q. Is C++ more powerful than Java?
Both C++ and Java have their strengths, and determining which is
"more powerful" depends on the context of use. C++ is often
considered a more powerful language in terms of performance and
low-level programming control due to its closer-to-the-hardware
nature, allowing developers to directly manage memory and
optimize code for speed and efficiency. This makes C++ a preferred
choice for resource-intensive applications like game development,
operating systems, and system programming.
On the other hand, Java is renowned for its portability, ease of use,
and robust ecosystem, making it a popular language choice for
enterprise-level software development, web applications, and
Android app development. Additionally, Java's garbage collection
mechanism simplifies memory management, reducing the
likelihood of memory leaks and buffer overflows compared to C++.
Ultimately, the choice between C++ and Java depends on the
specific requirements of the project and the trade-offs between
performance, development time, and ease of maintenance.
Q. Which is better for beginners? C++ or Java?
For beginners, Java is often considered a better choice due to its
simpler syntax, automatic memory management, and extensive
documentation. Java's object-oriented nature and built-in libraries
make it easier for beginners to grasp programming fundamentals
and build applications more quickly. Additionally, Java's platform
independence and vast community support provide ample resources
and tutorials for beginners to learn from. While C++ offers greater
control and a deeper understanding of computer systems, its syntax
and concepts can be more complex for beginners to grasp initially,
making Java a more accessible starting point for novice
programmers.
Q. Java vs C++ which is better for competitive programming?
For competitive programming, both Java and C++ have their
advantages. C++ is often considered a preferred language due to its
faster execution speed, which can be crucial for solving time-
sensitive problems in competitive programming contests. Its ability
to directly manage memory and optimize code for efficiency makes
it a popular choice among competitive programmers. Additionally,
C++ provides access to powerful standard template libraries (STL),
which offer a wide range of data structures and algorithms ready
for use.
Q. Does Java support manual memory management like C++?
No, Java does not support manual memory management like C++.
In C++, software developers have explicit control over memory
allocation and deallocation using new and delete operators, which
can lead to memory leaks and segmentation faults if not managed
carefully.
In contrast, Java features automatic memory management through
garbage collection. The Java Virtual Machine (JVM) automatically
handles memory allocation and deallocation, reclaiming memory
from objects that are no longer in use. This process, known as
garbage collection, helps prevent memory leaks and reduces the
risk of runtime errors related to memory management. By
abstracting away manual memory management, Java simplifies the
development process and promotes safer, more reliable code.
Test Your Skills: Quiz Time
Click here to view quiz
Click here to view quiz
Click here to view quiz
By now, you must know all about the difference between C++ and
Java programming languages. You might also be interested in
reading:
1. Difference Between Java And JavaScript Explained In Detail!
2. Difference Between Java And Python Decoded
3. Top 100+ Java Interview Questions And Answers (2024)
4. Difference Between C And C++ Explained With Code Example
5. 51 C++ Interview Questions For Freshers & Experienced (With
Answers)

You might also like