Basic Java Interview Questions
Basic Java Interview Questions
Object-Oriented – java is based on object-oriented programming where the class and methods
describe about the state and behavior of object.
Portable – Java program gets converted into Java Byte Codes that can be executed on any
platform without any dependency.
Platform independent – java works on “write once and run anywhere” as it supports multiple
platforms like Windows, Linux, Mac, Sun Solaris, etc.
Robust – Java has a strong memory management as there is no pointer allocations. It has
automatic garbage collection that prohibits memory leaks.
Interpreted – java compiler converts the codes into Java Byte Codes which are then interpreted
and executed by Java Interpreter.
JVM stands for Java Virtual Machine which provides runtime environment for Java Byte Codes
to be executed.
JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime.
JDK (Java Development Kit) consists of JRE along with the development tools required to write
and execute a program.
7. Define Inheritance.
Java includes the feature of inheritance which an object-oriented programming concept. Inheritance lets
a derived class to inherit the methods of a base class.
Read this tutorial to get clear understanding of Inheritance in Java.
When a Java program contains more than one methods with the same name but different properties,
then it is called method overloading.
10. Explain the creation of a thread-safe singleton in Java using double-checks locking
Singleton is created with double checked locking as before Java 5 acts as an broker and it’s been
possible to have multiple instances of Singleton when multiple threads creates an instance of Singleton
at the same time. Java 5 made it easy to create thread-safe Singleton using Enum. Using a volatile
variable is essential for the same.
Learn more about Java threading in this tutorial.
Storage area is Heap and modified easily. Storage is Heap and can be modified.
If an element is inserted into the Array List, it increases Vector defaults to doubling size of its array.
its Array size by 50%.
Array List does not define the increment size. Vector defines the increment size.
Array List can only use Iterator for traversing an Array Except Hashtable, Vector is the only other class which uses bot
List. Enumeration and Iterator.
Iterator Enumeration
Iterator is an interface found in the java.util package. Enumeration is an object that generates elements one at a
time. Used for passing through a collection for unknown size
1. i) hasNext() 1. i) hasMoreElements()
2. ii) next() 2. ii) nextElement()
3. iii) remove()
Iterators allow removing elements from the given It is used for passing through a collection, usually of unknow
collection during the iteration with well-defined size.
semantics.
Iterator method names have been improved. The traversing of elements can only be done once per
creation
14. Difference between the Inner Class and Sub Class.
It’s a class which is nested within another class. It’s a class which inherits from another class
called super class.
Inner class provides the access rights for the class which is nesting it and Sub-class provides access to all public and
that can access all variables and methods defined in the outer class. protected methods and fields of its super class
15. Can we execute any code, even before the main method? Explain?
Yes, We can execute any code, even before the main method. We are using a static block of code in the
class when creating the objects at load time of class. Any statements within this static block of code will
get executed one time while loading the class, even before the creation of objects in the main method.
Prepare yourself for the Java Certification with comprehensive Online Java Training.
Java doesn’t support multiple inheritance. Because we cannot use different methods in one class it
creates an ambiguity.
Example:
class Intellipaat1
{
void test()
{
system.out.println("test() method");
}
}class Intellipaat2
{
void test()
{
system.out.println("test() method");
}
}Multiple inheritance
class C extends Intellipaat1, Intellipaat2
{
………………………………………….
…………………………………………..
}
Intellipaat1 and Intellipaat2 test() methods are inheriting to class C
So which test() method C class will take. As Intellipaat1 & Intellipaat2 class test () methods are different,
So here we would face ambiguity.
18. Are constructors inherited? Can a subclass call the parent's class constructor?
We cannot inherit a constructor. We create an instance of a subclass using a constructor of one of its
superclass. Because override the superclass constructor is not our wish so that, we override a superclass
constructor, then we destroy the encapsulation abilities of the language.
Check the insightful tutorial to learn more about Java Constructors.
Expansion of JSON is “JavaScript Object Notation”, It is a much lighter and readable alternative to XML. It
is an independent and easily parse-able in all programming languages. It is primarily used for
Communicating between client – server or server -server communication. It is a much lighter and
readable alternative to XML.
clone() – This method helps to create and return a copy of the object.
equals() – This method helps to compare
finalize() – Called by the garbage collector on an object
getClass() – It helps to return the runtime class of an object.
hashCode() – Helps to return a hash code value for the object.
toString() – helps to return a string representation of the object.
notify(), notifyAll(), and wait() – It helps to synchronize the activities of independently running threads in
a program.
Read this tutorial to learn more about Java Methods.
If we have visited website to searching the information, we will get the information in different
languages and in different formats when a client makes an HTTP request to a server, the client can also
specify the media types here. The client can specify what it can accept back from the host and on the
basis of availability the host will return to the client. This is known as content negotiation because client
and server negotiated on the language and format of the content to be shared.
23. Can we import same package/class two times? Will the JVM load the package twice at runtime?
A package or class can be inherited multiple times in a program code. JVM and compiler will not create
any issue. Moreover JVM automatically loads the class internally once regardless of times it is called in
the program.
A class which contains the abstract keyword in its declaration is known as abstract class. It can have
abstract and non-abstract methods (method with body).
1. This class can have public, private, protected or constants and default variables.
2. It needs to be extended and its method implemented. It cannot be instantiated.
3. If a class has at least one abstract method, then the class must be declared abstract.
Java Annotations is a tag which symbolizes metadata associated with class, interface, methods,
fields, etc.
Annotations do not directly influence the operations.
The additional information carried by annotations are utilized by java compiler and JVM.
Pointers are susceptible and slight carelessness in their use may result in memory problems and hence
Java basically manages their use.
Static loading – Classes are loaded statically with operator “new”.Dynamic class loading – It is a
technique for programmatically invoking the functions of a class loader at run time. The syntax
isClass.forName (Test className);
28. Explain Struts 1 Classes are not Thread Safe whereas Struts 2 classes are thread safe?
Struts 1 actions are a singleton. So all threads operate on the single action object and hence make it
thread-unsafe.Struts 2 actions are not a singleton and a new action object copy are created each time a
new action request is made and hence it threads safe.
JAXP: – Stands for Java API for XML Processing. This provides a common interface for creating and using
DOM, SAX, and XSLT APIs in Java regardless of which vendor’s implementation is actually being used.
JAXB: – It Stands for Java API for XML Binding. This standard defines a system for a script out Java
objects as XML and for creating Java objects from XML structures.
31. How can we find the actual size of an object on the heap?
In Java, there is no way to find out the actual size of an object on the heap.
Java provides a Collection API which provides many useful methods which can be applied to a set of
objects. Some of the important classes provided by Collection API include ArrayList, HashMap, TreeSet,
and TreeMap.
Java.Lang.throwable – It is the super class of all exception classes and all exception classes are derived
from this base class.
The Vector class provides the ability to execute a growable array of objects. Vector proves to be very
useful if you don’t know the size of the array in advance, or we need one that can change sizes over the
lifetime of a program.
35. What is the difference between transient and volatile variable in Java?
Transient: In Java, it is used to specify the variable is not being serialized. Serialization is a process of
saving an object’s state in Java. When we want to persist and the object’s state by default, all instance
variables in the object are stored. In some cases, if we want to avoid persisting few variables because we
don’t have the necessity to transfer across the network. So, declare those variables as transient.
If the variable is confirmed as transient, then it will not be persisted. Transient keyword is used with that
instance variable which will not participate in the serialization process. We cannot use static with a
transient variable as they are part of the instance variable.Volatile: Volatile keyword is used with only
variable in Java and it guarantees that the value of the volatile variable will always be read from main
memory and not from Thread’s local cache, it can be static.
36. Why does Map interface not extend the Collection interface in the Java Collections Framework?
The map interface is not compatible with the Collection interface. Because Map requires key as well as
value, for example: if we want to add key-value pair so we will use put(Object key , Object value). There
are two parameters required to add an element to HashMap object. In Collection interface add(Object
o) has only one parameter.
The other reasons are Map supports valueSet, keySet as well as other suitable methods which have just
different views from the Collection interface.
39. What is the default size of load factor in hashing based collection?
Default Size = 0.75. The default capacity is computed as initial capacity * load factor.
40. What are the differences between the JSP custom tags & Java beans?
The expression language is used in JSP to simplify the accessibility of objects and It provides many
objects that can be used directly like param, requestScope, sessionScope, applicationScope, request,
session etc.
Implicit objects, also called as pre-defined variables, are created by JSP Engine inside the service method
so that it can be accessed directly without being declared explicitly.
43. Define a cookie and what are the difference between session and cookie?
A cookie is a small piece of information, this piece of information sends to the browser by a web server.
The browser stores the cookies for every Web server in a local file. In a future request or future
reference, the browser sends all stored cookies for that detailed Web server.
The difference between cookies and sessions are:
The session is working, regardless of the settings on the client browser. The client may have
selected to disable cookies.
The session and cookies also differ in storing the amount of information. The HTTP session is
able to store any Java object, while a cookie can only store String objects.
Tunneling refers to encapsulating private network data/information and transmitting it through a public
network. Instead of sending data as a packet, tunneling encrypts the data and encapsulates in a
connection. It allows the outside clients to collect all the data sent by client-side Object Request Broker
(ORB) that needs to be sent to server-side ORB. HTTP tunneling masks other protocol requests as HTTP
requests.
Lazy loading is a kind of setting that decides whether to load the child entities with the parent entities or
not. When enabling this feature the associated entities will be loaded only when it is requested directly.
The default value of this setting is true which stops child entities from loading.
We can fetch records from the database by the query method of JdbcTemplate. There are two interfaces
to do this:
1. ResultSetExtractor
2. RowMapper
The Dispatcher Servlet class works as the front controller in Spring MVC.
1. Transient: When objects are just created having no primary key are in transient state. Here the
objects are associated with any session.
2. Persistent: When the session of an object is just opened and its instance is just saved or
retrieved, it is said to be in persistent state.
3. Detached: When the session of an object is closed, it is said to be in detached state.
If we mark a class as mutable=”false”, the class will be treated as an immutable class. The default value
of mutable is “true”.