Java Interview Questions and Answers | Core Java Interview Questions | Intellipaat

Java Interview Questions and Answers | Core Java Interview Questions | Intellipaat

Hey guys. Welcome to Intellipaat. So as we all
know that Java is one of the most popular programming languages today. So
naturally there’s a huge demand for Java developers in the IT industry. Keeping
that in mind, we have come up with this video on top Java interview questions to
help you clear your next job interview But before we move forward, do subscribe
to Intellipaat’s youtube channel so that you don’t miss out on our next videos. So
now without any further delays, let’s get started. First and foremost, the basic
question which comes to everyone’s mind is what exactly is Java. Java is a
general-purpose programming language which is object-oriented, class-based and
concurrent which is mainly designed to have a fewer implementation dependencies
as possible. Java which was called as an Oak earlier which was still being
developed. The name Oak was used by Gosling after an oak tree that stood
outside his office and basically it is one of the object-oriented programming
language meaning that it is based on objects that work together to make the
program’s do their jobs easier. Java is all around us and the java code looks
like C, C++ or C# but the code written in those languages will not work in java
in most cases without being changed right. Now we will get into the next
question. What are classes and objects in Java? Classes and objects are basic
concepts of object-oriented programming language which revolve around the real
world entities. A class is a user-defined blueprint or a prototype from which the
objects are created. In simple words, a class is like an object constructor or a
blueprint for creating objects. Class has several variables and methods. For now, we
will consider the example of dog where the dog is considered as an object and
the variables are nothing but data members such as breed, size, age, color etc. Methods are eat, sleep, sit, run and many more. This is all
about class. Now we will see, what are objects? An object is a self-contained
component which consists of state or attributes and behavior or method. In
simple words, we can say that object is a real word entity which has both state
and behavior and it’s an instance of a class. For example, we will take pen which
is an object, its name is reynolds, color is white, known as its state and it is
used to write, so writing is its behavior. Let’s jump onto the next interesting
question which has been asked in every interviews that is list the top 10
features of Java. We will see each one by one. Java is easy to learn. Well it can
be learnt in a short span of time because it uses English like syntax and
it is concurrent. Java is an open-source software where it doesn’t require you to
pay a penny to write the codes for your applications in Java. Since everyone
loves to build the software for free, Java provides it. Java finds its use in
real world applications. So guys let us discuss some of the technologies that
makes use of Java in the real world. Java is used in Android applications, web
applications, ecommerce, solar application, scientific technologies and in many
frameworks. Opportunity with high salary. Nowadays, most of them are hunting for
the Java professionals. Java programmers are highly in demand and one can earn
good salaries in MNC’s. It’s an object-oriented programming language.
Everyone knows it is impossible to write a program without object and class. The
concepts of Java are relatable and much easier. This property makes Java a
flexible, system modular and extensible programming language. Java has powerful
development tools that take Java to peak level. It also offers a high level of debugging
assistance to the programmers and makes the coding easier. REPL that is read,
evolve, print, loop and initial scripting is making a Java coding very easy where
the programmers can write it in a single go. Java has rich apis’ which make Java a
successful language. It doesn’t have any compatibility issues and it handles wide
set of procedures and functions. Also it provides Java for input/output
networking and database connections that is JDBC. Moving on to the next feature
that is Java is present everywhere. Yes of course, Java is omnipresent. It is not limited to a single domain be it a desktop
applications, mobile applications, web applications and many more. Moreover it’s
also used in banking, retail and all will use an one or the other way. So it’s not
going out to the market anytime soon. Java is portable and versatile. It’s an
open source and platform independent where the Java supports the famous tag
line that is WORA that is write once and run anywhere. It helps in dynamic
coding and it’s highly secure. Java has tons of resources and community support.
It is one of the mature programming language that requires lots of resources
which are available online and there are also free and paid online courses. On
multiple online forums like Stack Overflow, Quora, Reddit etc. It will help
them to clarify the doubts which results in the growth of community itself. Now
we will jump on to our next question. What do you mean by JDK, JRE and JVM, explain with anyone difference? JDK is a software development environment used
for developing Java applications and applets. It includes the Java Runtime
environment that is JRE, an interpreter or a loader
that is Java, a compiler that is Java C and archiver jar, a documentation
generator that is Java doc and other tools needed in Java development. Java
development kit in short JDK is a kit which provides the environment to
develop and execute the program. JDK is like a kit or a package which includes
two things one is the development tools that is to provide an environment to
develop your Java program and one more is JRE to execute your Java program. JRE
stands for java runtime environment and may also be written as Java RTE. The java
runtime environment provides the minimum requirements for executing a java
application. It consists of a java virtual machine, core classes and
supporting files. JRE that is a java runtime environment is an installation
package which provides environment to only run the Java program on your
machine. JRE is only used by them who only wants to run the Java programs that
is end-users of your system. Coming to Java Virtual Machine, it will convert the
byte code to machine specific code. It is platform dependent and provides core java functions like garbage collection, security, memory management and so on.
It’s known as Virtual because it provides an interface that doesn’t
depend on machine hardware and other operating systems that is why it makes
Java program right once and run anywhere. Java virtual machine that is JVM is
a very important part of both JDK and JRE because it is contained or inbuilt
in both. Whatever java program you run using JRE or JDK goes into JVM
and JVM is responsible for executing the Java program line by line, hence it is
also known as interpreter. Coming to the differences that is JDK vs JRE and JVM that is JRE and JDK both contain JVM which
helps to run the Java program. JVM provides the flag from independence it’s
also known as the heart for the java programming language. JRE
is used for running the java programs whereas JDK provides the development
tools which are required to run the java application. Now we will see the next
question that is why Java is platform independent? Explain how the garbage
collection takes place in Java. When we compile the java program using java C
that is java compiler converts the source code into the bytecode that is the
jar class file and these codes are readable by java virtual machine and
each operating system as its own JVM. JVM is a platform dependent but
because of it has a provides an interface that doesn’t depends on
machine hardware and other operating system. Java program is write once and
run anywhere. In Java what happens is that when an object is not referenced
anymore, garbage collection takes place and an object is destroyed automatically.
For automatic garbage collection, Java calls either systems.gc method or
runtime.gc method. During garbage collection, the memory used by the object
is automatically written or drawn back, once all the references to an object are
gone. Our next question is explain why Java is
not hundred percent object-oriented programming language and how it enables
high performance. Java is not completely an object-oriented as it supports the
primitive data types such as long, byte, int and so on which are not objects. In Java,
we use the data types such as float, double, byte, int and so on which are
clearly not object-oriented. Therefore Java is not considered as hundred percent
object-oriented. Coming to the performance, Java uses just-in-time
compiler that is JIT to enable the high performance. JIT is used to convert the
instructions into the byte codes. Coming to our next question that is what gives
Java its write once and run anywhere nature? The bytecode that is the Java
compiler converts the Java programs into a class file that is bytecode which is
the intermediate language between source code and the machine code. This bytecode
is not platform specific and can be executed on any computer. For example, it
can be written in Windows machine and it can run on Linux machine. Explain the
basic structure of Java with an example? Structure of a Java program is the
standard format released by language developer to the industry programmer. Sun
Microsystems have suggested a structure for the Java programming language for
developing the Java applications. Here is an example, the first line says about the
package details. A package is nothing but a collection of classes, interfaces and
sub-packages. A package is imported by default and this package is known as
default package. Coming to the next line, that is class hello that is nothing but
class followed by class name. A class is a keyword used for developing the
user-defined data type and every Java program must start with a concept of
class. Coming to the class name, there’s nothing but it will represent a Java
valid variable name treated as the name of a class and it is case sensitive.
Coming to the data members that is int a, b where it represents either the
instance or static and they will be selected based on the name of the class.
Next is the void display says the user-defined method, it represents either
instance or static. They are meant for performing the operations either once or
each and every time. Coming to public static void main that is the main method,
there is known as a program driver. It is not returning any value and hence its
return type must be void. It executes only once throughout the Java program
execution and hence its nature must be static. Main method must be accessed by
every Java programmer and hence whose access specifier must be public.
Coming to the block of statements that is system.out.println where we
print the statements. Block of statements represents set of executable statements
which are in term calling user-defined methods are containing business logic.
Moving on to our next question that is what are the advantages of packages in
Java? There are various advantages of defining packages in Java. Packages helps
in avoiding the name clashes. The package also provides easier access control. We
can also of the hidden classes that are not visible outside and it can be used
by the package, it is easier to locate the related classes. Now we will see the
difference between equal to equal to and dot equals method in Java with an
example. The main difference between both of them is that dot equals is a method
whereas equal to equal to is an operator. The dot equals method is used for content comparison and equal to equal to is used for address comparison.
Here is an example, we will see how we can execute this in an Eclipse IDE. First
we will create a public class test. Next, we will write the main method that
is public static void main string of args. Next, we will create two string objects
namely s1 and s2 you now we will print both of these
statements first we will use equal to equal to operator in the next
line we will print whether dot equals method now we will see the output Ranas java application now the output
comes as for the first print statement it is showing false and for the second
thing it is showing us true in the above example we are creating two objects
namely s 1 and s 2 both s 1 and s 2 refers to different objects when they
use equal to equal to operator as shown in this line it is for comparison then
the result is false as both of them have different addresses in memory coming to
this thing that is dot equals method the result is true why because it’s only
comparing the values given and s 1 and s 2 now our next question is about oops
concepts we will see this one by one with an example
oops concepts include inheritance abstraction polymorphism and
encapsulation we will say this one by one with an example the first concept is
inheritance means that one class can extend to another class so that the
quotes can be reused from one class to another class existing class is known as
superclass whereas the derived class is known as subclass for example assume
that to inherit a class we use the extends keyword here class XYZ is a
child class and class ABC is a parent class the class XYZ is inheriting the
properties and methods of ABC class which is represented as class XY Z
extents ABC coming to our next concept that is encapsulation that means binding
of data and code together to protect the data from the outside world it helps in
hiding the details of how an object does something I assume that it’s in just an
inner layout used in terms of implementation for example we will
consider the mobile phone where the inner details for how the keypad button
and the display screen are connected with each other using sockets next
concept is struction where it is useful hiding the
unwanted data and giving the relevant data it helps you focus on what the
object does instead of how it does it as younger it is an outer layout used in
terms of design for example again we will consider the mobile phone where it
has a display screen and keypad button to dial a number coming to our last
concept that is polymorphism polymorphism nothing but many forms a
single object can refer the superclass or a sub class depending on the
reference type which is called polymorphism it is nothing but a single
action performing in different ways polymorphism can be performed by method
overloading or method overriding any object that verifies more than one is a
relationship then the object is said to be polymorphic in nature for example let
us consider a class animal and let can be a subclass of animal so any cat is
animal here Kant satisfies the is a relationship for its own type as well as
its superclass animal what is the differences between method overloading
and method overriding in Java explain with an example first we will see the
overloading method overloading occurs when two or more methods in one class
have the same method name but different parameters it follows compile-time
polymorphism now we will see the example here we have created a class named as
talk with an method called bug and we have printed it as who coming to the
overloading method it will come with the for loop again we will print it as Oh
in this overloading example the two-buck method can be invoked by using different
parameters compiler know they are different because they have different
methods signature that is nothing but method name and the method parameter
list Mikko in the first method that is public
void bug there is an empty parameter in the second method with the same name
that is like white back it is with any number so
it is nothing but having different method signature coming to our next
concept that is overriding overriding means having two methods were the same
method name and parameters that is method signature one of the method is
present in the parent class and other is in child class overriding allows a child
class to provide a specific implementation of a method that is
already provided by its parent class it follows runtime polymorphism here is
an example here we have created a class called dog and we have extended using
the moon and the program continuous as follows what happens in the above
example is that the dog variable is declared to be dog during compile time
the compiler checks if the dog class has the back method as long as the dog class
as the back method the code compiles at runtime a hoon is created and assigned
to dog the java virtual machine that is the jvo knows that dog is referring to
the object moon so it calls the back method of whom this is called as dynamic
polymorphism now we will see our next question that is difference between
string string builder and string buffer with an example string variables are
stored in a constant string pool once the string references changes the old
value that exists in the constant string pool it cannot be erased now we will see
the example where in a constant string pool we have assigned the string name
object and value assigned to the string as book that a string object equal to
book assume that the name has changed from book to path now what happens that
is the older value remains in the pool whereas it has been replaced by the pen
now we will see what a string buffer here string values are stored in stuck
if the wires are changed then the new one you replaces the older wiring
the string buffer is synchronized which is thread safe and the performance is
slower than the string builder here we can see the stack with a name coil book
that a string buffer name equal to book once the name value has been changed to
pen then the book is erased in the stack this is how the string buffer works
coming to the string builder that is this is same as string buffer except for
the string builder which is not threaded safety means that it is not synchronized
so obviously performance is fast compared to the string buffer now we
will see our next question that is explained above public and private
access specifiers everyone knows methods and instances variables are members
public members are visible in the same package as well as outside package that
is for other packages here you can see the public members of class a is visible
to Class B that is within the same package as well as it is visible to the
class C which is in different package now we will see what exactly is private
access specifier private members are visible in the same class only and not
for the other classes in the same package as well as classes in the
outside packages we may see this with an example here you can notice private
members in Class A is visible only in that class it is invisible for class P
as well as Class C now our next question is what is the difference between
abstract class and an interface coming to the abstract class which as a default
constructor and it is called whenever the concrete class is instantiated
whereas interface doesn’t have any constructor and it can’t be instantiated
abstract class consists of abstract methods as well as non abstract methods
whereas in interface abstract method should
vd clip coming to our next different status the class which extends the
abstract class shouldn’t require implementing all the methods on the
abstract methods need to be implemented whereas classes which implement the
interface should provide the implementation for all of the methods
last but not the least difference that is abstract class contains instance
variables whereas interface consists of only constants now our next question is
that what is meant by collections and Java and we will list some of the
operations that are performed by collections and Java collections are
nothing but a group of objects is known as collections collection as a framework
that is designed to store the objects and manipulate the design to store the
objects collections are used to perform the following operations such as
searching sorting manipulation insertion deletion and many more all the classes
and interfaces for collecting our available and the java.util package
coming to our next question that is what is the output of the following program
here I have created a class named test inside the main method I have printed
three statement the first statement that is 10 plus 20 plus Java T point what
happens in the first case is that 10 and 20 are treated as numbers and added to
be 30 now there some 30 is treated as a string and concatenated with a string
Java T point therefore the output will be 30 Java T point coming to our next
print statement that is Java T point plus 10 plus 20 what happens in the
second case is that the string Java T point is concatenated with ten to be the
string Java T point Cal which will then be concatenated with 20 to be Java T
point 10 20 same thing happens in the third case that is but here the case has
multiplication where the numbers 10 and 20 will be multiplied first and then the
result 200 is treated as a string and concatenated with a string Java T
point to produce the output 200 Java T point now
our next question is what exactly is multi-threading what are the two ways of
implementing multi-threading in Java my death reading is a programming concept
to run the multiple tasks in a concurrent manner within a single
program threads share the same process tag and running in parallel it helps and
performance improvement of any program runnable interface is used in Java for
implementing the multi-threaded applications Java dot light dot runnable
interface is implemented by a class to support the multi-threading
multi-threaded applications can be developed in Java in any of the two
methodologies following our by using the java dot language runnable interface
classes implement this interface to enable the multi-threading there is a
run method in this interface which is implemented and also it can be developed
in java by writing a class that extend Java dot land your thread class now our
next question is what is thread and explain the states of thread along with
how to you implement thread and Java a thread EPS facilitates multitasking and
multi processing within a program it utilizes CPU effectively and helps
improve the performance of the application in Java any words the user
to use the multiple threads at a time it can be run in parallel with other
programs without any interruptions this helps in reducing or time for the
process for example assume that if a program takes 60 seconds to execute we
can use six threads which will give us the result in 10 seconds a thread in
Java can be in either of the following state already running waiting and then
dead first coming to the ready state when a thread is created it is in ready
state running a thread currently being executed in the running state a thread
waiting 40 hundred to free the certain resources is a
waiting State last but not the least that is the dead state here the thread
which has gone dead after the execution lesson the dead
State Java provides two ways to implement thread in a program interface
Java Joe’s land rotor honorable has an instance Java dot lang a thread which
requires a task to execute through an instance the thread class already
implements runnable so a user can directly override run method by
extending the thread class or by implementing the runnable interface now
we will see a small program for an increment and decrement operators here
we have created a class named operator and inside main we have declared int a
equal to four and the first print statement that is X plus plus what
happens in the first case is that here plus plus is nothing but the postfix
operator which is used in which in-turn returns the value first and then it
increments hence its output will remain as for itself coming to the second print
statement that is plus plus followed by X but this is not the case in the second
print statement where plus plus operator isn’t priority and it is prefix so it
increments first and then it returns the value as five what happens in the third
print statement here – – represents the decrement operator and the third case
postfix that is – minus operator is used in which it returns the value first and
then decrement hence its output will be remain as for itself but in the last
case what happens is that – – operator is in priority and it’s a prefix so it
determines first and then it returns the value 3 as you can see in the program
what is constructor and how many types of constructors are used in Java the
constructor can be defined as a special type of method that is used to
initialize the state of an object it is invoke when the class is instantiated
and the memory is allocated for the object every time an object is created
using the new keyword the default constructor of the class is called the
name of the constructor must be similar to the class name and also the
constructor must not have an explicitly written type based on the parameters
passed in the Constructors there are mainly two types of constructors in Java
one is the default constructor and one more is the parameterised constructor
coming to the default constructor it is one which does not accept any value the
default constructor is mainly used to initialize the instance variable with
the default values it can also be used for performing some useful tasks on
object creation a default constructor is invoked implicitly by the compiler
if there is no constructor defined in the class coming to the parameterised
constructor it is the one which can initialize the instance variables
whether given values in other words we can say that the constructors which can
accept the arguments are called parameterized constructors coming to our
next question that is what are the differences between constructor and
Method in Java a constructor is used to initialize the state of an object
whereas method is used to expose the behavior of an object a constructor must
not have any return type whereas the method must have a return type the
constructor here is invoked implicitly whereas the method is invoked explicitly
the Java compiler provides a default constructor if you don’t have any
constructor in the class but in case of method it is not provided by the
compiler in any case the constructor name must be same as the class name
whereas the method name may or may not be same as the class name
what is exception in Java explained with an exception handling keywords an
exception is an error event that can happen during the execution of a program
and disrupts its normal flow an exception can arise from a different
kind of situation such as wrong data entered by the user hardware
failure network connection failure and many more whenever any error occurs Java
Runtime environment that is JRE tries to handle it
Java exception handling framework is used to handle the runtime errors only
compile time errors are not handled by exception handling framework here there
are mainly four key words used in exception handling they are throws
coming to the throw sometimes we explicitly want to create the exception
object and then throw it to all the normal processing of the program throw
keyword is used to throw the exception to the runtime to handle it next is
throws when we are throwing any checked exception in a method and not handling
it then we need to use the throws keyword and method signature to let the
caller program know the exceptions that might be thrown by the method the color
method might handle these exceptions or propagate it to its caller method using
throws keyword we can provide multiple exceptions in the throws class and it
can be used with main method also our next keyword is try catch we use try
catch block for exception handling in our code try as a start of a block and
catches at the end of try block to handle the exceptions we can have
multiple catch blocks with a try and try catch block can be nested also catch
block requires a parameter that should be of type exception our last but not
the least that is finally keyword finally block is optional and can be
used only with try catch block since exception halts the process of execution
we might have some resources open that will not get closed so we can use
finally block finally block gets executed always weather exception occurs
or not coming to our next question that is how many types of exception can occur
in the Java program there are basically two types of exceptions that is checked
and unchecked here an error is considered as an unchecked exception but
according to the Oracle basically there are three
types of exception first one is the checked exception check exceptions are
the one which are checked at compile time for example SQL exception class not
found exception etc the classes that exchange the throwable class accept
runtime exception and error are known as checked exception current to the second
exception that is unchecked exception these are the one which are handled at
runtime biggest they cannot be checked at compile time the classes that extend
runtime exception are known as unchecked exceptions for example arithmetic
exception null pointer exception array index out of bounds exception and many
more coming to the last but not the least that is Error error cause the
program to exit since they are not recoverable for example out of memory
error as an error etc now we will see some of the programs which is asked in
every interviews here the first programmers write a simple java program
to print the Fibonacci series using both the recursion and the iteration up to a
given number first of all Fibonacci series is a
series of natural number where the next number is equivalent to the sum of the
previous two number example FM will be equal to FN minus 1 plus FN minus 2 the
first two numbers of Fibonacci series is always 1 comma 1 in this Java program
example for Fibonacci series we create a function to calculate the Fibonacci
number and then print those numbers on Java console another twist in this
question is done some time interviewer asked to write a Java program for
Fibonacci numbers using recursion so it’s better you to prepare for both
iterative as well as recursive worship of Fibonacci number the one which you
can see is the recursive function of Fibonacci series here the system got
other print as printing n1 plus n2 where it is printing 1 and 1 coming to the
second thing that is where the print Fibonacci has been called where count
minus 2 well chick and – two biggest two numbers
are already printed this is how the recursive function of Fibonacci series
works coming to the iterative function now we will see the iterative function
of Fibonacci series here we are printed the Fibonacci series
here it will take the input from the user where it calculates based on the
sum of previous two numbers here it has printed us and some of previous two will
be num1 plus num2 and then it is as int num2 – num1 and after that with some of
previous two numbers will be assigned to num – this is how the Fibonacci series
for both the recursion as well as iteration works now we will see the
program to check whether the given number is prime or not a prime number is
the number which is divisible by only two numbers 1 and itself
so if any number is divisible by any other number it is not a prime number
here is a program and the program is continued as follows what happens here
is when you run the program the how put will be 29 is a prime number what
happens in the above program is that for loop is used to determine if the given
number is prime or not we only have to loop through 2 to half of the number
because no number is divisible by more than it’s up inside the for loop we
check if the number is divisible by any number in the given dream that is 2 –
number 2 if it is divisible then the flag is set to true and we break out of
the loop the state of mind the number is not a
prime number if number isn’t divisible by any number flag is false and number
is a prime number this is how the program of prime number checking works
here is the Java program to check whether the given string is palindrome
or not enter any string as input now we use the for loops and the offense
conditions along with the equals ignore case method to compute whether the enter
string is a palindrome or not here is the source code for a Java program to
check whether a string is palindrome or not the Java program is successfully
compiled and run on the system I assume that we are entering the string input as
mom if we reverse the string also we will get the same this is what
palindrome is now we will see the main program where it has been asked by many
of the IT companies that is write a Java program to find the factorial of a
number before going through the program let us understand what factorial is
factorial of a number n is denoted as n factorial and the value of n factorial
is 1 into 2 into 3 up to n minus 1 into n so let us see the program here what
happens is that we will find factorial of a number here I assume that you are
giving the input of for it now checks for the 4 factorial that is nothing but
n factorial here the N is 4 so it is 4 factorial and then the output as 4 into
3 into 2 into 1 is equal to 24 so the output will be 24 what are the
differences between through and dross keyword in Java the throw keyword is
used to throw an exception explicitly whereas the throws keyword is used to
declare an exception the check exceptions cannot be propagated with
throw only whereas check exception can be propagated with throws the throw
keyword is followed by an instance where as throws keyword is followed by class
the throw keyword is used within the method the throws keyword is used with
the method signature you cannot throw the multiple exceptions but in case of
throws you can declare multiple exceptions for example public void
method throws I will exception SQL exception now we will seek a small
program for throws keyword here what happens is that try is going to execute
it as through 90 and the catch for example it will cut the exception here
it supports the incompatible types where the int cannot be converted to throwable
in Java the true herbal objects can only be true if they try to throw an integer
object the Paulo will show an error since we cannot
draw the basic data type from a block of code so this brings us to the end of
this video we hope you found this video informative if you have any queries do
leave a comment and we will get back to you immediately thank you for watching

10 thoughts on “Java Interview Questions and Answers | Core Java Interview Questions | Intellipaat”

  1. Guys, which technology you want to learn from Intellipaat? Comment down below and let us know so we can create in depth video tutorials for you.:)

  2. I appreciate u guys lots of stuffs explained clearly in this video… I need about python language with Django. 🤗

  3. Hi Intellilaat Team, Big Thanks for making the content like this it is really awesome content, i have 3 yrs of Java Developer and i found it very helpfull in quick revision before going to interview. I would request you to kindly make some content on Data Structure and Algorithm based on java, and Java Design Patterns

  4. 👋 Guys everyday we upload high quality in depth tutorial on your requested topic/technology so kindly SUBSCRIBE to our channel👉( ) & also share with your connections on social media to help them grow in their career.🙂

Leave a Reply

Your email address will not be published. Required fields are marked *