You are on page 1of 16

INTERVIEW QUESTIONS

1. What are constructors?


Þ constructor is a set of statement which is executed each time
when an instance is created using new keyword.
Þ Name of constructor should be same as class name.
Þ Return type is not allowed for constructor. (void is not
allowed)
Þ Static keyword is not allowed for constructor.
Þ constructor cannot return a value.
Þ Constructor is used to initialize the object.

2. What is constructor overloading?


Þ Creating multiple constructor in a same class with different
arguments is known as constructor overloading.
Þ constructor overloading is used to initialize object in
different ways.

3. What is constructor chaining?


Þ Constructor chaining refers to the ability to call a
constructor inside another constructor. You can use a
constructor chain either within the same class or even with
another one. For the latter, the constructor should be
through inheritance from the super class.
4. Difference b/w static and local variables?

STATIC

Þ A static variable is declared using the static keyword.


Þ Memory is allocated at the run-time when the class is loaded
and only once. However, final keyword can be used to show
that the value of a static variable remains the same after
initialization.
Þ The static as well as non-static methods can access a static
variable.
Þ A static variable is common to every object of the class.
Þ It is like a global variable, available to all.
LOCAL

Þ A non-static variable is declared as a regular variable.


Þ Memory is allocated when a new object is created.
However, each time a new object is created new memory
space is allotted which can lead to wastage of memory.
Þ The static methods cannot use a non-static variable.
Þ A non-static variable is accessible to its instance(where it is
defined).
Þ It is a local variable and available to object of the class.

5. Var type in java?


Þ The var keyword was introduced in Java 10. Type inference
is used in var keyword in which it detects automatically the
datatype of a variable based on the surrounding context.
6. Exceptions & how to handle it?
Þ An exception is an event, which occurs during the execution
of a program, that disrupts the normal flow of the program's
instructions. When an error occurs within a method, the
method creates an object and hands it off to the runtime
system.
Þ Put the code you want to run in the try block, and any Java
exceptions that the code throws are caught by one or more
catch blocks. This method will catch any type of Java
exceptions that get thrown. This is the simplest mechanism
for handling exceptions.

7. One practical example of exception?


Þ The car owner always keeps an extra tire as an alternative
on a long-distance journey. He changes the punctured tire
with a new tire. After changing the tire, he continues the rest
of the journey. This alternative way is called exception
handling.

8. Difference b/w final, finally, finalize?

FINAL

Þ final is the keyword and access modifier which is used to


apply restrictions on a class, method or variable.
Þ Final keyword is used with the classes, methods and
variables.
FINALLY

Þ finally, is the block in Java Exception Handling to execute


the important code whether the exception occurs or not.
Þ Finally, block is always related to the try and catch block in
exception handling.

FINALIZE

Þ finalize is the method in Java which is used to perform clean


up processing just before object is garbage collected.
Þ finalize() method is used with the objects.

9. How does multicatch block works in java?


Þ In Java, a single try block can have multiple catch blocks.
When statements in a single try block generate multiple
exceptions, we require multiple catch blocks to handle
different types of exceptions. This mechanism is
called multi-catch block in java.
Þ Each catch block is capable of catching a different
exception. That is each catch block must contain a different
exception handler.
10. What is Inheritance. Give me practical example where you
used in your project?
Þ process of acquiring properties of 1type to another type is
known as inheritance.
Þ the class / type from which properties are acquired is known
as super class or base class.
Þ The class / type to which properties are acquired is known
as sub class or derived class.
Þ Java 11provides a keyword ‘extends’ for inheritance
subclass extends superclass.

EX: -
§ We inherit certain properties from the class ‘Human’ such as
the ability to speak, breathe, eat, drink, etc. We can also
take the example of cars. The class ‘Car’ inherits its
properties from the class ‘Automobiles’ which inherits some
of its properties from another class ‘Vehicles’.

11. What is Abstraction. Where did u used that in your project?


Þ Process of hiding the compiler implementation details and
providing implementation details and providing essential
functionalities to user is known as abstraction.
Þ In java interface is used to achieve abstraction.

EX: -
§ TV remote. The remote has different functions like on/off,
change channel, increase/decrease volume etc. You use
these functionalities just pressing the button. The internal
mechanism of these functionalities are abstracted from you
as those are not essential for you to know.

12. What is Encapsulation. Give one practical example?


Þ Encapsulation in Java is a process of wrapping code and
data together into a single unit, for example, a capsule which
is mixed of several medicines.
Þ We can create a fully encapsulated class in Java by making
all the data members of the class private. Now we can use
setter and getter methods to set and get the data in it.
Þ The Java Bean class is the example of a fully encapsulated
class.

EX: -
§ School bag is one of the most real examples of Encapsulation.
School bag can keep our books, pens, etc.
§ When you log into your email accounts such as Gmail, Yahoo Mail,
or Rediff mail, there is a lot of internal processes taking place in the
backend and you have no control over it.

13. What is Polymorphism?


Þ Polymorphism is a concept by which we can perform a single
action in different ways.
Þ Polymorphism is derived from 2 Greek words: poly and
morphs. The word "poly" means many and "morphs" means
forms. So, polymorphism means many forms.
Þ Polymorphism are two types
• Runtime polymorphism.
• Compile time polymorphism.

14. What is Run time polymorphism?


Þ Runtime polymorphism is a process in which a call to an
overridden method is resolved at runtime rather than
compile-time.
Þ It is also known as or dynamic method dispatch

15. What is Compile time polymorphism?


Þ Compile-time polymorphism is a polymorphism that is
resolved during the compilation process.
Þ Overloading of methods is called through the reference
variable of a class.
Þ Compile-time polymorphism is achieved by method
overloading and operator overloading.
Þ Changing the implementation of super class method in sub
class is known as overriding.
Þ For overriding inheritance is must.
Þ Overriding method in subclass must have same method
name, same args, and same return type.
Þ Overriding method in subclass can have wider access
modifier, but can’t have weaker access modifier.
Þ Only non-static methods can be overriding.
Þ Private method, final method and constructors cannot be
overridden.
16. What is Data Hiding?
Þ Data hiding is a software development technique specifically
used in object-oriented programming (OOP) to hide internal
object details (data members). Data hiding ensures exclusive
data access to class members and protects object integrity
by preventing unintended or intended changes.

17. Why strings are immutable?


ÞString is Immutable in Java because String objects are
cached in String pool. Since cached String literals are
shared between multiple clients there is always a risk, where
one client's action would affect all another client.

18. What are mutable objects?


ÞThe objects in which you can change the fields and states
after the object is created are known as Mutable
objects. Example: java.util.Date, StringBuilder, and etc.

19. What are immutable objects?


ÞThe objects in which you cannot change anything once the
object is created are known as Immutable
objects. Example: Boxed primitive objects like Integer,
Long and etc.

20. Difference b/w string buffer & string builder?

STRING BUFFER
ÞStringBuffer is synchronized i.e. thread safe. It means two
threads can't call the methods of StringBuffer
simultaneously.
ÞStringBuffer is less efficient than StringBuilder.
ÞStringBuffer was introduced in Java 1.0

STRING BUILDER
ÞStringBuilder is non-synchronized i.e. not thread safe. It
means two threads can call the methods of StringBuilder
simultaneously.
ÞStringBuilder is more efficient than StringBuffer.
ÞStringBuilder was introduced in Java 1.5

21. What is transient keyword?


Þ In Java, Serialization is used to convert an object into a
stream of the byte. The byte stream consists of the data of the
instance as well as the type of data stored in that instance.
Deserialization performs exactly opposite operation. It
converts the byte sequence into original object data. During
the serialization, when we do not want an object to be
serialized we can use a transient keyword.

22. What are threads. Give one practical example?


Þ A thread in Java is the direction or path that is taken while
a program is being executed. Generally, all the programs
have at least one thread, known as the main thread, that is
provided by the JVM or Java Virtual Machine at the starting
of the program’s execution.

EXAMPLE: -
23. What is synchronized keyword in java?
ÞSynchronized blocks in Java are marked with the
synchronized keyword. A synchronized block in Java is
synchronized on some object. All synchronized blocks
synchronize on the same object can only have one thread
executing inside them at a time.

24. Thread life cycle?


ÞAccording to Sun microsystems, there are 4 states in the
java thread life cycle. They are:
ÞNew: - A thread is in the “New” state, when an object of
the thread class is instantiated but the “start” method is
not invoked.
§ Runnable: -When the “start” method has been invoked on
the thread object. In this state, the thread is either waiting
for the scheduler to pick it up for execution or it’s already
running. Let us call the state when the thread is already
picked for execution, the “running” state.
§ Non-Runnable(Blocked , Timed-Waiting): - When the
thread is alive, i.e., the thread class object exists, but it
cannot be picked by the scheduler for execution. It is
temporarily inactive.
§ Terminated: - When the thread completes execution of its
“run” method, it goes into the “terminated” state. At this
stage, the task of the thread is completed.

25. Runnable interface?


ÞThe java.lang.Runnable is the interface that is to be
implemented by the class whose instances are intended to be
executed by a thread. There are two ways to start a new
Thread – Subclass Thread and implements the Runnable
interface. There is no need for subclassing Thread when the
task can be done by overriding only run() method of
Runnable.

26. Thread Pool. Where we can use that practically?


ÞJava Thread pool represents a group of worker threads that
are waiting for the job and reused many times.
ÞIn the case of a thread pool, a group of fixed-size threads is
created. A thread from the thread pool is pulled out and
assigned a job by the service provider. After completion of
the job, the thread is contained in the thread pool again.
ÞIt is used in Servlet and JSP where the container creates a
thread pool to process the request.
27. What is Volatile keyword?
ÞVolatile keyword is used to modify the value of a variable by
different threads. It is also used to make classes thread safe.
It means that multiple threads can use a method and instance
of the classes at the same time without any problem. The
volatile keyword can be used either with primitive type or
objects.

28. What are enums?


ÞAn enum is a special "class" that represents a group
of constants (unchangeable variables, like final variables).
ÞTo create an enum, use the enum keyword (instead of class
or interface), and separate the constants with a comma.
29. Wrapper classes?
ÞThe wrapper class in Java provides the mechanism to
convert primitive into object and object into primitive.
ÞSince J2SE 5.0, autoboxing and unboxing feature convert
primitives into objects and objects into primitives
automatically. The automatic conversion of primitive into an
object is known as autoboxing and vice-versa unboxing.

30. Marker interface?

ÞAn interface that does not contain methods, fields, and


constants is known as marker interface. In other words, an
empty interface is known as marker interface or tag
interface. It delivers the run-time type information about an
object. It is the reason that the JVM and compiler have
additional information about an object.
The Serializable and Cloneable interfaces are the example
of marker interface. In short, it indicates a signal or
command to the JVM.

31. New feature of java version 8?


Þ Lambda expressions,
Þ Method references,
Þ Functional interfaces,
Þ Stream API,
Þ Default methods,
Þ Base64 Encode Decode,
Þ Static methods in interface,
Þ Optional class,
Þ Collectors class,
Þ ForEach() method,
Þ Nashorn JavaScript Engine,
Þ Parallel Array Sorting,
Þ Type and Repating Annotations,
Þ IO Enhancements,
Þ Concurrency Enhancements,
Þ JDBC Enhancements etc.

32. Memory of jvm (Stack, heap)?


Java Heap Space
ÞJava Heap space is used by java runtime to allocate memory
to Objects and JRE classes. Whenever we create an object,
it’s always created in the Heap space.
ÞGarbage Collection runs on the heap memory to free the
memory used by objects that don’t have any reference. Any
object created in the heap space has global access and can
be referenced from anywhere of the application.

Java Stack Memory


ÞJava Stack memory is used for the execution of a thread.
They contain method-specific values that are short-lived
and references to other objects in the heap that is getting
referred from the method.
ÞStack memory is always referenced in LIFO (Last-In-First-
Out) order. Whenever a method is invoked, a new block is
created in the stack memory for the method to hold local
primitive values and reference to other objects in the
method.
ÞAs soon as the method ends, the block becomes unused and
becomes available for the next method.
Stack memory size is very less compared to Heap memory.

33. Difference b/w array list and link list?

ARRAYLIST
ÞArrayList internally uses a dynamic array to store the
elements.
ÞManipulation with ArrayList is slow because it internally
uses an array. If any element is removed from the array, all
the other elements are shifted in memory.
ÞAn ArrayList class can act as a list only because it
implements List only.
ÞArrayList is better for storing and accessing data.
ÞThe memory location for the elements of an ArrayList is
contiguous.
Þ Generally, when an ArrayList is initialized, a default
capacity of 10 is assigned to the ArrayList.
ÞTo be precise, an ArrayList is a resizable array.

LINKEDLIST
ÞLinkedList internally uses a doubly linked list to store the
elements.
ÞManipulation with LinkedList is faster than ArrayList
because it uses a doubly linked list, so no bit shifting is
required in memory.
ÞLinkedList class can act as a list and queue both because it
implements List and Deque interfaces.
ÞLinkedList is better for manipulating data.
ÞThe location for the elements of a linked list is not
contagious.
ÞThere is no case of default capacity in a LinkedList. In
LinkedList, an empty list is created when a LinkedList is
initialized.
ÞLinkedList implements the doubly linked list of the list
interface.

34. Difference hash map, hash table, hash set?


HASH SET:
ÞHash Set inherits Abstract Set class and implements Set
interface. Set objects are always unique and no duplicate
objects are allowed. One null key value is allowed. The
hashing mechanism is used to insert the objects into a Hash
Set.

HASH MAP:
ÞHashMap class in java, implements the map interface by
using a Hash Table. It inherits Abstract Map class and
implements the Map interface. It represents a group of
objects and every object will be in key-value pair form. It
maintains no order for its elements. Duplicate key is not
allowed. It can have only one null as key and multiple null
as values.

HASH TABLE:
ÞHash table inherits Dictionary class and implements Map
interface. Hash table contains elements/objects/items in
key-value pair and does not allow any duplicate key. It is
Thread-Safe because of its synchronized nature. The null is
not allowed for both key and value. The hash code () method
is used to find the position of the elements.

35. What are comparators?


ÞJava Comparator is an interface for sorting Java objects.
Invoked by “java.util.comparator,” Java Comparator
compares two Java objects in a “compare(Object 01, Object
02)” format.
ÞUsing configurable methods, Java Comparator can
compare objects to return an integer based on a positive,
equal or negative comparison.

36. stream API?


ÞStream API was introduced in Java 8. It is used to process
the collections of objects. With the help of Streams and
Lambda functions, we can write clean, concise and
understandable code.
ÞStreams is pipeline of operations, which we can use to
evaluate the data.

You might also like