150+ Java Interview Questions
150+ Java Interview Questions
Introduction
1)What is Language?
➔Java is a high-level object oriented secure,robust and platform independent programming language.
-By using Java languge,we can develope various application,which are as follows.
i)Software or Standalone application
ii)Website or Webapplication
iii)Mobile application
iv)Enterprise application
v)Distributed application and Gaming application
-In the Software Application,maintenance is a challenge,so we can upload the private data program at server
location with valid username and password,in client can view,update the record,In a personal profile with the
help of Web application.
ii)JEE(Java Enterprise Edition)-Is is also known as Advance Java,By using JEE we can develop Website.
iii)JME(Java Micro Edition)-It is also known as Android Java,by using JME we can develop mobile
application.
iv)JavaFx-It is used to develop GUI application,It is outdated you have to develope JUI application now we
are using NodeJS or react.
7)History of Java.
➔-Java was developed by Sun-Microsystem but on 27th January 2010,Java was overtaken by Oracle
Corporation so now,Java is the product of oracle corporation.
-Java was developed by James Gosling.
8)What is fuction?
➔-A function is a self-defined block which is used for performing some operation calculate or printing the
Data.
9)Types of function.
10)Advantages of function.
➔i)Modularity(Dividing the bigger task into number of smaller task)
ii)Easy to understand(Once the task is divided into number of independent modules then it is easy to
understand the entire module.
➔We should pass parameter to a function for getting more information regarding the function.
the compiler will generate a very special machine code file i.e.Class file(also known as byte code).Now this
.class file we submit to JVM for execution purpose.
-The role of JVM is to load and execute the .class file.Here JVM plays a major role because it converts the
.class file into appropriate machine code instruction(operating system format) so java becomes platform
Independent language and it is highly suitable for website development.
-We have different JVM for different operating system that means JVM is platform dependent technology
Where and java is platform independent technology.
-JVM internally contain an interpreter so it is executes the code line by line.It is written in ‘C’ language
Hence platform dependent.
17)What is JVM?
➔-It is stands for Java Virtual Machine,it is responsible to load and execute the .class file.it internally contain
an interpreter and it is written by using C language.
-Java is platform dependent,each O.S contains it’s specific JVM,JVM plays a major role because it converts
.class file into appropriate operating system format.
18)Difference between Compiler and Interpretor.
➔
Compiler Interpretor
i)It scans the entire program once and converts i)It scans the program line by line for generating
v)The languages like C,C++,Java,C# and so on are v)The languages like Python,Javascript,HTML,
using compiler. Java and so on are using Interpretor.
19)What is JDK?
➔-It stands for Java Development Kit,it is a developer version that means by using JDK we can develope as we
20)What is JRE?
➔It stands for Java Runtime Environment,It is a client version so by using JRE we can only executes our java
programs.
Numeric Non-numeric
boolean(1)
byte(8-bits) float(32-bits)
Short(16-bits) double(64-bits)
int(32-bits)
long(64-bits)
23)What is the difference between Statically typed and Dynamically typed language?
➔-The languages where data type is compulsory before initialization of a variable are called statically typed
language.
-The languages where data type is not compulsory and it is optional before initialization of a variable then it
is called dynamically typed language.
-Static(Beacause the method belongs to the class,so object creation is required so that’s reason main
Method must be static.
-Void(It is a keyword.it means no return type,whenever we define any method in java and if we don’t want
to return any kind of value from that particular method then we should write void before the name of the
method.
-main()-It is a user defined method because a user is responsible to define some logic inside the main
method,as well as the execution of the program ends with main() method only.
compile.
iii)Boolean Literal
iv)Character Literal
v)String Literal
➔-No,Java is not a pure object-oriented language because it is accepting primary data types,Actually any
language which accepts primary data type is not a pure object-oriented language.
-Only object are moving in the network but not the primary data type so java has introduced Wrapper class
Concept to convert the primary data types into corresponding wrapper object.
35)Why we can’t use a local variable outside of the method or block or constructor?
➔-In java,Every methods are executed in a special memory called Stack memory.
-Stacks memory works on LIFO(Last In First Out) basis . In java,whenever we call a method then a separate
stack will be created for each and every method.
-Once the method execution is over then the corresponding method statck frame will also be deleted from
stack area,that is the reason we can’t use local variable outside of the method.
36)Member access operator(.).
➔It is called member access operator,by using this we can access the member of the class.
37)New keyword.
40)Advantages of OOPs.
41)Features of OOPs.
➔i)Class
ii)Object
iii)Abstraction
iv)Encapsulation
v)Inheritance
vi)Polymorphism
42)What is Class?
➔-A class is a model/blueprint/template/prototype for creating the object.
-A class is a logical entity , which does not take any memory.
-A class is a user – defined data type which contains data member member function.
43)What is instance or Non-static variable?
➔It is a class level variable so it has default value,If a non-static variable is defined inside a class but outside of
the method then it is called instance variable.
44)Parameter Variable.
➔-It is a method level variable hence does not have default value.
-If a variable is declared inside a method parameter then it is called Parameter Variable.
iii)Parameterized constructor.
53)How many ways we can make an object eligible for Garbage Collector?
➔i)Assigning null literal to existing reference variable.
ii)Creating an object inside a method.
➔If a final instance variable is not initialized at the time of declaration , then it is called blank final field.
iv)Multiple Inheritance
v)Hybrid Inheritance
➔Whenever Super class variable name and Sub-class variable name both are same then it is called Variable
Hiding.
.class file JVM will verify the presence of main() in the .class file . If main method is not available then it will
Generate a runtime error that “main method not found in so so class”.
64)How many ways we can load the .class file into JVM memory?
➔i)By using java command
Exception.
-java.lang.NoClassDefFoundError(It occurs when the class was present at the time of COMPILATION but at
Runtime the required .class file is not available (manually) deleted by user).
➔-It is a special block in java which is automatically executed at the time of loading .class file.
➔Writing two or more non-static method in super and sub class in such a way that method name along with
method parameter must be same is called Method Overriding.
➔While overriding the method from super class , the access modifier of sub class method must be greater or
equal in comparison to access modifier of super class method otherwise we will get compilation error.
newInstance() method.
➔A class that does not provide complete implementation is defined as an abstract class.
➔A Compile time constant is a constant that is evaluated and replace with its value at compile time rather
then runtime.
➔It is a technique through which we can make our application is independent of data type . It is represented
by <T>.
-It contains an abstract method test() which takes type parameter<T> and returns boolean value . The main
purpose of this interface to test one argument boolean expression.
-It contains an abstract method accept() and returns nothing . It is used to accept the parameter value or
consume the value.
-It provides an abstract method apply that accept one argument(T) and produces a result(R).
of type T.
98)Define UnaryOperator<T>?
➔-It is a predefined functional interface available in java.util.function sub package.
-It is a functional interface in java that represents in operation on a single operand that produces a result of
the same type as it’s operand .This is a specialization of function for the case where the operand and result
are of same type.
99)Define BinaryOperator<T>?
➔If an interface does not contain any field or method , Basically it is an empty interface then it is called
Marker interface.
EXCEPTION HANDLING
101)What is Exception Handling in java?
➔An exception is a abnormal situation or unexpected situation in a normal execution flow.
v)java.lang.NullPointerException
vi)java.lang.NumberFormatException
vii)java.util.InputMismatchException
➔If we want to declare a Universal Constants then we should use enum concept, all the universal constants
Must be seprated by comma and at the end ; is optional.
104)When we use try block?
➔Whenever our statements is error suspecting statement or risky statement then we should write the
Statement inside the try block.
➔ The finally block is used to write code that must be executed, even if an exception is thrown or not.
responsible to create the exception object with JVM as well as throw the exception object to the nearest
catch-block, but if a developer want’s to throw exception object explicitely then we should use throw
keyword.
-throw new ArithmaticException();
-throw new LowBalanceException();
After using throw keyword the control will transfer to the nearest catch block so after throw keyword
Statements, the remaining statements are un-reachable.
-throws
throws keyword describes that the method might throw an Exception, It also might not, It is used only
at the end of a method declaration to indicate what exception it supports or what type of Exception it
might throw.
It is used to skip from the current situation so now the exception will be propagated to the caller method
or JVM for handling purpose.
It is mainly used to work with Checked Exception.
MULTITHREADING
113)What is Multitasking in java?
➔In multitasking multiple tasks can concurrently work with CPU so, our task will completed as soon as
possible.
114)Types of Multitasking.
➔-In order to create user-defined thread we can use the following two packages.
i)java.lang.package
ii)java.util.concurrent sub package
➔If we want to put a thread into temporarly waiting state then we should use sleep() method.
120)If we write Thread.sleep(1000) then exactly after 1 sec the Thread will re-start?
➔No, we can’t say that the Thread will direcly move from waiting state to Running state.
The thread will definitely wait for 1 sec in the waiting state and then again it will re-enter into runnable
State which is control by Thread Schedular so we can’t say that the thread will re-start just after 1 sec.
-Here the Currently executing thread stops it’s execution and the thread goes into the waiting state. The
thread goes into the waiting state. The current thread remains in the wait state until the thread on the
which the join method is invoked has achieved it’s dead state.
122)Define the Limitation of Multithreading?
➔-In Data Race or Race Condition, all the thread try to access the resource at the same time so the result may
be corrupted.
-In multithreading if we want to perform read operation and data is not updatable then multithreading is
good but if the data(modified data) then multithreading may produce some wrong result or wrong data.
123)What is Synchronization?
➔Synchronization makes sure that only one thread can access a critical section of code at a time.
➔In method level synchronization, the entire method gets synchronized so all the thread will wait at method
Level and one thread will enter inside synchronized area.
COLLECTION FRAMEWORK
126)What is Collection Framework in java?
➔The Collection Framework in Java is a set of classes and interfaces that help you store, manage, and
process groups of objects (called collections) easily and efficiently.
(common elements)
-d)public boolean removeAll(Collection c):- It is used to delete all the elements from the existing collection.
-e)public boolean remove(Object element):- It is used to delete an element from the collection based on
the object.
-f)public int size():- It is used to find out he size of the Collection (Total number of elements available).
-g)public void clear():- It is used to clear all the elements at once from the collection.
-We can perform automatic sorting by using Collection.sort(List list) because sort() accept List as a
parameter.
-It stores the elements on the basis of index because internally it is using array concept.
-In list interface few classes are dynamically growable like Vector and Array List.
-d)public void all(int index , Object o):- Insert the elements based on the index position)
-e)public boolean addAll(int index , Collection c):- Insert the collection based on the index position.
-f)public Object get(int index):- To retrieve the elements based on the index position.
-g)public Object remove(int index):- Remove the elements based on the index position.
-h)public void set(int index , Object o):- To override or replace the existing elements based on the index
Position).
-i)public boolean remove(Object element):- Remove the elements based on the object element. It is the
Collection interface method extended by List interface.
-j)public int indexOf():- Index position of the elements.
-k)public Iterator iterator():- To fetch or iterate or retrive the elements from the collection in forward
direction only.
-l)public ListIterator listIterator():- To fetch or iterate or retrieve the elements from the collection in
Forward and backward direction.
130)How many ways we can fetch the Collection Object?
➔-1)By using toString() method of respective class [JDK 1.0]
-2)By using ordinary for loop [JDK 1.0]
-3)By using forEach() loop [JDK 1.5]
-4)By using Enumeration interface [JDK 1.2]
direction.
iii)ArrayList<E>:- ArrayList in Collection is a resizable array in Java. It stores a group of objects and grows
automatically when you add more items.
iv)LinkedList<E>:- LinkedList in Collection is a special type of list where elements are connected like a chain.
Each element knows where the next (and previous) element is.
-vi)Object removeLast()
ix) keySet()
• Returns a set of all the keys in the map.
x) values()
• Returns a collection of all the values in the map.
xi) entrySet()
146)Define HashMap<K,V>?
➔ HashMap<K, V> is a class in Java that stores data in key-value pairs. It allows you to quickly find, add,
update, or remove values by using their keys. Keys must be unique, but values can be duplicated. HashMap
does not maintain any order of its elements.
the order in which you added the entries. So, when you print or loop through it, you see the data in the
same order you inserted.
meaning it is safe to use in multi-threaded programs. It does not allow any null key or null value.
150)Define SortedMap<K,V>?
➔ SortedMap<K, V> is a special type of Map where the keys are always kept in sorted (ascending) order.
It automatically sorts the keys when you add them.
151)Define TreeMap<K,V>?
➔ TreeMap<K, V> is a class in Java that stores key-value pairs and automatically keeps the keys in sorted
(ascending) order. It is part of the SortedMap family.
152)What is Generics?
➔ Generics means writing code that can work with any data type (like Integer, String, etc.) without rewriting
the code again and again. without rewriting the code again and again.