100% found this document useful (2 votes)
7K views

Grokking The Java Developer Interview - More Than 200 Questions To Crack The Java, Spring

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
7K views

Grokking The Java Developer Interview - More Than 200 Questions To Crack The Java, Spring

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 330

Grokking The Java Developer Interview

More Than 200 Ques ons To Crack The Java, Spring,


SpringBoot
& Hibernate Interview

JATIN ARORA
GROKKING THE JAVA DEVELOPER INTERVIEW
COPYRIGHT © 2020 BY JATIN ARORA.
All rights reserved. No part of this book may be reproduced,
distributed, or transmi ed in any form or by any means, including
photocopying, recording or other electronic or mechanical
methods, without the prior wri en permission of the author,
except in the case of brief quota ons embodied in cri cal reviews
and certain other non-commercial uses permi ed by copyright
law.
For permission or any other informa on, contact me at:
ja [email protected]
Preface
Grokking The Java Developer Interview helps you to crack a Java, Spring
& Hibernate interview.
This book covers enough details of a Java/Spring topic that will surely
help the absolute beginners and if you are already familiar with Java &
Spring, then this book will help you to brush-up on the concepts.
The book has more than 200 ques ons that are frequently asked during
an interview for Java, Spring, SpringBoot & Hibernate profile. Some of
the important topics like Mul -threading, Collec on framework,
Singleton Pa ern, SpringBoot Annota ons and many more are covered
in detail. Most of these topics are explained with code examples that
will help you to quickly grasp the concept.

Who is this book for?


This book is for you if you are either preparing for an interview, planning
to move into this field in future, brushing up your Java & Spring skills or
just want to get an in-depth overview of the field. This book provides
the most important and frequently asked ques ons along with their
solu ons.
Who is this book NOT for?
This book is not for you if you are looking for an in-depth study of Java
or Spring. The objec ve of this book is not to discuss the ongoing
research or challenges in this industry or serve as a subs tute for a
course book.
If you follow this book diligently, you would be be er equipped to face
any Java interview. Whether you are a beginner or an intermediate level
expert in Java, this book has enough juice for you.

This book contains a lot of code examples, most of the code snippets
are displayed in image format that you can zoom-in and out of.
However, if some image is not clearly visible on your device, you can also
refer to the GitHub repository for this book. The GitHub repo contains
all of the snippets shown in this book and it can be found at:

h ps://github.com/reachja n/Grokking-The-Java-Developer-
Interview

Contact Informa on
I would love to hear from you.
For feedback or queries, you can contact me at
ja [email protected]

Your valuable sugges ons to improve the book are always welcomed.
I wish you all the best and I am confident that this book will help you in
making that job switch that you are looking for.
Happy Learning, Cheers :)
-Ja n Arora
Acknowledgements
I thank my brother, Sumit Kumar, for encouraging me to take up this
project. From reading early dra s to advising me on the structure of the
book to making sure that it was wri en in a manner that was coherent,
organized and engaging at the same me, his role has been pivotal in
the making of this book.
To elaborate a li le on his background, Sumit is an UChicago Alum, and
presently working for Amazon, Sea le as a Research Scien st at Alexa AI.
He has also worked with Samsung, India as a Lead Engineer. He is an
ac ve blogger and regularly post ar cles on Data structures and
Algorithms, System Design, NLP and many more interes ng topics. You
can find those at:
h ps://medium.com/@sumit.arora
h ps://blog.reachsumit.com

To Sumit, for his pa ence in reading through various preliminary


versions of this book, and his vital sugges ons. Thank you brother for
never saying no to my numerous feedback requests and providing all the
construc ve cri cism. I dedicate this book to him.
I would like to express my sincere gra tude to my family, friends, and
everyone who mo vated me throughout. Thanks for the support, the
honest feedback, and for everything else that has helped to make this
book possible in its current form.

-Ja n Arora
Table of Content s
Preface
Acknowledgements
Ques on 1: What are the 4 pillars of OOPS?
Ques on 2: What is an abstract class?
Ques on 3: Does Abstract class have constructor?
Ques on 4: What is an Interface?
Ques on 5: Difference between abstract class and interface
Ques on 6: What to choose – interface or abstract class
Ques on 7: Why Java 8 has introduced default methods?
Ques on 8: Why Java 8 has introduced sta c methods?
Ques on 9: Why Java does not allow mul ple inheritance?
Ques on 10: What are the rules for Method Overloading and Method
Overriding?
Ques on 11: Can we override final methods?
Ques on 12: Can constructors and private methods be overridden?
Ques on 13: What is final keyword and where it can be used?
Ques on 14: What is excep on and excep on handling?
Ques on 15: Difference between error and excep on
Ques on 16: What are the different types of excep ons?
Ques on 17: How excep on handling is done in java?
Ques on 18: Can we write a try block without catch block?
Ques on 19: How to handle mul ple excep ons together?
Ques on 20: When finally block will not get executed
Ques on 21: Difference between throw and throws keyword. And
discuss Excep on Propaga on
Ques on 22: Excep on handling w.r.t. method overriding
Ques on 23: Programs related to Excep on handling and return keyword
Ques on 24: How to make your own custom excep on class?
Ques on 25: How to make custom checked / unchecked excep on?
Ques on 26: What happens when you throw an excep on from finally
block?
Ques on 27: What will be Output of below program related to try-catch-
finally?
Ques on 28: Explain try-with-resources
Ques on 29: Why String is Immutable?
Ques on 30: What does the equals() method of String class do?
Ques on 31: Explain StringBuffer and StringBuilder
Ques on 32: Explain the output of below program related to equals()
method of StringBuilder
Ques on 33: When to use String/StringBuffer/StringBuilder
Ques on 34: Explain equals and hashcode contract
Ques on 35: What is Marker Interface?
Ques on 36: Can you write your own custom Marker interface?
Ques on 37: What is Comparable and Comparator?
Ques on 38: How to compare a list of Employees based on name and
age such that if name of the employee is same then sor ng should be
based on age
Ques on 39: Difference between Comparable and Comparator
Ques on 40: Different methods of Object class
Ques on 41: What type of arguments are allowed in System.out.println()
method?
Ques on 42: Explain System.out.println() statement
Ques on 43: Explain Auto-boxing and Un-boxing
Ques on 44: Find the output of below program
Ques on 45: Can you pass primi ve long value in switch statement?
Ques on 46: Explain sta c keyword in Java
Ques on 47: What is an Inner Class in Java, how it can be instan ated
and what are the types of Inner Classes?
Ques on 48: What is Constructor Chaining in java?
Ques on 49: What is init block?
Ques on 50: What is called first, constructor or init block?
Ques on 51: What is Variable shadowing and Variable hiding in Java?
Ques on 52: What is a constant and how we create constants in Java?
Ques on 53: Explain enum
Ques on 54: What is Cloneable?
Ques on 55: What is Shallow Copy and Deep Copy?
Ques on 56: What is Serializa on and De-serializa on?
Ques on 57: What is SerialVersionUID?
Ques on 58: Serializa on scenarios with Inheritance
Ques on 59: Stopping Serializa on and De-serializa on
Ques on 60: What is Externalizable Interface?
Ques on 61: Externalizable with Inheritance
Ques on 62: Difference between Serializable and Externalizable
Ques on 63: How to make a class Immutable?
Ques on 64: Explain Class loaders in Java
Ques on 65: What is Singleton Design Pa ern and how it can be
implemented?
Ques on 66: What are the different ways in which a Singleton Design
pa ern can break and how to prevent that from happening?
Ques on 67: What are the different design pa erns you have used in
your projects?
Ques on 68: Explain Vola le keyword in java
Ques on 69: What is Garbage Collec on in Java, how it works and what
are the different types of Garbage Collectors?
Ques on 70: Explain Generics in Java
Ques on 71: What is Mul -threading?
Ques on 72: How to create a thread in Java?
Ques on 73: Which way of crea ng threads is be er: Thread class or
Runnable interface
Ques on 74: What will happen if I directly call the run() method and
not the start() method to execute a thread
Ques on 75: Once a thread has been started can it be started again
Ques on 76: Why wait, no fy and no fyAll methods are defined in the
Object class instead of Thread class
Ques on 77: Why wait(), no fy(), no fyAll() methods must be called
from synchronized block
Ques on 78: difference between wait() and sleep() method
Ques on 79: join() method
Ques on 80: yield() method
Ques on 81: Tell something about synchronized keyword
Ques on 82: What is sta c synchroniza on?
Ques on 83: What will be output of below program where one
synchronized method is calling another synchronized method?
Ques on 84: Programs related to synchronized and sta c synchronized
methods
Ques on 85: What is Callable Interface?
Ques on 86: How to convert a Runnable to Callable
Ques on 87: Difference between Runnable and Callable
Ques on 88: What is Executor Framework in Java, its different types and
how to create these executors?
Ques on 89: Tell something about awaitTermina on() method in
executor
Ques on 90: Difference between shutdown() and shutdownNow()
methods of executor
Ques on 91: What is Count down latch in Java?
Ques on 92: What is Cyclic Barrier?
Ques on 93: Atomic classes
Ques on 94: What is Collec on Framework?
Ques on 95: What is Collec ons?
Ques on 96: What is ArrayList?
Ques on 97: What is default size of ArrayList?
Ques on 98: Which data structure is used internally in an ArrayList?
Ques on 99: How add() method works internally or How the ArrayList
grows at run me
Ques on 100: How to make an ArrayList as Immutable
Ques on 101: What is LinkedList?
Ques on 102: When to use ArrayList / LinkedList
Ques on 103: What is HashMap?
Ques on 104: Explain the internal working of put() and get() methods of
HashMap class and discuss HashMap collisions
Ques on 105: equals and hashCode method scenarios in HashMap when
the key is a custom class
Ques on 106: How to make a HashMap synchronized?
Ques on 107: What is Concurrent HashMap?
Ques on 108: What is HashSet class and how it works internally?
Ques on 109: Explain Java’s TreeMap
Ques on 110: Explain Java’s TreeSet
Ques on 111: Difference between fail-safe and fail-fast iterators
Ques on 112: Difference between Iterator and ListIterator
Ques on 113: Difference between Iterator.remove and
Collec on.remove()
Ques on 114: What is the difference between a Monolith and Micro-
service architecture?
Ques on 115: What is Dependency Injec on in Spring?
Ques on 116: What are the different types of Dependency Injec on?
Ques on 117: Difference between Constructor and Se er injec on
Ques on 118: What is @Autowired annota on?
Ques on 119: What is the difference between BeanFactory and
Applica onContext?
Ques on 120: Explain the life-cycle of a Spring Bean
Ques on 121: What are the different scopes of a Bean?
Ques on 122: What is the Default scope of a bean?
Ques on 123: What happens when we inject a prototype scope bean in
a singleton scope bean?
Ques on 124: How to inject a prototype scope bean in a singleton scope
bean?
Ques on 125: Explain Spring MVC flow
Ques on 126: What is the difference between <context:annota on-
config> and <context:component-scan>?
Ques on 127: What is the difference between Spring and SpringBoot?
Ques on 128: What is auto-configura on in SpringBoot?
Ques on 129: What are SpringBoot starters?
Ques on 130: What is @SpringBootApplica on Annota on?
Ques on 131: Where does a Spring Boot applica on start from?
Ques on 132: How to remove certain classes from ge ng auto-
configured in SpringBoot?
Ques on 133: How to autowire a class which is in a package other than
SpringBoot applica on class’s package or any of its sub-packages
Ques on 134: What is applica on.proper es file in a SpringBoot
applica on?
Ques on 135: How to configure the port number of a SpringBoot
applica on?
Ques on 136: Which jar builds our springboot applica on automa cally
whenever we change some code just like a node.js applica on?
Ques on 137: What default embedded server is given in spring boot
web starter and how we can change the default embedded server to the
one of our choice
Ques on 138: Where should we put our html and javascript files in a
spring boot applica on?
Ques on 139: What are the different stereotype annota ons?
Ques on 140: Difference between @Component, @Controller, @Service,
@Repository annota ons?
Ques on 141: Difference between @Controller and @RestController
annota on
Ques on 142: What is @RequestMapping and @RequestParam
annota on?
Ques on 143: How to define a Get or Post endpoint?
Ques on 144: Which annota on is used for binding the incoming json
request to the defined pojo class?
Ques on 145: What is @Qualifier annota on?
Ques on 146: What is @Transac onal annota on?
Ques on 147: What is @ControllerAdvice annota on?
Ques on 148: What is @Bean annota on?
Ques on 149: Difference between @Component and @Bean
Ques on 150: How to do profiling in a SpringBoot applica on
Ques on 151: What is RestTemplate?
Ques on 152: What is Spring Data JPA?
Ques on 153: What is the difference between JPARepository,
CRUDRepository, PagingAndSor ngRepository and which one you have
used in your applica ons?
Ques on 154: What is Spring AOP?
Ques on 155: Have you used Spring Security in your applica on
Ques on 156: What do you know about Spring Batch framework?
Ques on 157: Difference between SOAP and REST
Ques on 158: What is Res ul api?
Ques on 159: What is a stateless object?
Ques on 160: What is the difference between Web Server and
Applica on Server?
Ques on 161: What do you know about CommandLineRunner and
Applica onRunner?
Ques on 162: What do you know about Eureka Naming Server?
Ques on 163: What do you know about Zuul?
Ques on 164: What do you know about Zipkin?
Ques on 165: What do you know about Hysterix?
Ques on 166: What is JPA?
Ques on 167: What is Hibernate?
Ques on 168: Difference between JPA and Hibernate
Ques on 169: What is @En ty?
Ques on 170: How to give a name to a table in JPA?
Ques on 171: What is @Id, @GeneratedValue?
Ques on 172: How to use a custom database sequence in Hibernate to
generate primary key values?
Ques on 173: How to give names to the columns of a JPA En ty
Ques on 174: How to define a @OneToMany rela onship between
en es
Ques on 175: Why annota ons should be imported from JPA and not
from Hibernate?
Ques on 176: What is the difference between get() and load() method
of Hibernate Session?
Ques on 177: What is the difference between save(), saveOrUpdate()
and persist() method of Hibernate Session?
Ques on 178: What is Session and SessionFactory in Hibernate?
Ques on 179: What is First Level and Second Level Cache in Hibernate?
Ques on 180: What is session.flush() method in Hibernate?
Ques on 181: How can we see the SQL query that gets generated by
Hibernate?
Ques on 182: What is Hibernate Dialect and why we need to configure
it?
Ques on 183: What do you know about hibernate.hbm2ddl.auto
property in Hibernate?
Ques on 184: What is Maven?
Ques on 185: What is pom.xml?
Ques on 186: What is local repo and central repo?
Ques on 187: Where we define our local repo path?
Ques on 188: Where do we define proxies so that maven can download
jars from the internet in a corporate environment?
Ques on 189: Explain Maven build life-cycle
Ques on 190: What do you know about SQL Joins?
Ques on 191: Difference between TRUNCATE & DELETE statements
Ques on 192: Difference between Func on and Stored Procedure
Ques on 193: What is DDL, DML statements?
Ques on 194: How to find the nth highest salary from Employee table
Ques on 195: Difference between UNION and UNION ALL commands in
SQL
Ques on 196: Difference between Unique Key and Primary Key in SQL
Ques on 197: What is the difference between Primary and Foreign key in
SQL?
Ques on 198: What is the difference between clustered and non-
clustered index?
Ques on 199: What is the difference between WHERE and HAVING
clause in SQL
Ques on 200: How to change the gender column value from Male to
Female and Female to Male using single Update statement
Ques on 201: Find first 3 largest numbers in an array
Ques on 202: Move all nega ve numbers at the beginning of an array
and all posi ve numbers at the end
About the Author
Question 1: What are the 4 pillars of OOPS?
Answer: 4 pillars of OOPS are:

1. Abstrac on
2. Encapsula on
3. Inheritance
4. Polymorphism
Let’s take a look at them:

1. Abstrac on : Abstrac on is a process of hiding the


implementa on details and showing only func onality to the
user.

Real world examples:


- TV remote: To start the TV, you have to press the power
bu on, you don’t have to know about the internal circuit
opera ons like how infrared waves are passing.
- Car gears: We know what happens when we change the
gear. But we don’t know how changing gear works under
the hood, that informa on is irrelevant to us, so it is
abstracted.
In java, Abstrac on can be achieved in two ways:
- Abstract
classes
- Interfaces
2. Encapsula on : Encapsula on is a process of Binding data and
methods within a class . Think of it like showing the essen al
details of a class by using the access control modifiers (public,
private, protected ). So, we can say that Encapsula on leads
to the desired level of Abstrac on.

Example:
Java Bean, where all data members are made private and you
define certain public methods to the outside world to access
them.
3. Inheritance : Using inheritance means defining a parent-child
rela onship between classes, by doing so, you can reuse the
code that is already defined in the parent class. Code
reusability is the biggest advantage of Inheritance.

Java does not allow mul ple inheritance through classes but it
allows it through interfaces.
4. Polymorphism : Poly means many and Morph means forms.
Polymorphism is the process in which an object or func on
takes different forms. There are 2 types of Polymorphism :

- Compile Time Polymorphism (Method


Overloading)
- Run Time Polymorphism (Method Overriding)

In Method overloading, two or more methods in one class have


the same method name but different arguments. It is called as Compile
me polymorphism because it is decided at compile me which
overloaded method will be called.
Overriding means when we have two methods with same name and
same parameters in parent and child class. Through overriding, child
class can provide specific implementa on for the method which is
already defined in the parent class.

Question 2: What is an abstract class?


Answer: A class that is declared using “abstract” keyword is known as
abstract class. It can have abstract methods (methods without body) as
well as concrete methods (methods with body).
Some points to remember:
- An abstract class cannot be instan ated, which means you
are not allowed to create an object of the abstract class.
This also means, an abstract class has no use unless it is
extended by some other class
- If there is any abstract method in a class then that class
must be declared abstract
- The first non-abstract class which is extending from an
abstract class will have to give implementa on of the
abstract methods defined in abstract class
Example:
Output:

Question 3: Does Abstract class have constructor?


Answer: This is a famous interview ques on and the answer is: Yes,
abstract classes have constructor. Either you can provide it or the
default one will be provided by Java. Now, you must be wondering if you
cannot create an object of abstract class then what is the need of a
constructor.
One thing you must know is that the constructors are used when you
are crea ng an object of a class, to ini alize the data members of that
class and your abstract class can have data members.
Now, when your class extends abstract class then the same abstract class
will become super class for your extending class and remember when
you have constructor of your class then first line of your constructor is
always a call to super class constructor and this is the me when your
abstract class constructor will get called.
Example 1:

Output:

Example 2:
Output:
Question 4: What is an Interface?
Answer:

- An interface in Java is a blueprint of a class. It has sta c


constants and abstract methods.
- Interface specify what a class must do but not how to do
- An interface is like defining a contract that is fulfilled by
implemen ng classes
- An interface is used to achieve full abstrac on.
- All methods in an interface are public and abstract by
default and all variables declared in an interface are
constants i.e. public, sta c and final
- A class which implements an interface will have to provide
implementa on of all the methods that are defined in the
interface
- A class can implement more than one interface, this is how
Java allows mul ple inheritance.
- Since Java 8, we can have default and sta c methods in an
interface

Question 5: Difference between abstract class and


interface
Answer: The differences are:
- Abstract class can have both abstract and concrete
methods but interface can only have abstract methods
(Java 8 onwards, it can have default and sta c methods as
well)
- Abstract class methods can have access modifiers other
than public but interface methods are implicitly public and
abstract
- Abstract class can have final, non-final, sta c and non-sta c
variables but interface variables are only sta c and final
- A subclass can extend only one abstract class but it can
implement mul ple interfaces
- An Abstract class can extend one other class and can
implement mul ple interfaces but an interface can only
extend other interfaces
In this ques on, the interviewer may try to confuse you by saying that
from Java 8 onwards, you can have sta c and default methods in an
Interface so now what is the difference between abstract class and
interface and the answer you should tell is – We can s ll extend only
one class but can implement mul ple interfaces.

Question 6: What to choose – interface or abstract class


Answer: Consider these points while choosing between the two:
- When you want to provide default implementa on to some
of the common methods that can be used directly by the
sub-classes then you can use abstract class because it can
have concrete methods also, this is not the case with
Interface because the child classes that are implemen ng
this interface will have to provide implementa on for all
the methods that are declared in the interface
- If your contract keeps on changing then Interface will create
problems because then you will have to provide
implementa on of those new methods in all the
implemen ng classes, whereas with abstract class you can
provide one default implementa on to the new methods
and only change those implemen ng classes that are
actually going to use these new methods
Most of the mes, interfaces are a good choice. It is also one of the best
prac ces, when you code in terms of interfaces.

Question 7: Why Java 8 has introduced default methods?


Answer: To extend the capability of an already exis ng interface, default
methods are introduced in Java 8. Let’s understand this by one example:
Consider there are 100 classes that are implemen ng one
interface. Now you want to define one new method inside your
interface. In this case you will have to change all the implementa on
classes to fulfill the interface contract. So, Java introduced default
methods, here you can provide default implementa on of that new
method inside your interface and as it is not mandatory to provide
implementa on of default methods by the implemen ng classes, all the
100 classes can use the default implementa on or if they want they can
provide their own implementa on by overriding the default method.
Now consider one interes ng scenario: You have two interfaces,
Interface1 and Interface2 both having default method hello() and one
class is implemen ng these 2 interfaces without giving implementa on
to this default method. You see the problem here? Yes, it is the famous
Diamond Problem (Refer to Ques on 9 , if you’re not already familiar
with this problem).

So, to avoid this error, it is mandatory to provide implementa on for


common default methods of interfaces

Output:
Question 8: Why Java 8 has introduced static methods?
Answer: Consider an example where you want to define a u lity class,
what you usually do is you define a class which contains sta c methods
and then you call these methods using class name. Now, Java 8 onwards
you can do the same thing using an Interface by giving only sta c
methods inside your interface. This way of using Interface for defining
u lity classes is be er as it helps in performance also, because using a
class is more expensive opera on than using an interface.

Question 9: Why Java does not allow multiple


inheritance?
Answer: Mul ple inheritance occurs when a class has more than one
parent classes.
Why Java does not allow this : let us consider there are 2 parent classes
having a method named hello() with same signature and one child class
is extending these 2 classes, if you call this hello() method which is same
in both parents, which parent class method will get executed – it results
into an ambiguous situa on, this is also called Diamond Problem .
You will get a compile me error if you try to extend more than one
class.
Question 10: What are the rules for Method Overloading
and Method Overriding?
Answer: Method Overloading Rules: Two methods can be called
overloaded if they follow below rules:
- Both have same method
name
- Both have different
arguments
If both methods follow above two rules, then they may or may not:
- Have different access modifiers
- Have different return types
- Throw different checked or unchecked excep ons
Method Overriding Rules: The overriding method of child class must
follow below rules:
- It must have same method name as that of parent class
method
- It must have same arguments as that of parent class
method
- It must have either the same return type or covariant
return type (child classes are covariant types to their
parents)
- It must not throw broader checked excep ons
- It must not have a more restric ve access modifier (if
parent method is public, then child method cannot be
private/protected)

Question 11: Can we override final methods?


Answer: No, final methods cannot be overridden.

Question 12: Can constructors and private methods be


overridden?
Answer: No

Question 13: What is final keyword and where it can be


used?
Answer: If you use final with a primi ve type variable, then its value
cannot be changed once assigned.
If you use final with a method, then you cannot override it in the
subclass.
If you use final with class, then that class cannot be extended.
If you use final with an object type, then that object cannot be
referenced again.

Question 14: What is exception and exception handling?


Answer: An excep on is an event that disrupts the normal flow of the
program. It is an object which is thrown at run me, so excep on
handling is a mechanism by which normal flow of the program is
maintained.
Program showing the excep on is thrown:

Output:

Program showing that the excep on is handled:

Output:
Question 15: Difference between error and exception
Answer: Error : Errors in a program are irrecoverable, they indicate that
something severe has gone wrong in the applica on and the program
gets terminated in case of error occurrence e.g. running out of memory:
OutOfMemoryError , making too many recursive calls: StackOverflowError
etc.
Excep on : Excep ons on the other hand are something that we can
recover from by handling them properly e.g.: trying to access a
property/method from a null object: NullPointerExcep on , dividing an
integer by zero: Arithme cExcep on etc.

Question 16: What are the different types of exceptions?


Answer: There are 2 types of excep ons:
- Checked Excep ons: All excep ons other than
Run meExcep on and Error are known as Checked
excep on. These excep ons are checked by the compiler at
the compile me itself. E.g. when you are trying to read
from a file, then compiler enforces us to handle the
FileNotFoundExcep on because it is possible that the file
may not be present. Some other checked excep ons are
SQLExcep on , IOExcep on etc.
- Unchecked Excep ons: Run me Excep ons are known as
Unchecked excep ons. Compiler does not force us to
handle these excep ons but as a programmer, it is our
responsibility to handle run me excep ons e.g.
NullPointerExcep on , Arithme cExcep on ,
ArrayIndexOutOfBoundExcep on etc.

Question 17: How exception handling is done in java?


Answer: try-catch block is used for excep on handling. If you think that
certain statements may throw an excep on, surround them with try
block.
A try block is always followed by a catch block or finally or both.
You cannot use the try block alone:
Question 18: Can we write a try block without catch
block?
Answer: Yes, we can write a try block with finally, but we cannot write a
try block alone.

Question 19: How to handle multiple exceptions together?


Answer: You can write mul ple catch blocks one a er another for each
excep on or you can write a single catch block using a pipe symbol (|) to
separate the excep ons.
While wri ng mul ple catch block, you have to follow the below rule:
-
Handle the most specific excep on and then move down to
the most generic ones, means you cannot handle Excep on
(base class of excep on) before FileNotFoundExcep on

Suppose, your method is throwing more than one excep on and you
want to perform some specific ac on based on the excep on thrown,
you should use mul ple catch blocks in this case.
Example using mul ple catch blocks:
When using pipe (|) symbol:

Question 20: When finally block will not get executed


Answer:

- when System.exit() is called


- when JVM crashes
Question 21: Difference between throw and throws
keyword. And discuss Exception Propagation
Answer:

- throw is a keyword which is used to explicitly throw an


excep on in the program, inside a func on or inside a block
of code, whereas throws is a keyword which is used with
the method signature to declare an excep on which might
get thrown by the method while execu ng the code
- throw keyword is followed by an instance of an Excep on
class whereas throws is followed by Excep on class names
- You can throw one excep on at a me but you can declare
mul ple excep ons using throws keyword
- Using throw keyword, only unchecked excep ons are
propagated, whereas using throws keyword both checked
and unchecked excep ons can be propagated.
Excep on propaga on :
An excep on is first thrown from the top of the stack and if it is not
caught, it drops down the call stack to the previous method, If not
caught there, the excep on again drops down to the previous method,
and so on un l they are caught or un l they reach the very bo om of
the call stack. This is called excep on propaga on.

- When method m1( ) calls method m2() which calls method


m3(), a stack is formed which gets unfold from the top, so if
method m3() throws an excep on and it is not handled
there, it will drop down the call stack to method m2(), if it
is not handled there, it will drop down the call stack to
method m1(), this happens un l we reach the bo om of
the stack or un l the excep on is caught. This is called
Excep on Propaga on in java.

Example: unchecked excep on is thrown and it can be seen from the call
stack that it is propagated
Output:

Example: Here the unchecked excep on is handled

Output:
Example: checked excep ons are not propagated down the call chain
by default ,

You have to use throws keyword if you want to propagate the checked
excep on, like

No ce in the above example that the checked excep on is propagated


and now it is the responsibility of the caller method to either handle the
excep on or throw it further. Below example is showing that the
checked excep on is handled ,

Output:

throws can be used with unchecked excep ons also, though it is of no


use because unchecked excep ons are by default propagated. See this in
the below program:
Output:

Unchecked excep ons are by default propagated:

Output:

Question 22: Exception handling w.r.t. method overriding


Answer:

- If the parent class method does not declare an excep on


then child class overridden method cannot declare checked
excep ons but it can declare unchecked excep ons
- If the parent class method declares an excep on, then child
class overridden method
- can declare no excep on
- can declare same excep on
- can declare a narrower excep on (more broader
excep on declara on than parent one is not allowed)
Example when parent class method does not declare an excep on and
child class declares checked excep on :

Example when parent class method does not declare an excep on and
child class declares unchecked excep on :
Output:

Example when Child class overridden method throws a broader


excep on than the parent one :

Example when child class overridden method throws same excep on as


the parent one :
Output:

Example when child class overridden method declares a narrower


excep on than the parent one :
Output:

Example when parent class method declares an excep on and child


class overridden method does not declare any excep on :
Output:

Question 23: Programs related to Exception handling and


return keyword
Answer: These ques ons are asked a lot of mes to the new
programmers.

One thing you should remember is, if you write anything a er return
statement / throw excep on statement, then that will give Compile me
error as ‘Unreachable code’.

Program 1:

Program 2 : finally block is always executed


Output:

Program 3 : program execu on returns from the finally block


Program 4 :
Output:

Program 5 :
Output:

Question 24: How to make your own custom exception


class?
Answer: In java, you can create your own custom excep on class which
are basically derived classes from the Excep on class.
For Example:
Output:

Question 25: How to make custom checked / unchecked


exception?
Answer: If you want to make a custom unchecked excep on class then
extend Run meExcep on and for crea ng a custom checked excep on
class, extend Excep on class.

Question 26: What happens when you throw an exception


from finally block?
Answer: When excep on is thrown from finally block, then it takes
precedence over the excep ons that are thrown from try/catch block

Question 27: What will be Output of below program


related to try-catch-finally?
With everything you have read so far, you should be able to answer this
easily. I will leave this one for you.

Question 28: Explain try-with-resources


Answer: try-with-resources concept was introduced in Java 7. It allows us
to declare resources which will be used inside the try block and it
assures us that the resources will be closed a er execu on of this block.
A resource is an object that must be closed a er finishing the program.
The resources declared must implement AutoCloseable interface.
Syntax:

Question 29: Why String is Immutable?


Answer: String is immutable for below reasons:

1. String Pool : String Pool is possible only because String is


Immutable in Java. String pool is a special storage area in
Java heap. If the string is already present in the pool, then
instead of crea ng a new object, old object’s reference is
returned. This way different String variables can refer to the
same reference in the pool, thus saving a lot of heap space
also. If String is not immutable then changing the string with
one reference will lead to the wrong values to other string
variables having the same reference.
2. Security : String parameters are used in network connec ons,
database URL’s, username and passwords etc. Because String
is immutable, these values can’t be changed. Otherwise any
hacker could change the referenced values which will cause
severe security issues in the applica on.
3. Mul -threading : Since String is immutable, it is safe for
mul threading. A single String instance can be shared across
different threads. This avoids the use of synchroniza on for
thread safety. Strings are implicitly thread-safe.
4. Caching : The hashcode of string is frequently used in Java.
Since string is immutable, the hashcode will remain the same,
so it can be cached without worrying about the changes. This
makes it a great candidate for using it as a Key in Map.
5. Class Loaders : Strings are used in Java ClassLoaders and since
String is made immutable, it provides security that correct
class is being loaded.

Question 30: What does the equals() method of String


class do?
Answer: As we know, Object class is the parent of all classes, and Object
class has a equals() method that compares the reference of two objects,
but String class has overridden this method, and String class’s equals()
method compares the contents of two strings.
Program:

Output:

Question 31: Explain StringBuffer and StringBuilder


Answer: Both StringBuffer and StringBuilder classes are used for String
manipula on. These are mutable objects. But StringBuffer provides
thread-safety as all its methods are synchronized, this makes
performance of StringBuffer slower as compared to StringBuilder.
StringBuffer class is present from Java 1.0, but due to its slower
performance, StringBuilder class was introduced in Java 1.5
If you are in a single-threaded environment or don’t care about thread
safety, you should use StringBuilder. Otherwise, use StringBuffer for
thread-safe opera ons.

Question 32: Explain the output of below program related


to equals() method of StringBuilder

Output:

Answer: This is another very famous interview ques on. If you were
expec ng ‘Equal’ as output, then you were wrong. The output is not
‘Equal’ because StringBuffer and StringBuilder does not override equals
and hashcode methods. In the above program, Object’s class equals()
method is ge ng used and as it compares the reference of two objects,
the output of above program is ‘Not Equal’.
Since hashcode is used in data structures that use hashing algorithm to
store the objects. Examples are HashMap, HashSet, HashTable,
ConcurrentHashMap etc. and all these data structures requires their
keys not to be changed so that stored values can be found by using
hashcode method but StringBuffer/StringBuilder are mutable objects.
This makes them a very poor choice for this role.
You must have heard about the equals and hashcode contract in Java,
which states that if two objects are equals according to equals() method
then their hashcode must be same, vice-versa is not true. Now, had the
equals method for StringBuilder/StringBuffer be overridden, their
corresponding hashcode method would also need to be overridden to
follow that rule. But as explained earlier, these classes don’t need to
have their own hashcode implementa on and hence same is with their
equals method.

Question 33: When to use


String/StringBuffer/StringBuilder
Answer: You should use String class if you require immutability, use
StringBuffer if you require mutability + Thread safety and use
StringBuilder if you require mutability and no thread safety.

Question 34: Explain equals and hashcode contract


Answer: The equals and hashcode contract says:
-
If two objects are equals according to equals() method, then
their hashcode must be same but reverse is not true i.e. if two
objects have same hashcode then they may/may not be
equals.

Question 35: What is Marker Interface?


Answer: Marker interface is an interface which is empty. Some of the
Marker interfaces are Cloneable, Serializable, Remote etc. If you have
read that Marker interfaces indicate something to the compiler or JVM,
then you have read it wrong, it has nothing to do with JVM.
Consider the example of Cloneable:
It is said that you cannot call clone() method on a class object unless the
class implements Cloneable interface. Well this statement is true,
because when you call clone() method then the first statement in clone()
is, obj instanceOf Cloneable .
The class object on which clone() method is ge ng called is
checked, whether the class implements Cloneable interface or not, by
using instanceOf operator. If class does not implement Cloneable
interface then CloneNotSupportedExcep on is thrown.
Same is true with writeObject(Object) method of ObjectOutputStream
class. Here, obj instanceOf Serializable is used to check whether the class
implements Serializable interface or not. If class does not implement
Serializable interface then NotSerializableExcep on is thrown.

Question 36: Can you write your own custom Marker


interface?
Answer: Yes. As you already know that Marker interfaces have got
nothing to do with indica ng some signal to JVM or compiler, instead it
is just a mere check of using instanceOf operator to know whether the
class implements Marker interface or not.
In your method, you can put a statement like: object instanceOf
MyMarkerInterface.
You can use Marker interface for classifica on of your code.

Question 37: What is Comparable and Comparator?


Answer: Both Comparable and Comparator interfaces are used to sort
the collec on of objects. These interfaces should be implemented by
your custom classes, if you want to use Arrays/Collec ons class sor ng
methods.
Comparable interface has compareTo(Obj) method, you can override this
method in your class, and you can write your own logic to sort the
collec on.
General rule to sort a collec on of objects is:
If ‘this’ object is less than passed object, return nega ve integer.
If ‘this’ object is greater than passed object, return posi ve integer.
If ‘this’ object is equal to the passed object, return zero.
Comparable Example :
Employee.java
ComparableDemo.java :

Output:

Here, we have sorted the Employee list based on ‘id’ a ribute.


Now, if we want to sort the employee list based on any other a ribute,
say name, we will have to change our compareTo() method
implementa on for this. So, Comparable allows only single sor ng
mechanism.
But Comparator allows sor ng based on mul ple parameters. We can
define another class which will implement Comparator interface and
then we can override it’s compare(Obj, Obj) method.
Suppose we want to sort the Employee list based on name and salary.
NameComparator.java :
String class already implements Comparable interface and provides a
lexicographic implementa on for compareTo() method which compares
2 strings based on contents of characters or you can say in lexical order.
Here, Java will determine whether passed String object is less than,
equal to or greater than the current object.
ComparatorDemo.java :

Output:

The output list is sorted based on employee’s names.


SalaryComparator.java :

ComparatorDemo.java :

Output:

Question 38: How to compare a list of Employees based on


name and age such that if name of the employee is same
then sorting should be based on age
Answer: When comparing by name, if both names are same, then
comparison will give 0. If the compare result is 0, we will compare based
on age.
NameAgeComparator.java :

ComparatorDemo.java :

Output:
Question 39: Difference between Comparable and
Comparator
Answer:

- Comparable interface can be used to provide single way of


sor ng whereas Comparator interface is used to provide
mul ple ways of sor ng
- Comparable interface is present in ‘java.lang’ package
whereas Comparator interface is present in ‘java.u l’
package
- For using Comparable, the class needs to implement
Comparable interface whereas for using Comparator, there
is no need to make changes in the class
- Comparable provides compareTo() method to sort
elements, whereas Comparator provides compare() method
to sort elements
- We can sort the list elements of Comparable type by using
Collec ons.sort(listObj) method, whereas to sort the list
elements of Comparator type, we have to provide a
Comparator object like, Collec ons.sort(listObj,
Comparator)
At mes, when you are using any third-party classes or the classes where
you are not the author of the class, then in that case Comparator is the
only choice to sort those objects

Question 40: Different methods of Object class


Answer: Object class sits at the top of class hierarchy tree. Every class is
a child of Object class. Below methods are present inside Object class:
//Creates and returns a copy of this object
protected native Object clone() throws
CloneNotSupportedException
//Indicates whether some other object is "equal to"
this one
public boolean equals(Object obj )
//Returns a hash code value for the object
public native int hashCode()
//Returns the runtime class of an Object
public final native Class<?> getClass()
//Returns a string representation of the object
public String toString()
//Called by the garbage collector on an object when
garbage collection
//determines that there are no more references to the
object
protected void finalize() throws Throwable
//Wakes up a single thread that is waiting on this
object's monitor
public final native void notify()
//Wakes up all threads that are waiting on this
object's monitor
public final native void notifyAll()
public final native void wait( long timeout ) throws
InterruptedException
public final void wait( long timeout , int nanos )
throws InterruptedException
public final void wait() throws InterruptedException
Question 41: What type of arguments are allowed in
System.out.println() method?
Answer: println() method of PrintStream class is overloaded, and it
accepts below arguments :

Question 42: Explain System.out.println() statement


Answer:
- System is a class in java.lang package
- out is a sta c member of System class and is an instance of
java.io.PrintStream
- println() is a method of PrintStream class

Question 43: Explain Auto-boxing and Un-boxing


Answer: In Java 1.5, the concepts of Auto-boxing and Un-boxing were
introduced to automa cally convert primi ve type to object and vice-
versa.
When Java automa cally converts a primi ve type, like int into its
corresponding wrapper class object i.e. Integer, then this is called Auto-
boxing. While the opposite of this is called Un-boxing, where an Integer
object is converted into primi ve type int.
Example:

Output:

Question 44: Find the output of below program


Answer: Here, the compiler is confused as to which method to be called,
so it throws Compile Time error.

Question 45: Can you pass primitive long value in switch


statement?
Answer: No, switch works only with 4 primi ves and their wrappers, as
well as with the enum type and String class:

- byte and Byte


- short and Short
- char and Character
- int and Integer
- enum
- String
Question 46: Explain static keyword in Java
Answer: In Java, a sta c member is a member of a class that isn’t
associated with an instance of a class. Instead, the member belongs to
the class itself.
In Java, Sta c is applicable for the following:

- Variable
- Method
- Block
- Nested
class
Sta c Variable : if any variable is declared as sta c, then it is known as
‘sta c variable’. Only single copy of the variable gets created and all
instances of the class share same sta c variable. The sta c variable gets
memory only once in the class area at the me of class loading.
When to use sta c variable : sta c variables should be used to declare
common property of all objects as only single copy is created and shared
among all class objects, for example, the company name of employees
etc.
Sta c Method : When a method is declared with sta c keyword then it is
known as sta c method. These methods belong to the class rather than
the object of the class. As a result, a sta c method can be directly
accessed using class name without the need of crea ng an object.
One of the basic rules of working with sta c methods is that you can’t
access a non-sta c method or field from a sta c method because the
sta c method doesn’t have an instance of the class to use to reference
instance methods or fields. Another restric on is, ‘this’ and ‘super’
cannot be used in sta c context.
For example: main() method is sta c, Java Run me uses this method to
start an applica on without crea ng an object.

Sta c Block : Sta c block gets executed exactly once when the class is
first loaded, use sta c block to ini alize the sta c variables.

Sta c nested classes :


Sta c nested classes are a type of inner class in java where the inner
class is sta c. Sta c nested classes can access only the sta c members of
the outer class. The advantage of using sta c nested classes is that it
makes the code more readable and maintainable.
In the case of normal inner class, you cannot create inner class object
without first crea ng the outer class object, but in the case of sta c
inner class, there can be a sta c inner class object without the outer
class object.
How to create object of sta c inner class:
OuterClass.Sta cNestedClass nestedClassObject = new
OuterClass.Sta cNestedClass();

Compile Time Error comes when we try to access non-sta c member


inside sta c nested class:
Using inner class object:

Output:
If you have sta c members in your Sta c Inner class then there is no
need to create the inner class object:

Output:

Question 47: What is an Inner Class in Java, how it can be


instantiated and what are the types of Inner Classes?
Answer: In Java, when you define one non-sta c class within another
class, it is called Inner Class/Nested Class. Inner class enables you to
logically group classes that are only used in one place, thus, this
increases the use of encapsula on and creates more readable and
maintainable code.
Java inner class is associated with the object of the class and they can
access all the variables and methods of the outer class. Since inner
classes are associated with the instance, we can’t have any sta c
variables in them.
The object of java inner class is part of the outer class object and to
create an instance of the inner class, we first need to create an instance
of outer class.
Java Inner classes can be instan ated like below:
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Example:

Output:
Compile me error when sta c variable and sta c method is present in
Inner class:

There are 2 special types of Inner Classes:


- local inner class
- anonymous inner class
local inner class : Local Inner Classes are the inner classes that are
defined inside a block. Generally, this block is a method body.
Some mes this block can be a for loop , or an if clause. Local Inner
classes are not a member of any enclosing classes. They belong to the
block in which they are defined in, due to which local inner classes
cannot have any access modifiers associated with them. However, they
can be marked as final or abstract. These classes have access to the
fields of the class enclosing it. Local inner class must be instan ated in
the block they are defined in.
Points to remember:

- local inner class cannot be instan ated from outside of the


block where they are defined
- local inner class has access to the members of the enclosing
class
- ll Java 1.7, local inner class can access only final local
variable of the enclosing block where they are defined. But
from Java 1.8 onwards, it is possible to access the non-final
local variable of the enclosing block
- the scope of local inner class is restricted to the block
where they are defined
- A local inner class can extend an abstract class or can
implement an interface
Example:
Output:

Remember, you can only access the block level variables, and cannot
change them. You will get compile me error if you try to change them:
A variable whose value is not changed once ini alized is called as
effec vely final variable .

Anonymous inner class :


An inner class that does not have any name is called Anonymous Inner
class. You should use them when you want to use local class only once.
It does not have a constructor since there is no class name and it cannot
be declared as sta c.
Generally, they are used when you need to override the method of a
class or an interface.
When to use :
Example 1 : Let’s understand this by an example, Suppose you are want
to return a list of employee class objects and they should be sorted
based on employee name, now for this you can write a comparator in a
separate class and pass its object inside the Collec ons.sort(list,
comparatorObject)
Instead, you can use the anonymous inner class and you don’t have to
create a new class just for wri ng a comparator that you are not going
to use later on.

Program:
Employee.java :
AnonymousInnerDemo.java :

Output:

Example 2 : Using anonymous inner class, you can implement a


Runnable also
Output:

Example 3 : You can use anonymous inner class in situa ons where you
want to override the parent class method without crea ng a separate
child class:
Output:

Some points to remember:

- anonymous class does not have a constructor as it does not


have any class name
- in anonymous class, we cannot have sta c members except
sta c constants
- an anonymous class has access to the members of its
enclosing class
- an anonymous class cannot access local variables in its
enclosing scope(block) that are not final or effec vely final

Anonymous Inner Class Example :


Output:
Compile me error in case of using sta c variable which is not final :

Question 48: What is Constructor Chaining in java?


Answer: when one constructor calls another constructor, it is known as
constructor chaining. This can be done in two ways:
- this() : it is used to call the same class constructor
- super() : it is used to call the parent class
constructor
this() Example:
Output:
super() example:
Output:

Some points to remember:


- this() can call same class constructor only
- super() can call immediate super class constructor only
- this() and super() call must be the first statement, because
of this reason both cannot be used at the same me
- you must use super() to call the parent class constructor
but if you do not provide a super() call, JVM will put it
automa cally

Question 49: What is init block?


Answer: init block is called instance ini alizer block
-
init block is used to ini alize the instance data members
-
init block runs each me when object of the class is created
-
init block runs in the order they appear in the program
-
compiler replaces the init block in each constructor a er
the super() statement
- init block is different from sta c block which runs at the
me of class loading
Example 1:
Output:

Example 2:
Output:

Example 3:
Output:
Order of execu on :
- sta c blocks of super classes
- sta c blocks of the class
- init blocks of super classes
- constructors of super classes
- init blocks of the class
- constructors of the class
1. The code in sta c ini aliza on block will be executed
at class load me (and yes, that means only once per
class load), before any instances of the class are
constructed and before any sta c methods are called.
2. The instance ini aliza on block is actually copied by
the Java compiler into every constructor the class has.
So, the code in instance ini aliza on block is
executed exactly before the code in constructor.

Question 50: What is called first, constructor or init block?


Answer: Constructor is invoked first. Compiler copies all the code of
instance ini alizer block into the constructor a er first statement
super().

Question 51: What is Variable shadowing and Variable


hiding in Java?
Answer: Variable shadowing : When a local variable inside a method has
the same name as one of the instance variables, the local variable
shadows the instance variable inside the method block.
Example 1:
Output:

If you want to access instance variables then you can do so using ‘this’
keyword like below:
Example 2:
Output:

Variable Hiding : When the child and parent classes both have a variable
with the same name, the child class variable hides the parent class
variable.
Example 1:
Output:

If you want to access parent’s class variable then you can do this using
super keyword:
Example 2:
Output:

Variable hiding is not same as Method Overriding :


While variable hiding looks like overriding a variable (similar to method
overriding), it is not. Overriding is applicable only to methods while
hiding is applicable to variables.
In the case of method overriding, overridden methods completely
replace the inherited methods, so when we try to access the method
from a parent's reference by holding a child's object, the method from
the child class gets called.
But in variable hiding, the child class hides the inherited variables
instead of replacing them, so when we try to access the variable from
the parent's reference by holding the child's object, it will be accessed
from the parent class.
When an instance variable in a subclass has the same name as an
instance variable in a super class, then the instance variable is chosen
from the reference type.
Output:

Question 52: What is a constant and how we create


constants in Java?
Answer: A constant is a variable whose value cannot change once it has
been assigned. To make any variable a constant, we can use ‘sta c’ and
‘final’ modifier like below:
public sta c final TYPE NAME_OF_CONSTANT_VARIABLE = VALUE;
We can also use “enum” to define constants.

Question 53: Explain enum


Answer: enum in java is a data type which contains a fixed set of
constants. In enum, we can also add variables, methods and
constructors. Some common examples of enums are: days of week,
colors, excel report columns etc.
Some points to remember:
- enum constants are sta c and final implicitly
- enum improves type safety
- enum can be declared inside or outside of a class
- enum can have fields, constructors (private) and methods
- enum cannot extend any class because it already extends
Enum class implicitly but it can implement many interfaces
- We can use enum in switch statement
- We can have main() method inside an enum
- enum has values(), ordinal() and valueOf() methods.
values() return an array containing all values present inside
enum, ordinal() method returns the index of given enum
value and valueOf() method returns the value of given
constant enum
- enum can be traversed
- enum can have abstract methods
- enum cannot be instan ated because it contains private
constructor only
- The constructor is executed for each enum constant at the
me of enum class loading
- While defining enum, constants should be declared first,
prior to any fields or methods, or else compile me error
will come
Example 1:
main() method in enum, itera ng over enum:
Output:

Example 2:
Output:

Question 54: What is Cloneable?


Answer: Cloneable is an interface in Java which needs to be
implemented by a class to allow its objects to be cloned.
A class implements the Cloneable interface to indicate to the
Object.clone() method that it is legal for that method to make a field-for-
field copy of instances of that class.
If you try to Clone an object which doesn’t implement the Cloneable
interface, it will throw CloneNotSupportedExcep on.
Example without implemen ng Cloneable interface:
Program 1:

Output:

Here, we have created Employee object e1 with new keyword but then
we created another object Employee e2 which has the same reference as
of e1. So, any change in e2 object will reflect in e1 object and vice-versa.
Now, let’s implement Cloneable interface in our Employee class and
invoke the clone() method on e1 object to make its clone:
Program 2:

Output:

In the above example, we have only primi ve types in our Employee


class, what if we have an object type i.e. another class object reference,
see the example below:
Program 3:

Can you guess the output of the last 2 sysout? Here is the output:
If you are surprised with the above output, then let me make it clear by
saying that, by default Object’s clone() method provide Shallow copy.
This brings us to the next interview ques on: What is shallow copy and
deep copy
Question 55: What is Shallow Copy and Deep Copy?
Answer: Shallow Copy : When we use the default implementa on of
clone() method, a shallow copy of object is returned, meaning if the
object that we are trying to clone contains both primi ve variables and
non-primi ve or reference type variable, then only the object’s
reference is copied not the en re object itself.
Consider this with the example:
Employee object is having Company object as a reference, now when we
perform cloning on Employee object, then for primi ve type variables,
cloning will be done i.e. new instances will be created and copied to the
cloned object but for non-primi ve i.e. Company object, only the
object’s reference will be copied to the cloned object. It simply means
Company object will be same in both original and cloned object,
changing the value in one will change the value in other and vice-versa.
Now, if you want to clone the Company object also, so that your original
and cloned Employee object will be independent of each other, then you
have to perform Deep Copy.

Deep Copy : in Deep copy, the non-primi ve types are also cloned to
make the original and cloned object fully independent of each other.
Program 1:
Output:
In above example, we have overridden the clone method in our
employee class and we called the clone method on mutable company
object.
We can also use Copy constructor to perform deep copy:
Program 2:
Output:

There are 2 other methods by which you can perform deep copy:
- By using Serializa on, where you serialize the original
object and returns the deserialized object as a clone
- By using external library of Apache Commons Lang. Apache
Common Lang comes with Serializa onU ls.clone() method
for performing deep copy on an object. It expects all classes
in the hierarchy to implement Serializable interfaces else
SerializableExcep on is thrown by the system

Question 56: What is Serialization and De-serialization?


Answer: Serializa on is a mechanism to convert the state of an object
into a byte stream while De-serializa on is the reverse process where
the byte stream is used to recreate the actual object in memory. The
byte stream created is pla orm independent that means objects
serialized on one pla orm can be deserialized on another pla orm.
To make a Java Object serializable, the class must implement Serializable
interface. Serializable is a Marker interface. ObjectOutputStream and
ObjectInputStream classes are used for Serializa on and Deserializa on
in java.
We will serialize the below Employee class:

Serializa onDemo.java:
Output:

Here, while de-serializing the employee object, salary is 0, that is


because we have made salary variable to be ‘transient’. ‘sta c’ and
‘transient’ variables do not take part in Serializa on process. During de-
serializa on, transient variables will be ini alized with their default
values i.e. if objects, it will be null and if “int”, it will be 0 and sta c
variables will be having the current value.

And if you look at the file present in C:/temp/bytestream.txt, you can


see how the object is serialized into this file,
Question 57: What is SerialVersionUID?
Answer: SerialVersionUID : The serializa on process at run me
associates an id with each Serializable class which is known as
SerialVersionUID. It is used to verify the sender and receiver of the
serialized object. The sender and receiver must have the same
SerialVersionUID, otherwise, InvalidClassExcep on will be thrown when
you deserialize the object. A Serializable class can declare its own UID
explicitly by declaring a field. It must be sta c, final and of type long.
Remember, there is an excep on for SerialVersionUID that although it is
sta c, it gets serialized too, so that at the object deserializa on the
sender and receiver can be verified.
If a serializable class doesn’t explicitly declare a serialVersionUID, then
the serializa on run me will calculate a default one for that class based
on various aspects of class. This default serialVersionUID gets changed,
when you add a new field or remove the transient keyword from a
variable or convert the sta c variable to non-sta c variable. And if you
are modifying the class structure a er Serializa on has been done, you
will not be able to deserialize the object, see the example below:
Let’s change the Employee class in our previous example and remove
the transient keyword from salary variable:
Program 1:
We have already serialized the Employee object in our previous
example, now let’s try to de-serialize it back:

Output:
java.io.InvalidClassException : com.serialization.demo.Employee; local class
incompatible: stream classdesc serialVersionUID = -3697389390179909057,
local class serialVersionUID = -3759917827722067163
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.serialization.demo.DeserializationTest.main(
DeserializationTest.java:13 )

Hence, it is strongly recommended that all serializable classes explicitly


declare serialVersionUID value, in case you are using an IDE and not
giving any serialVersionUID, compiler will give you a warning like below:

Example with SerialVersionUID:


Program 2:

Serializa onDemo.java:
Output:

Now, let’s add one more field ‘company’ and remove the transient
keyword from our Employee class. Here, as we are changing the class
structure, let’s see if we get the error of InvalidClassExcep on again:
Program 3:
Deserializa onTest.java:

Output:

Some Points to remember:


- If a parent class has implemented Serializable interface
then child class doesn’t need to implement it but the
reverse is not true
- Sta c data members and transient data members are not
saved via Serializa on process (serialVersionUID is an
excep on). So, if you don’t want to save value of a non-
sta c data member then make it transient
- Constructor of serialized class is never called when the
serialized object is deserialized (in case of inheritance, no-
arg constructor of parent gets called during de-serializa on)

Question 58: Serialization scenarios with Inheritance


Case 1: If super class is Serializable then by default, its sub-classes are
also Serializable
Output:
Case 2: When super class does not implement the Serializable Interface,
then also we can serialize the subclass provided that it implements
Serializable interface.
In this case, when we de-serialize the subclass object, then no-arg
constructor of its parent class gets called. So, the serializable sub-class
must have access to the default no-arg constructor of its parent class
(general rule is that the Serializable sub-class must have access to the
no-arg constructor of first non-Serializable super class).

(Note: serializeObject() and deserializeObject() remains same as the Case


1 program)
Output:

When no-arg constructor is present in Super class:


(Note: serializeObject() and deserializeObject() remains same as the Case
1 program)
Output:

Question 59: Stopping Serialization and De-serialization


Suppose, parent class implements Serializable interface but we don’t
need the child class to be serialized
Here, we can implement writeObject() and readObject() methods in sub-
class and throw NotSerializableExcep on from these methods :
(Note: serializeObject() and deserializeObject() remains same as the
Ques on 58 - Case 1 program)
Output:
Serializa on and De-serializa on process can be customized also by
providing writeObject() and readObject() methods in the class that we
want to serialize.

Declaring both methods as private is necessary (public methods will not


work), so other than the JVM nothing else can see them. This also
proves that neither method is not inherited nor overridden or
overloaded. The JVM automa cally checks these methods and calls them
during the serializa on-deserializa on process. The JVM can call these
private methods, but other objects cannot. Thus, the integrity of the
class is maintained and the serializa on protocol can con nue to work
as normal.
For example, you can have encryp on and decryp on logic in these
methods.

Question 60: What is Externalizable Interface?


Answer: The default serializa on process is very slow as it is fully
recursive, so whenever we try to serialize one object, the serializa on
process tries to serialize all the fields of our class (except sta c and
transient variables). So, if we have a class with lots of variables present
and we do not want to serialize all of them, we have to make all of
those variables as transient, all these fields will always be assigned with
default values. This makes the en re process very slow.
Externalizable interface is used when we want to implement custom
logic to serialize/deserialize an object. Externalizable interface extends
the Serializable interface, and it has two methods, writeExternal() and
readExternal() which are used for serializa on and de-serializa on. This
way, we can change the JVM’s default serializa on behavior because
while using Externalizable, we decide what to store in stream.
Program 1:
Employee.java:
TestExternalizable.java:

Output:
Now, one thing to remember here is that the public no-arg constructor
gets called before readExternal() method, so we have to provide this no-
arg constructor or else we will get an excep on during run- me.
Comment the public no-arg constructor from Employee.java:
Program 2:

Now, run TestExternalizable.java, you will get below output:

So, if interviewer asks a ques on that you have a class which has 1000
variables and you want to serialize only 10 specific variables, your
answer should be using an Externalizable interface.
Some points to remember:
- Using Externalizable interface, we can implement custom
logic for serializa on and deserializa on of object
- When using Externalizable, we have to explicitly men on
what fields or variables we want to serialize
- When using Externalizable, a public no-arg constructor is
required
- Using Externalizable, we can also serialize transient and
sta c variables
- readExternal() method must read the values in the same
sequence and with the same types as were wri en by
writeExternal() method

Question 61: Externalizable with Inheritance


If a class is implemen ng Externalizable and also has a parent class, how
we can save the data of parent class and how we can recover it back. In
case of Externalizable, we have to implement the readExternal() and
writeExternal() methods in each and every class we want to serialize or
deserialize.
Case 1 : When both parent and child classes are implemen ng
Externalizable interfaces:
Department.java:
Student.java:
Here, in the overridden writeExternal() and readExternal() methods in
child class Student, we are also calling super.writeExternal() and
super.readExternal() methods to serialize and de-serialize fields of
parent class Department.

ExternalizableDemo.java:
Output:

The capacity and age variable values are 0 because we did not serialize
these two variables.

Case 2 : When only child class is implemen ng the Externalizable


interface
Department.java:

Student.java:
Here, we are using ge ers and se ers of parent class, Department, to
serialize and de-serialize its fields.

ExternalizableDemo.java:
Output:

Question 62: Difference between Serializable and


Externalizable
Answer:
- Serializable is a marker interface which means it does not
contain any method whereas Externalizable is a child
interface of Serializable and it contains two methods
writeExternal() and readExternal()
- When using Serializable, JVM takes full responsibility for
serializing the class object but in case of Externalizable, the
programmer has full control over serializa on logic
- Serializable interface is a be er fit when we want to
serialize the en re object whereas Externalizable is be er
suited for custom serializa on
- Default serializa on is easy to implement but it comes with
some issues and performance cost whereas in case of
Externalizable, the programmer has to provide the
complete serializa on logic which is a li le hard but results
in be er performance
- Default serializa on does not call any constructor whereas
a public no-arg constructor is needed when using
Externalizable interface
- When a class implements Serializable interface, it gets ed
with default serializa on which can easily break if structure
of the class changes like adding/removing any variable
whereas using Externalizable, you can create your own
binary format for your object

Question 63: How to make a class Immutable?


Answer: As we know, String is an Immutable class in Java, i.e. once
ini alized its value never change. We can also make our own custom
Immutable class, where the class object’s state will not change once it is
ini alized.
Benefits of Immutable class:
- Thread-safe: With immutable classes, we don’t have to
worry about the thread-safety in case of mul -threaded
environment as these classes are inherently thread-safe
- Cacheable: An immutable class is good for Caching because
while we don’t have to worry about the value changes
How to create an Immutable class in java:
- Declare the class as final so that it cannot be extended
- Make all fields as private so that direct access to them is
not allowed
- Make all fields as final so that its value can be assigned only
once
- Don’t provide ‘se er’ methods for variables
- When the class contains a mutable object reference,

1. While ini alizing the field in constructor, perform a


deep copy
2. While returning the object from its ge er method,
make sure to return a copy rather than the actual
object reference
Example:
We will make Employee class as immutable, but Employee class contains
a reference of Address class
Address.java:
Employee.java:

TestImmutable.java:
Here, a er crea ng Employee object, the first change is done in local
address object and then we used the employee’s ge er method to
access the address object and tried to change the value in it.

Output:

As, you can see that the value remained the same.
If we don’t follow the rule about mutable object reference present in
the class, let’s see what will happen in that case.
Let’s change the Employee class constructor and ge er method:
Employee.java:
Now, if we run our TestImmutable.java class, below is the output:

Why we perform deep copy in constructor:

- When you assign the actual address object in the


constructor, then remember it is storing the reference of
address object, so if you change the value in this address
object, it will reflect in the employee object
Why we don’t return original reference from the ge er:
-
When you return the original address object from the ge er
method then you can use the returned object reference to
change the values in employee object

Question 64: Explain Class loaders in Java


Answer: ClassLoader is a java class which is used to load .class files in
memory. When we compile a java class, JVM creates a bytecode which is
pla orm independent. The bytecode is present in .class file. When we
try to use a class, then classloader loads it into memory.
There are 3 types of built-in class loaders in java:
1. Bootstrap class loader : it loads JDK class files from
jre/lib/rt.jar and other core classes. It is the parent of all class
loaders, it is also called Primordial classloader.
2. Extensions class loader : it loads classes from JDK extensions
directory, it delegates class loading request to its parent,
Bootstrap and if the loading of class is unsuccessful, it loads
classes from jre/lib/ext directory or any other directory
pointed by java.ext.dirs system property.
3. System class loader : It loads applica on specific classes from
the CLASSPATH. We can set classpath while invoking the
program using -cp or classpath command line op ons. It is a
child of Extension ClassLoader.

Java class loader is based on three principles:

1. Delega on principle: It forwards the request for class loading


to its parent class loader. It only loads the class if the parent
does not find or load the class.
2. Visibility principle: According to Visibility principle, the child
ClassLoader can see all the classes loaded by parent
ClassLoader. But the parent class loader cannot see classes
loaded by the child class loader.
3. Uniqueness principle: According to this principle, a class
loaded by Parent should not be loaded by Child ClassLoader
again. It is achieved by delega on principle.

Suppose, you have created a class Employee.java and compiled this class
and Emloyee.class file is created. Now, you want to use this class, the
first request to load this class will come to System/Applica on
ClassLoader, which will delegate the request to its parent, Extension
ClassLoader which further delegates to Primordial or Bootstrap class
loader
Now, Bootstrap ClassLoader will look for this class in rt.jar, since this
class is not there, the request will come to Extension ClassLoader which
looks in jre/lib/ext directory and tries to locate this class there, if this
class is found there then Extension ClassLoader will load this class and
Applica on ClassLoader will not load this class, this has been done to
maintain the Uniqueness principle. But if the class is not loaded by
Extension ClassLoader, then this Employee.class will be loaded by
Applica on ClassLoader from the CLASSPATH.
Employee.java:

Output:

If you are thinking why null is printed when we tried to know which
classloader is loading the java.lang.System class then take a look at the
Javadoc :
We can also create our own custom class loader by extending the
ClassLoader class.
Question 65: What is Singleton Design Pattern and how it
can be implemented?
Answer: This is a famous interview ques on, as it involves many follow-
up ques ons as well. I will cover all of them here:
What is Singleton Design Pa ern?
Singleton design pa ern comes under Crea onal Design Pa erns
category and this pa ern ensures that only one instance of class exists
in the JVM.
Singleton pa ern is used in:
- logging, caching, thread pool etc.
- other design pa erns like Builder, Prototype, Abstract
Factory etc.
- core java classes like java.lang.Run me etc.
How to implement Singleton Pa ern
To implement a Singleton pa ern, we have different approaches but all
of them have the below common concepts:

- private constructor to restrict instan a on of the class from


other classes.
- private sta c variable of the same class that is the only
instance of the class.
- public sta c method that returns the instance of the class,
this is the global access point for outer world to get the
instance of the singleton class.
The approaches to implement singleton pa ern are:
Eager Ini aliza on : In eager ini aliza on, the instance of Singleton
Class is created at the me of class loading, this is the easiest method to
create a singleton class but it has a drawback that instance is created
even though client applica on might not even use it.
Example:
A.java:
Test.java:

Output:

We can also access the instance by using the classname like A.instance
because of sta c keyword but we will have to change its access modifier
to ‘public’, so that it is visible outside the singleton class.
Let’s suppose you are crea ng a large object by using a lot of resources,
there may be a chance that object crea on may throw an excep on but
the above way of crea ng a singleton class does not provide any op on
for excep on handling as you can write try-catch only inside a block of
code. There is a solu on to tackle this par cular problem where you can
create the class instance inside a sta c block.

Sta c Block Eager Ini aliza on:

Both the above approaches create the object even before it is used
(ini alized at the me of class loading because of sta c variable and
sta c block), but there are other approaches where we can create a
Singleton class instance in a lazy ini aliza on way i.e. only when
someone asks for it. These approaches are discussed below.

Lazy Ini aliza on: using this way of crea ng Singleton class, the object
will not get created unless someone asks for it. Here we will create the
class instance inside the global access method.
This approach is suitable for only single-threaded applica on, suppose
there are 2 threads and both have checked that the instance is null and
now they are inside the “if(…)” condi on, it will destroy our singleton
pa ern and both threads will have different instances. So, we must
overcome this problem so that our singleton pa ern doesn’t break in
case of mul -threaded environment.

Thread Safe Singleton implementa on: here the easiest way to prevent
mul ple threads from crea ng more than one instance is to make the
global access method ‘synchronized’, this way threads will acquire a lock
first before entering the getInstance() method.
Synchronizing the en re method comes with performance degrada on,
also acquiring the lock and releasing the lock on every call to
getInstance() method seems un-necessary, because only first few calls to
getInstance() method needs to be synchronized, what I mean to say by
this statement is: let’s suppose there are 10 threads that are trying to
call getInstance() method, now you need to apply synchroniza on to
only these 10 threads at this me and the thread which first acquires
the lock will create the object. A er that, every thread will get the same
object because of null check in if condi on, so we can op mize the
above code by using double-checked locking principle , where we will
use a synchronized block inside the if condi on, like shown below:
The reason of second if condi on inside the synchronized block :
suppose there are 2 threads and both called the getInstance() method
at the same me, now they will both be inside the first if condi on as
instance is null at this me, and the first thread which acquires the lock
will create the object and as soon as it exits the synchronized block,
other thread which was wai ng, will acquire the lock and it will also
create another object thus breaking the singleton pa ern. This is why it
is called “double-checked locking”.

Now, some people have faced issues with the above approach in java 1.4
and earlier versions, which was solved in later versions by using ‘vola le’
keyword with the above approach like below:
localRef variable is there for the cases where instance is already
ini alized (discussed above), the vola le field is only accessed once
because we have wri en return localRef not return instance .
There is another approach where an inner sta c class is used to create
the Singleton class instance and it is returned from the global access
method. This approach is called Bill Pugh Singleton Implementa on .
Bill Pugh Singleton Implementa on Program:
The inner class does not get loaded at the me of class A loading, only
when someone calls getInstance() method, it gets loaded and creates
the Singleton instance.

Question 66: What are the different ways in which a


Singleton Design pattern can break and how to prevent
that from happening?
Answer: There are 3 ways which can break Singleton property of a class,
they are discussed below with examples:
Reflec on : Reflec on API in java is used to change the run me behavior
of a class. Hibernate, Spring’s Dependency injec on also uses Reflec on.
So, even though in the above singleton implementa ons, we have
defined the constructor as private, but using Reflec on, even private
constructor can be accessed, so Reflec on can be used to break the
singleton property of a class.
A.java:
No ce, I am using public access modifier with the only instance of
singleton class so that it can be accessed outside this class using just the
class name.
Now, let’s see how Reflec on can break Singleton:

Test.java:
Output:

As we can see from the output, the 2 instances have different hashcode,
thus destroying Singleton.
Now to prevent Singleton from Reflec on, one simple solu on is to
throw an excep on from the private constructor, so when Reflec on
tries to invoke private constructor, there will be an error.

Now, if you run Test.java , you will get below output:


The other solu on to prevent Singleton from Reflec on is using Enums ,
as its constructor cannot be accessed via Reflec on, JVM internally
handles the crea on and invoca on of enum constructor
SingletonEnum.java:

TestSingletonEnum.java:
Output:

Another way which can break Singleton property of a class is:


Serializa on and Deserializa on : Our Singleton class may implement
Serializable interface so that the object’s state can be saved and at a
later point in me, it can be accessed back using Deserializa on, now
the problem here is, when we deserialize the object, a new instance of
the class will be created, thus breaking the singleton pa ern.
See, the example below:
A.java:
Test.java:

Output:
To prevent our Singleton class from Serializa on, there is a method
called readResolve() which is called when ObjectInputStream has read
an object from the stream and is preparing to return it to the caller, so
we can return the only instance of this class from this method, and this
way the only instance of singleton will be assigned to instance2, see
below:
A.java:

Test.java:
Output:

You can also throw an excep on from the readResolve() method, but
returning the only instance approach is be er as your program
execu on will not stop.

One last way which can break Singleton property of a class is:
Cloning : As we know, Cloning is used to create duplicate objects (copy
of the original object). If we create a clone of the instance of our
Singleton class then a new instance will be created thus breaking our
Singleton pa ern.
See the program below:
TestSingleton.java:
Output:

As you can see, both instances have different hashcodes indica ng our
Singleton pa ern is broken, so to prevent this we can override clone
method in our Singleton class and either return the same instance or
throw CloneNotSupportedExcep on from it.
See the program changes below:
Output:

clone() returning the same instance, see the program changes below:

Output:

Question 67: What are the different design patterns you


have used in your projects?
Answer: You will be asked this ques on almost in all interviews
nowadays. So, be prepared with some design pa erns that are used in
mostly all projects, like:
- Factory Design Pa ern
- Abstract Factory Design
Pa ern
- Strategy Design Pa ern
- Builder Design Pa ern
- Singleton Design Pa ern
- Observer Design Pa ern
And any other if you have used in your projects.

Question 68: Explain Volatile keyword in java


Answer: Vola le is a keyword in java, that can be applied only to
variables. You cannot apply vola le keyword to classes and methods.
Applying vola le to a shared variable that is accessed in a mul -
threaded environment ensures that threads will read this variable from
the main memory instead of their own local cache.
Consider below code:

Now, let’s suppose 2 threads are working on this class and both threads
are running on different processors having their own local copy of
variable x. if any thread modifies its value, the change will not be
reflected back in the original variable x in the main memory leading to
data inconsistency because the other thread is not aware of the
modifica on.
So, to prevent data inconsistency, we can make variable x as vola le:

Now, all the threads will read and write the variable x from/to the main
memory. Using vola le, also prevents compiler from doing any
reordering or any op miza on to the code.

Question 69: What is Garbage Collection in Java, how it


works and what are the different types of Garbage
Collectors?
Answer: Garbage collec on in java is the process of looking at heap
memory, iden fying which objects are in use and which are not and
dele ng the unused objects. An unused object or unreferenced object, is
no longer referenced by any part of your program.
Garbage collector is a daemon thread that keeps running in the
background, freeing up heap memory by destroying the unreachable
objects.
There was an analysis done on several applica ons which showed that
most objects are short lived, so this behavior was used to improve the
performance of JVM. In this method, the heap space is divided into
smaller parts or genera ons. These are, Young Genera on , Old or
Tenured Genera on and Permanent Genera on .
The Young Genera on is where all new objects are allocated and aged.
The young genera on is further divided into 3 parts: Eden Space,
Survivor space S0 and Survivor space S1. When the young genera on fills
up, this causes a minor garbage collec on . Some surviving objects are
aged and eventually move to the old genera on. All minor garbage
collec ons are "Stop the World" events. This means that all applica on
threads are stopped un l the opera on completes. Minor garbage
collec ons are always Stop the World events.
The Old Genera on is used to store long surviving objects. Typically, a
threshold is set for young genera on object and when that age is met,
the object gets moved to the old genera on. Eventually the old
genera on needs to be collected. This event is called a major garbage
collec on . Major garbage collec on are also Stop the World events.
O en a major collec on is much slower because it involves all live
objects. So, for Responsive applica ons, major garbage collec ons
should be minimized. Also note that the length of the Stop the World
event for a major garbage collec on is affected by the kind of garbage
collector that is used for the old genera on space.

The Permanent genera on contains metadata required by the JVM to


describe the classes and methods used in the applica on. The
permanent genera on is populated by the JVM at run me based on
classes in use by the applica on. In addi on, Java SE library classes and
methods may be stored here.

Classes may get collected (unloaded) if the JVM finds they are no longer
needed and space may be needed for other classes. The permanent
genera on is included in a full garbage collec on. And Perm Gen was
available ll Java 7, it is removed from Java 8 onwards and JVM uses
na ve memory for the representa on of class metadata which is called
MetaSpace.
There is a flag MaxMetaspaceSize, to limit the amount of memory used
for class metadata. If we do not specify the value for this, the Metaspace
re-sizes at run me as per the demand of the running applica on.

How Garbage collec on works:


When new objects are first created, they are stored in the eden space of
Young Genera on and at that me both Survivor spaces are empty.
When the eden space is filled, then a minor garbage collec on is
triggered. All the unused or un-referenced objects are cleared from the
eden space and the used objects are moved to first Survivor space S0.
At the next minor garbage collec on, same process happens, un-
referenced objects are cleared from the eden space but this me, the
surviving objects are moved to Survivor space S1. In addi on, the objects
that were in S0 will also be matured and they also get moved to S1.
Once all surviving objects are moved to S1, both eden and S0 are
cleared.
At the next minor GC, the same process repeats. When surviving objects
reached a certain threshold, they get promoted from Young genera on
to Old genera on. These minor GC will con nue to occur and objects
will con nue to be promoted to the Old genera on.
Eventually, a major GC will be performed on the Old genera on which
cleans up and compacts the space.

Types of Garbage collector in Java:


Serial GC:
Serial GC is designed for smaller applica ons that have small heap sizes
of up to a few hundred MBs. It only uses single virtual CPU for its
garbage collec on and the collec on is done serially. It takes around
couple of second for Full garbage collec ons.
It can be turned on by using -XX:+UseSerialGC
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseSerialGC -jar C:\temp\test.jar

Parallel/Throughput GC:
Parallel garbage collector uses mul ple threads to perform the garbage
collec on. By default, on a host with N CPUs, this collector uses N
garbage collector threads for collec on. The number of collector threads
can be controlled with the command line op on: -
XX:ParallelGCThreads=<N>
It is called Throughput collector as it uses mul ple CPUs to speed up the
applica on throughput. A drawback of this collector is that it pauses the
applica on threads while performing minor or full GC, so it is best
suited for applica ons where long pauses are acceptable. It is the
default collector in JDK 8.
It can be turned on by using below 2 op ons:
-XX:+UseParallelGC
With this command line op on, you get a mul -thread young genera on
collector with a single-threaded old genera on collector. The op on also
does single-threaded compac on of old genera on.
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseParallelGC -jar C:\temp\test.jar
-XX:+UseParallelOldGC
With this op on, the GC is both a mul threaded young genera on
collector and mul threaded old genera on collector. It is also a
mul threaded compac ng collector.
Compac ng describes the act of moving objects in a way that there are
no holes between objects. A er a garbage collec on sweep, there may
be holes le between live objects. Compac ng moves objects so that
there are no remaining holes. This compac ng of memory makes it faster
to allocate new chunks of memory to the heap.
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseParallelOldGC -jar C:\temp\test.jar

Concurrent Mark Sweep (CMS) Collector:


The CMS collector, also called as the concurrent low pause collector,
collects the tenured genera on. It a empts to minimize the pauses due
to garbage collec on, by doing most of the garbage collec on work
concurrently with the applica on threads.
It can be turned on by passing -XX:+UseConcMarkSweepGC in the
command line op on.
If you want to set number of threads with this collector, pass -
XX:ParallelCMSThreads=<N>
java -Xmx12m -Xms3m -Xmn1m -XX:PermSize=20m -
XX:MaxPermSize=20m -XX:+UseConcMarkSweepGC -
XX:ParallelCMSThreads=2 -jar C:\temp\test.jar

G1 Garbage Collector:
The Garbage First or G1 collector is a parallel, concurrent and
incrementally compac ng low-pause garbage collector
G1 collector par ons the heap into a set of equal-sized heap regions.
When G1 performs garbage collec on then a concurrent global marking
phase is performed to determine the liveliness of objects throughout
the heap. A er this mark phase is complete, G1 knows which regions are
mostly empty. It collects unreachable objects from these regions first,
which usually yields a large amount of free space, also called Sweeping.
So G1 collects these regions (containing garbage) first, and hence the
name Garbage-First.
It can be turned on by passing -XX:+UseG1GC in the command line
op ons
java –Xmx25g –Xms5g -XX:+UseG1GC -jar C:\temp\test.jar

Java 8 has introduced one JVM parameter for reducing the unnecessary
use of memory by crea ng too many instances of the same String. This
op mizes the heap memory by removing duplicate String values to a
global single char[] array. We can use the -XX:+UseStringDeduplica on
JVM argument to enable this op miza on.
G1 is the default garbage collector in JDK 9.
Question 70: Explain Generics in Java
Answer: Java Generics provides a way to reuse the same code with
different inputs.
Advantages:
-
Generics provide compile- me type safety that allows
programmers to catch invalid types at compile me.
Before Generics:

A er Generics:

-
When using Generics, there is no need of type-cas ng.
Before Generics:

A er Generics:
-
By using generics, programmers can implement generic
algorithms that work on collec ons of different types, can be
customized and are type safe and easier to read.

Output:
Mul -threading: you will be asked many ques ons on mul -threading,
so, read as much as you can and whatever you can. Here, I am including
some of the important ques ons that are mostly asked in every
interview.

Question 71: What is Multi-threading?


Answer: Mul -threading is a process of execu ng two or more threads
concurrently, u lizing available CPU resources. A single thread is a
lightweight sub-process and the smallest unit of processing. Threads are
independent, if any excep on occurs in one thread, it does not affect
other threads.
When we execute a Java program without making any separate thread,
then also our program runs on a thread called ‘main thread’.
There are 2 types of threads in an applica on, user thread and daemon
thread. When the applica on is first started, main thread is the first
user thread created. We can create mul ple user threads and daemon
threads.
One thing to remember here is that, JVM does not have any control on a
thread’s execu on. The thread execu on is controlled by Thread
scheduler which is part of Opera ng System. A thread can be assigned a
priority using setPriority(int) method, where 1 is the minimum and 10 is
the maximum priority, however thread priority is not guaranteed as it is
pla orm dependent.
Mul -threading is used in a me-consuming task, one common example
is File Upload.

Question 72: How to create a thread in Java?


Answer: There are 2 ways to create a thread:

- By extending Thread class


- By implemen ng Runnable
interface
By extending Thread class:
ThreadTest.java:

Output:

Here a Task class extends Thread class and overrides the run() method
which contains the business logic, then we make an object of this Task
and call the start() method, which starts the thread execu on. start()
method internally calls run() method .
By implemen ng Runnable interface:
ThreadTest.java:

Output:

If you see the outputs of this and previous program, you will see that
they are different, because any thread can get a chance to execute its
run() method, when the CPU resources are available.
Question 73: Which way of creating threads is better:
Thread class or Runnable interface
Answer: Implemen ng Runnable is always the preferred choice, see the
reasons below:
- As you know, Java does not allow mul ple inheritance
through classes (because of Diamond problem discussed in
Ques on 9), so if you are crea ng threads by extending
Thread class then you will not be able to extend any other
class.
- When we are working with mul -threading, we are not
looking to overwrite any exis ng func onality of Thread
class, we just want to execute the code with mul ple
threads, so in that way also, Runnable is a good choice.
- One more reason to choose Runnable is that, most people
don’t work with just Raw Threads, they use the Executor
framework that is provided from Java 5, that separates the
task from its execu on and we can execute Runnables using
execute(Runnable Task) method of Executor interface .

Question 74: What will happen if I directly call the run()


method and not the start() method to execute a thread
Answer: if run() method is called directly, then a new thread will not be
created instead the code will run on the current thread which is main
thread. Calling run() method directly will make it behave as any other
normal method call. Only a call to start() method creates separate
thread.

Question 75: Once a thread has been started can it be


started again
Answer: No. A thread can be started only once in its life me. If you try
to start a thread which has already been started, an
IllegalThreadStateExcep on is thrown, which is a run me excep on. A
thread in runnable state or a dead thread cannot be restarted.

Question 76: Why wait, notify and notifyAll methods are


defined in the Object class instead of Thread class
Answer: This is another very famous mul -threading interview ques on.
The methods wait, no fy and no fyAll are present in the Object class,
that means they are available to all class objects, as Object class is the
parent of all classes.
wait() method – it tells the current thread to release the lock and go to
sleep un l some other thread enters the same monitor and calls no fy()
no fy() method – wakes up the single thread that is wai ng on the same
object’s monitor
no fyAll() method – wakes up all the threads that called wait() on the
same object

if these methods were in Thread class, then thread T1 must know that
another thread T2 is wai ng for this par cular resource, so T2 can be
no fied by something like T2.no fy()
But in java, the object itself is shared among all the threads, so one
thread acquires the lock on this object’s monitor, runs the code and
while releasing the lock, it calls the no fy or no fyAll method on the
object itself, so that any other thread which was wai ng on the same
object’s monitor will be no fied that now the shared resource is
available. That is why these methods are defined in the Object class.
Threads have no specific knowledge of each other. They can run
asynchronously and are independent. They do not need to know about
the status of other threads. They just need to call no fy method on an
object, so whichever thread is wai ng on that resource will be no fied.
Let’s consider this with a real-life example:
Suppose there is a petrol pump and it has a single washroom, the key of
which is kept at the service desk. This washroom is a shared resource for
all. To use this shared resource, the user must acquire the key to the
washroom lock. So, the user goes to service desk, acquires the key,
opens the door, locks it from the inside and use the facility.
Meanwhile if another user arrives at the petrol pump and wants to use
the washroom, he goes to the washroom and found that it is locked. He
goes to the service desk and the key is not there because it is with the
current user. When the current user finishes, he unlocks the door and
returns the key to the service desk. He does not bother about the
wai ng users. The service desk gives the key to wai ng user. If there are
more than one user wai ng to use the facility, then they must form a
queue.
Now, apply this analogy to Java, one user is one thread and the
washroom is the shared resource which the threads wish to execute. The
key will be synchronized keyword provided by Java, through which
thread acquires a lock for the code it wants to execute and making other
threads wait un l the current thread finishes. Java will not be as fair as
the service sta on, because any of the wai ng threads may get a chance
to acquire the lock, regardless of the order in which the threads came.
The only guarantee is that all the wai ng threads will get to use the
shared resource sooner or later.
In this example, the lock can be acquired on the key object or the service
desk and none of them is a thread. These are the objects that decide
whether the washroom is locked or not.

Question 77: Why wait(), notify(), notifyAll() methods


must be called from synchronized block
Answer: these methods are used for inter-thread communica on. So, a
wait() method only makes sense when there is a no fy() method also.
If these methods are not called from a synchronized block then
- IllegalMonitorStateExcep on will be thrown
- Race condi on can occur
Let’s first look at the IllegalMonitorStateExcep on:
WaitDemo.java:

Output:

Now, let’s understand how a race condi on can occur:


Producer-Consumer problem : The problem describes two processes, the
producer and the consumer, who share a common, fixed-size buffer used
as a queue. The producer's job is to generate data, put it into the buffer
and start again. At the same me, the consumer is consuming the data
(i.e. removing it from the buffer), one piece at a me. The problem is to
make sure that the producer won't try to add data into the buffer, if it's
full and that the consumer won't try to remove data from an empty
buffer.
The solu on for the producer is to either go to sleep or discard data if
the buffer is full. The next me the consumer removes an item from the
buffer, it no fies the producer, who starts to fill the buffer again. In the
same way, the consumer can go to sleep if it finds the buffer empty. The
next me the producer puts data into the buffer, it wakes up the
sleeping consumer.
Pseudo code:

How the race condi on problem can occur:


Suppose, a thread has called the consume() method and it finds that the
buffer is Empty
Now, just before the wait() method is called, another thread calls
produce() and adds an item to the buffer and calls no fy()
And The first thread calls the wait() method. In this case, no fy() call by
the second thread will be missed
if by any chance, produce() method is not called then the consumer
thread will be stuck in wai ng state indefinitely, even though there is
data available in the buffer.
The solu on to above problem is using synchronized method/block to
make sure that no fy() is never called between the condi on isEmpty()
and wait()

Question 78: difference between wait() and sleep()


method
Answer: The differences are:

- wait() method can only be called from a synchronized


context while sleep() method can be called without
synchronized context
- wait() method releases the lock on the object while wai ng
but sleep() method does not release the lock it holds while
wai ng, it means if the thread is currently in a synchronized
block/method then no other thread can enter this
block/method
- wait() method is used for inter-thread communica on while
sleep() method is used to introduce a pause on execu on
- wai ng thread can be waked by calling no fy() or no fyAll(),
while sleeping thread will wake up when the specified sleep
me is over or the sleeping thread gets interrupted
- wait() method is non-sta c, it gets called on an object on
which synchroniza on block is locked while sleep() is a
sta c method, we call this method like Thread.sleep(), that
means it always affects the currently execu ng thread
- wait() is normally called when a condi on is fulfilled like if
the buffer size of queue is full then producer thread will
wait, whereas sleep() method can be called without a
condi on

Question 79: join() method


Answer: join() method causes the current thread to pause execu on
un l the thread which has called join() method is dead.
join() method can be used to execute the threads sequen ally or in
some specific order.
Let’s see an example below:
There are 3 threads and I want to execute them in the order 1, 3, 2:
JoinMethodDemo.java:
Output:

In the code, if you don’t write t2.join(), then current thread will not wait
from the t2 thread to die, see the output below when t2.join()
statement is commented from the code :
There are overloaded versions of join() method also,

- join(long milliseconds) : when this method is called, then


the current thread will wait at most for the specified
milliseconds
- join(long milliseconds, long nanoseconds) : when this
method is called, then the current thread will wait at most
for the specified milliseconds plus nanoseconds.
These join methods are dependent on the underlying Opera ng system
for ming. So, you should not assume that join() will wait exactly as long
as you specify.
You can execute threads in a sequence using CountDownLatch also.

Question 80: yield() method


Answer: yield() method pauses the currently execu ng thread
temporarily for giving a chance to the remaining wai ng threads of the
same priority to execute. If there is no wai ng thread or all the wai ng
threads have a lower priority then the same thread will con nue its
execu on.
When the yielded thread will get the chance for execu on is decided by
the thread scheduler whose behavior is vendor dependent. Yield
method doesn’t guarantee that the current thread will pause or stop
but it guarantees that CPU will be relinquished by current Thread as a
result of a call to Thread.yield() method in java.

Question 81: Tell something about synchronized keyword


Answer: synchronized keyword in java is used to control the access of
mul ple threads to any shared resource, so that any consistency
problem can be avoided.
We can make the en re method as synchronized or just the part where
the shared resource is ge ng used, to do this synchronized blocks are
used.
Synchronized method/block can only have one thread execu ng inside
it, all the other threads trying to enter into the synchronized
method/block will get blocked un l the thread inside finishes its
execu on. When the thread exits the synchronized method/block then
Java guarantees that changes to the state of the object is visible to all
the threads. This eliminates the memory inconsistency errors.

Question 82: What is static synchronization?


Answer: When synchronized keyword is used with a sta c method, then
that is called sta c synchroniza on. In this, lock will be on the class not
the object. This means only one thread can access the class at a me.
The purpose of sta c synchroniza on is to make the sta c data thread-
safe.
Let’s look at some programs:
Here, we have a Hello class which has a synchronized method:
A Task class which implements Runnable and its run() method simply
calls the synchronized method of Hello class:
Our main class:

We have 2 objects of our Hello class, one object is shared among First
and Second thread, and one object is shared among Third and Fourth
thread, and we are star ng these threads.
Output:
As you can see from the output, the First and Second thread are not
having any thread interference. Same way, Third and Fourth thread does
not have any thread interference but First and Third thread are entering
the synchronized method at the same me with their own object locks
(Hello obj1 and obj2).
Lock which is hold by First thread will only stop the Second thread from
entering the synchronized block, because they are working on the same
instance i.e. obj1, but it cannot stop Third or Fourth thread as they are
working on another instance i.e. obj2.

If we want our synchronized method to be accessed by only one thread


at a me then we have to use a sta c synchronized method/block to
have the synchroniza on on the class level rather than on the instance
level.
Let’s see the output now:
Here, only one thread is accessing the sta c synchronized method.
Same can be done by synchronized block also:

Question 83: What will be output of below program where


one synchronized method is calling another synchronized
method?
Output:
One thing to remember here is that Java synchronized keyword is re-
entrant in nature, it means if a synchronized method calls another
synchronized method which requires same lock then current thread
which is holding the lock can enter into that method without acquiring
lock.

Question 84: Programs related to synchronized and static


synchronized methods
There are some confusing programs that interviewer can ask where
some methods are sta c synchronized and some methods are non-sta c
synchronized, and some mes they are calling each other, so let’s discuss
those.
Scenario 1 : There are 2 threads that are calling 2 different sta c
synchronized methods.
Here, these 2 threads will block each other, as only one lock per class
exists. So, these 2 sta c synchronized methods will not be executed at
the same me.
Program:
Output:
Scenario 2 : There are 2 threads, one is calling sta c synchronized
method on one object, and the other thread is calling non-sta c
synchronized method on another object.
Here, these 2 threads will not block each other and will be executed
concurrently as both locks are different, the thread execu ng the sta c
synchronized method holds a lock on the class and the thread execu ng
the non-sta c synchronized method holds the lock on the object on
which the method has been called.
In short, sta c synchronized method do not block a non-sta c
synchronized method.
Program:

(Main class is same as Scenario 1)


Output:
Question 85: What is Callable Interface?
Answer: Callable interface represents an asynchronous task which can
be executed by a separate thread, it has one call() method, which
returns a Future object.
A Runnable can be executed by passing it into the Thread class
constructor but Thread class does not have a constructor that accepts a
Callable, so Callable can be executed only by submit() method of
ExecutorService Interface.
Callable’s call() method can throw checked excep ons, the excep ons
are collected in Future object which can be checked by making a call to
Future.get() method, an Execu onExcep on is thrown which wraps the
original excep on. We can get the original checked excep on by making
a call to getCause() method on the excep on object thrown. However, if
we don’t call Future.get() method then the excep on will not be
reported back and the task will be marked as completed.
One thing you should remember is that the Future.get() method blocks
the execu on, so meouts should be used when using this method to
avoid unexpected waits.
Program showing how Callable is used and how the result is returned in
Future object:

Output:
Let’s pass a nega ve number, so that excep on will be thrown:

Output:

You can call e.getCause() to get the original excep on.

Question 86: How to convert a Runnable to Callable


Answer: We have a u lity method in “Executors” class:
- callable(Runnable task) : Returns a Callable object that,
when called, runs the given task and returns null.
- callable(Runnable task, T result) : Returns a Callable object
that, when called, runs the given task and returns the given
result
Question 87: Difference between Runnable and Callable
Answer: The difference is:

- Runnable tasks can be executed by using Thread class or


ExecutorService interface whereas Callable tasks can be
executed by using ExecutorService interface only
- Return type of Runnable’s run() method is void whereas
Callable’s call() method returns Future object
- Runnable’s run() method does not throw checked
excep ons whereas Callable’s call() method can throw
checked excep ons

Question 88: What is Executor Framework in Java, its


different types and how to create these executors?
Answer: Executor Framework is an abstrac on to managing mul ple
threads by yourself. So, it decouples the execu on of a task and the
actual task itself. Now, we just have to focus on the task that means,
only implement the Runnables and submit them to executor. Then these
runnables will be managed by the executor framework. It is available
from Java 1.5 onwards.
Also, we don’t have to create new threads every me. With executor
framework, we use Thread pools. Think of Thread Pool as a user-defined
number of threads which are called worker threads, these are kept alive
and reused. The tasks that are submi ed to the executor will be
executed by these worker threads. If there are more tasks than the
threads in the pool, they can be added in a Queue and as soon as one of
thread is finished with a task, it can pick the next one from this Queue
or else, it will be added back in the pool wai ng for a task to be
assigned.
So, it saves the overhead of crea ng a new thread for each task. If you
are thinking about what is the problem with crea ng a new thread every
me we want to execute a task, then you should know that crea ng a
thread is an expensive opera on. Thread objects use a significant
amount of memory, and in a large-scale applica on, alloca ng and
dealloca ng many thread objects creates a significant memory
management overhead and new threads without any thro ling will lead
to the crea on of large number of threads. These threads will cause
wastage of resources.
There are 2 main interfaces that you must know, one is Executor and the
other is ExecutorService .
Executor interface contains execute(Runnable task) method through
which you can execute only Runnables. Also, the return type of execute()
method is void, since you are passing a Runnable to it and it does not
return any result back.
ExecutorService interface contains the submit() method which can take
both Runnable and Callable, and its return type is Future object.
ExecutorService extends the Executor Interface, so it also has the
execute() method.

Now, we have an idea of what is an Executor Framework, let’s look at


different types of Executors:
SingleThreadExecutor :
This executor has only one thread and is used to execute tasks in a
sequen al manner. If the thread dies due to an excep on while
execu ng the task, a new thread is created to replace the old thread and
the subsequent tasks are executed in the new thread.
How to create a SingleThreadExecutor:
ExecutorService executor =
Executors.newSingleThreadExecutor ();
Executors is a u lity class which contains many factory methods to
create different types of ExecutorService, like the one called
SingleThreadExecutor, we just created.

FixedThreadPoolExecutor :
As its name suggests, this is an executor with a fixed number of threads.
The tasks submi ed to this executor are executed by the specified
number of threads and if there are more tasks than the number of
threads, then those tasks will be added in a queue (e.g.
LinkedBlockingQueue).
How to create a FixedThreadPoolExecutor:
ExecutorService executor = Executors.newFixedThreadPool
(5);
Here, we have created a thread pool executor of 5 threads, that means
at any given me, 5 tasks can be managed by this executor. If there are
more ac ve tasks, they will be added to a queue un l one of the 5
threads becomes free.
An important advantage of the fixed thread pool is that applica ons
using it degrade gracefully. To understand this, consider a web server
applica on where each HTTP request is handled by a separate thread. If
the applica on simply creates a new thread for every new HTTP request,
and the system receives more requests than it can handle immediately,
the applica on will suddenly stop responding to all requests when the
overhead of all those threads exceed the capacity of the system. With a
limit on the number of the threads that can be created, the applica on
will not be servicing HTTP requests as quickly as they come in, but it will
be servicing them as quickly as the system can sustain.

CachedThreadPoolExecutor :
This executor is mainly used when there are many short-lived tasks to be
executed. If you compare this with the fixed thread pool, here the
number of threads of this executor pool is not bounded. If all the
threads are busy execu ng the assigned tasks and if there is a new task,
then a new thread will be created and added to the pool. If a thread
remains idle for close to sixty seconds, it is terminated and removed
from the cache.
Use this one, if you are sure that the tasks will be short-lived, otherwise
there will be a lot of threads in the pool which will lead to performance
issues.
How to create a CachedThreadPoolExecutor:
ExecutorService executor =
Executors.newCachedThreadPool ();

ScheduledExecutor :
Use this executor, when you want to schedule your tasks, like run them
at regular intervals or run them a er a given delay. There are 2 methods
which are used for scheduling tasks: scheduleAtFixedRate and
scheduleWithFixedDelay .
How to create ScheduledExecutor:
ExecutorService executor =
Executors.newScheduledThreadPool (4);
ScheduledExecutorService interface extends the ExecutorService
interface.

Now, apart from using Executors class to create executors, you can use
ThreadPoolExecutor and ScheduledThreadPoolExecutor class also. Using
these classes, you can manually configure and fine-tune various
parameters of the executor according to your need. Let’s see at some of
those parameters:

Core and Max Pool sizes:


A ThreadPoolExecutor will automa cally adjust the pool size according
to the bounds set by corePoolSize and maximumPoolSize
When a new task is submi ed to the executor then:

- If the number of threads running are less than the


corePoolSize, a new thread is created to handle the request
- If the number of threads running are more than
corePoolSize but less than maximumPoolSize then a new
thread will be created only if the queue is full
Let’s understand this with an example:
You have defined the core pool size as 5, maximum pool size as 10 and
the queue capacity as 100. Now as tasks are coming in, new threads will
be created up to 5, then other new tasks will be added to queue un l it
reaches 100. Now when the queue is full and if new tasks are coming in,
threads will be created up to the maximumPoolSize i.e. 10. Once all the
threads are in use and the queue is also full, the new tasks will be
rejected. As the queue reduces, so does the number of ac ve threads.

Keep Alive Time and TimeUnit:


When the number of threads are greater than the core size, this is the
maximum me that excess idle threads will wait for new tasks before
termina ng. It is used to avoid the overhead of crea ng a new thread.
Let’s understand this with an example:
You have defined the core pool size as 5 and maximum pool size as 15
and all the 15 threads are ge ng used at the moment. Now when the
threads are ge ng finished with their work, the excess 10 threads (15-5)
become idle and eventually die. To avoid these 10 threads being killed
too quickly, we can specify the keep alive me for these by using the
keepAliveTime parameter in the ThreadPoolExecutor constructor. If you
have given its value as 1 and me unit as TimeUnit.MINUTE, each thread
will wait for 1 min a er it had finished execu ng a task. Basically, it is
wai ng for a new task to be assigned. If it is not given any task, it would
let itself complete. And in the end, the executor will be le with the core
threads (5).

BlockingQueue:
The queue to use for holding tasks before they are executed. This queue
will hold only the Runnable tasks submi ed by the execute method, you
can use a ArrayBlockingQueue or LinkedBlockingQueue like:
BlockingQueue<Runnable> queue = new
ArrayBlockingQueue<>(100);

ThreadFactory:
The factory to use when the executor creates a new thread. Using thread
factories removes hardwiring of calls to new Thread , enabling
applica ons to use special thread subclasses, priori es, etc.

RejectedExecu onHandler:
This handler is used when a task is rejected by the executor because all
the threads are busy and the queue is full.
When this handler is not provided and the task submi ed to execute()
method is rejected, then an unchecked RejectedExecu onExcep on is
thrown.
But adding a handler is a good prac ce to follow, there is a method:
void rejectedExecution(Runnable r , ThreadPoolExecutor
executor );
This method will be invoked by ThreadPoolExecutor when execute()
cannot accept a task.

Pu ng it all together:
Question 89: Tell something about awaitTermination()
method in executor
Answer: This method blocks un l all tasks have completed execu on
a er a shutdown request, or the meout occurs, or the current thread is
interrupted, whichever happens first.

It returns true if this executor is terminated and false if the meout is


elapsed before termina on.

Question 90: Difference between shutdown() and


shutdownNow() methods of executor
Answer: An executor will not shut down automa cally even when there
is no task to process. It will stay alive and wait for new work. It will keep
the JVM running.
When shutdown() method is called on an executor, then the executor
will not accept new tasks and it will wait for the currently execu ng tasks
to finish.
When shutdownNow() is called, it tries to interrupt the running threads
and shutdown the executor immediately. However, there is no guarantee
that all the running threads will be stopped at the same me.
One good way to shutdown an executor is to use both of these methods
along with awaitTermina on(). With this approach, the executor will
stop accep ng new tasks and waits up to the specified dura on for all
running tasks to be completed. If the me expires, it will shutdown
immediately.

Question 91: What is Count down latch in Java?


Answer: CountDownLatch is used in requirements where you want one
or more tasks to wait for some other tasks to finish before it starts its
own execu on. One example can be a server which is dependent on
some services to be up and running before it can start processing
requests.
How to use: When we create an object of CountDownLatch, then we
specify the number of threads that it should wait for, then wai ng
thread calls the countDownlatch.await() method and un l all the
specified thread calls countDownLatch.countDown() method, the wai ng
thread will not start its execu on.
For example, there are 3 services which a server is dependent on, before
the server accepts any request, these services should be up and running.
We will create a CountDownLatch by specifying 3 threads that the main
thread should wait for, then main thread will call await() method means
it will wait for all 3 threads. Once the threads are complete they will call
countdown() method, decreasing the count by 1. The main thread will
start its execu on only when count reaches to zero.
Output:

CountDownLatch can also be used to start mul ple threads at the same
me, you can create a CountDownLatch of size 1, make all the other
threads wait by calling countDownLatch.await(), then a single call to
countDownLatch.countDown() method will resume execu on for all the
wai ng threads at the same me.
CountDownLatch cannot be reused once the count reaches to zero,
therefore in those scenarios, CyclicBarrier is used.

Question 92: What is Cyclic Barrier?


Answer: CyclicBarrier is used to make mul ple threads wait for each
other to reach a common barrier point. It is used mainly when mul ple
threads perform some calcula on and the result of these threads needs
to be combined to form the final output.
All the threads that wait for each other to reach the barrier are called
par es, CyclicBarrier is created with a number of par es to wait for, and
the threads wait for each other at the barrier by calling
cyclicBarrier.await() method which is a blocking method and it blocks
un l all threads have called await().
The barrier is called Cyclic because it can be re-used a er the wai ng
threads are released, by calling cyclicBarrier.reset() method which resets
barrier to the ini al state.
A CyclicBarrier supports an op onal Runnable command that is run once
per barrier point, a er the last thread in the party arrives, but before
any threads are released. This barrier ac on is useful for upda ng
shared-state before any of the par es con nue.

The CyclicBarrier uses an all-or-none breakage model for failed


synchroniza on a empts: If a thread leaves a barrier point prematurely
because of interrup on, failure, or meout, all other threads wai ng at
that barrier point will also leave abnormally via BrokenBarrierExcep on
(or InterruptedExcep on if they too were interrupted at about the same
me).
Example: One thread is adding first 5 natural numbers to the list, the
other thread is adding next 5 numbers to the list and we will perform an
addi on of all these numbers to compute the sum of first 10 natural
numbers
Output:
Question 93: Atomic classes
Answer: The java.u l.concurrent.atomic package defines classes that
support atomic opera ons on single variables. All classes have get and
set methods that work like reads and writes on vola le variables. That
is, a set call has a happens-before rela onship with any subsequent get
call on the same variable.
For example, consider below code:
In the above runnable task, we are just incremen ng an integer 50
mes,
We are using join() method so that the main thread will wait for the
thread t1 to die, otherwise the sysout will be executed before t1 has
finished execu on. Let’s see what will be the output when only one
thread is running:
Output:

As, you can see, the output is as expected because only one thread is
accessing the count, let’s see what will happen in case the count
variable is accessed by more than one thread, un-comment the code
regarding second thread t2 and run the main class:
Output:

The expected output was 100 but we got a different output, if you run
the above program you will see a different output and it will be
anywhere between 50 and 100. The reason for this is that 2 threads are
accessing a mutable variable without any synchroniza on. One solu on
that will be coming to your mind will be using synchroniza on block,
and yes this problem can be solved using that but it will have a
performance impact, as threads will acquire the lock, update the value
and release the lock, and then giving other threads access to the shared
mutable variable.
But java has provided Atomic wrapper classes for this purpose that can
be used to achieve this atomic opera on without using Synchroniza on.
Let’s see the change in our Runnable:

Output:
Question 94: What is Collection Framework?
Answer: Collec on framework represents an architecture to store and
manipulate a group of objects. All the classes and interfaces of this
framework are present in java.u l package.
Some points:

- Iterable interface is the root interface for all collec on classes,


it has one abstract method iterator()
- Collec on interface extends the Iterable interface

Question 95: What is Collections?


Answer: Collec ons is a u lity class present in java.u l package that we
can use while using collec ons like List, Set, and Map etc.
Some commonly used methods are Collec ons.sort(),
Collec ons.unmodifiableList() etc.

Question 96: What is ArrayList?


Answer: ArrayList is a resizable-array implementa on of List Interface.
When we create an object of ArrayList then a con guous block of
memory is allocated to hold its elements. As it is a con guous block, all
the elements address is already known, that is how ArrayList allows
random access.
Some points about ArrayList class:

- ArrayList class maintains inser on order


- ArrayList class can contain duplicate elements
- ArrayList class allows random access to its elements as it works
on index basis
- ArrayList class is not synchronized
- You can add any number of null elements in the ArrayList class
Time complexity of ArrayList’s get(), add(), remove() opera ons:
get(): constant me
add(): here the new element can be added at the first, middle or last
posi ons, if you are using add(element), then it will append element at
the last posi on and will take O(1), provided that the arrayList is not full
otherwise it will create a new arrayList of one and a half mes the size
of previous arrayList and copy all arrayList elements to this new
arrayList, making it O(n).
If the element is added in the middle or at any specific index, let’s say at
index 2, then a space needs to be made to insert this new element by
shi ing all the elements one posi on to its right, making it O(n).
add() opera on runs in amor zed constant me.
remove(): it is also same as add(), if you want to remove element from a
specific index, then all elements to its right needs to be shi ed one
posi on to their le , making it O(n) but if element needs to be removed
from the last, then it will take O(1).

Question 97: What is default size of ArrayList?


Answer: 10
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;

Question 98: Which data structure is used internally in an


ArrayList?
Answer: Internally, ArrayList uses Object[]
/**
* The array buffer into which the elements of the
ArrayList are stored.
* The capacity of the ArrayList is the length of this
array buffer. Any
* empty ArrayList with elementData ==
DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first
element is added.
*/
transient Object[] elementData ;

Question 99: How add() method works internally or How


the ArrayList grows at runtime
Answer: this is what happens when we create an ArrayList object using
its default constructor,
/**
* Constructs an empty list with an initial capacity of
ten.
*/
public ArrayList() {
this .elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA
;
}
Here, elementData is a transient variable and
DEFAULTCAPACITY_EMPTY_ELEMENTDATA is an empty Object[] array:
transient Object[] elementData ;
private static final Object[]
DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
Now, let’s see Javadoc of add() method
/**
* Appends the specified element to the end of this
list.
*
* @param e element to be appended to this list
* @return <tt> true </tt> (as specified by {@link
Collection#add} )
*/
public boolean add(E e ) {
ensureCapacityInternal(size + 1); // Increments
modCount!!
elementData [size ++] = e ;
return true ;
}
Here size is a private variable
/**
* The size of the ArrayList (the number of elements it
contains).
*
* @serial
*/
private int size ;
The default value of size will be 0, so call to ensureCapacityInternal() will
have value 1, now let’s see ensureCapacityInternal() Javadoc:
private void ensureCapacityInternal(int minCapacity ) {
ensureExplicitCapacity(calculateCapacity (elementData
,minCapacity ));
}
Here minCapacity is holding value 1, calculateCapacity() method is:
private static int calculateCapacity(Object[]
elementData , int minCapacity ) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
) {
return Math.max (DEFAULT_CAPACITY , minCapacity );
}
return minCapacity ;
}
Now, as both elementData and
DEFAULTCAPACITY_EMPTY_ELEMENTDATA are same (see the default
ArrayList constructor above), if condi on will be true and then
Math.max(10,1) will return 10 from calculateCapacity() method
Now, 10 will be passed to ensureExplicitCapacity()
private void ensureCapacityInternal(int minCapacity ) {
ensureExplicitCapacity(calculateCapacity (elementData
, minCapacity ));
}
private void ensureExplicitCapacity(int minCapacity ) {
modCount ++;
// overflow-conscious code
if (minCapacity - elementData .length > 0)
grow(minCapacity );
}
modCount is used when we are itera ng over ArrayList using Iterator or
ListIterator, here minCapacity is 10 and elementData.length will be 0, so
if condi on will be sa sfied and grow() method will be called with value
10:
private void grow(int minCapacity ) {
// overflow-conscious code
int oldCapacity = elementData .length ;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity ;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity (minCapacity );
// minCapacity is usually close to size, so this is a
win:
elementData = Arrays.copyOf (elementData ,
newCapacity );
}
Here, oldCapacity will be 0, and newCapacity will also be 0, so the first if
condi on will be sa sfied because (0-10 < 0), so newCapacity will be
minCapacity i.e. 10, the second if condi on will not be sa sfied as
MAX_ARRAY_SIZE is a very huge number,
private static final int MAX_ARRAY_SIZE =
Integer.MAX_VALUE - 8;
So, the ensureCapacityInternal() of add() will be executed :
public boolean add(E e ) {
ensureCapacityInternal(size + 1); // Increments
modCount!!
elementData [size ++] = e ;
return true ;
}
Element e will be added to object array elementData at index 0 and size
will be incremented and finally add() method will return true, now this
same process will repeat.
So, we can say before adding the element in arrayList, first it is ensured
that the array can hold the element, if not the capacity will be increased
and for this, grow() method is called. Suppose you are trying to add 11th
element in the list, then grow() method will be called and the statement
int newCapacity = oldCapacity + (oldCapacity >> 1); will make the new
capacity to be one and a half mes(50% increase) the size of list.
Let’s make a simple code to understand this line:

Output:
Question 100: How to make an ArrayList as Immutable
Answer: This is also a common interview ques on nowadays. If your
answer is making the list as “final” then see the code below:
Program 1:

Output:

Although, we have made the list as final but s ll we are able to add
elements into it, remember applying final keyword to a reference
variable ensures that it will not be referenced again meaning you cannot
give a new reference to list variable:
So, to make the list as unmodifiable, there is a method
unmodifiableList() in Collec ons u lity class,
Program 2:

Output:
Here, if you assign Collec ons.unmodifiableList (list); to a new reference
then you will be able to change the original list which will in turn change
the new list also, see below:
Program 3:

Output:

Guava library also provides certain ways to make immutable list and
Java 9 has List.of() method.
There are other u lity methods also, to make unmodifiable collec ons:

Question 101: What is LinkedList?


Answer: Java LinkedList class is an implementa on of linked list data
structure and it uses a doubly linked list to store the elements. In Java
LinkedList, elements are not stored in con guous loca ons, they are
stored at any available space in memory and they are linked with each
other using pointers and addresses.
As Java LinkedList internally uses doubly linked list, so LinkedList class
represents its elements as Nodes. A Node is divided into 3 parts:
Previous, Data, Next
Where Previous points to the previous Node in the list, Next points to
the next Node in the list and Data is the actual data.
Some points about LinkedList class:

- LinkedList class maintains inser on order


- LinkedList class can contain duplicate elements
- LinkedList class is not synchronized
- LinkedList class can be used as list, stack or queue
- You can add any number of null elements in LinkedList
Time complexity of LinkedList’s get(), add() and remove():
get(): As LinkedList does not store its elements in con guous block of
memory, random access is not supported here, elements can be
accessed in sequen al order only, so get() opera on in LinkedList is O(n).
add() and remove(): Both add and remove opera ons in LinkedList is
O(1), because no elements shi ing is needed, just pointer modifica on is
done (although remember ge ng to the index where you want to
add/remove will s ll be O(n)).
Here, I am showing some por ons of LinkedList Javadoc’s:
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable,
java.io.Serializable
{
transient int size = 0;
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item !=
null)
*/
transient Node<E> first ;
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item !=
null)
*/
transient Node<E> last ;
/**
* Constructs an empty list.
*/
public LinkedList() {
}
We can see that the LinkedList class implements List and Deque
interfaces. There are first and last Node references also.
Let’s see the add() method:
public boolean add(E e ) {
linkLast(e );
return true ;
}
void linkLast(E e ) {
final Node<E> l = last ;
final Node<E> newNode = new Node<>(l , e , null );
last = newNode ;
if (l == null )
first = newNode ;
else
l .next = newNode ;
size ++;
modCount ++;
}
Here, in linkLast() method, Node class is used, let’ see that:
private static class Node<E> {
E item ;
Node<E> next ;
Node<E> prev ;
Node(Node<E> prev , E element , Node<E> next ) {
this .item = element ;
this .next = next ;
this .prev = prev ;
}
}
Here, we can see that Node class has 3 fields: item, prev and next.

Question 102: When to use ArrayList / LinkedList


Answer: When you have a requirement in which you will be doing a lot
of add or remove opera ons near the middle of list, then prefer
LinkedList, and when you have a requirement, where the frequently
used opera on is searching an element from the list, then prefer
ArrayList as it allows random access.

Question 103: What is HashMap?


Answer: HashMap class implements the Map interface and it stores data
in key, value pairs. HashMap provides constant me performance for its
get() and put() opera ons, assuming the equals and hashcode method
has been implemented properly, so that elements can be distributed
correctly among the buckets.
Some points to remember:
- Keys should be unique in HashMap, if you try to insert the
duplicate key, then it will override the corresponding key’s
value
- HashMap may have one null key and mul ple null values
- HashMap does not guarantee the inser on order (if you want
to maintain the inser on order, use LinkedHashMap class)
- HashMap is not synchronized
- HashMap uses an inner class Node<K, V> for storing map
entries
- Hashmap has a default ini al capacity of 16, which means it
has 16 buckets or bins to store map entries, each bucket is a
singly linked list. The default load factor in HashMap is 0.75
- Load factor is that threshold value which when crossed will
double the hashmap’s capacity i.e. when you add 13th element
in hashmap, the capacity will increase from 16 to 32

Question 104: Explain the internal working of put() and


get() methods of HashMap class and discuss HashMap
collisions
Answer: If you are giving a Core java interview, then you must prepare
for this ques on, as you will most likely be asked about this. So, let’s get
right into it:
put() method internal working:
When you call map.put(key,value), the below things happens:
- Key’s hashCode() method is called
- Hashmap has an internal hash func on which takes the key’s
hashCode and it calculates the bucket index
- If there is no element present at that bucket index, our <key,
value> pair along with hash is stored at that bucket
- But if there is an element present at the bucket index, then
key’s hashCode is used to check whether this key is already
present with the same hashCode or not.
If there is key with same hashCode, then equals method is
used on the key. If equals method returns true, then the key’s
previous value is replaced with the new value otherwise a new
entry is appended to the linked list.

get() method internal working:


When you call map.get(key), the below things happen:

- Key’s hashCode() method is called


- Hash func on uses this hashCode to calculate the index, just
like in put method
- Now the key of element stored in bucket is compared with the
passed key using equals() method, if both are equals, value is
returned otherwise the next element is checked if it exists.

See HashMap’s Javadoc:


Default capacity:
/**
* The default initial capacity - MUST be a power of
two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; //
aka 16

Load factor:
/**
* The load factor used when none specified in
constructor.
*/
static final float DEFAULT_LOAD_FACTOR = 0.75f;

Node class:
static class Node<K,V> implements Map.Entry<K,V> {
final int hash ;
final K key ;
V value ;
Node<K,V> next ;
Node(int hash , K key , V value , Node<K,V> next ) {
this .hash = hash ;
this .key = key ;
this .value = value ;
this .next = next ;
}

Internal Hash func on:


static final int hash(Object key ) {
int h ;
return (key == null ) ? 0 : (h = key .hashCode()) ^
(h >>> 16);
}

Internal Data structure used by HashMap to hold buckets:


transient Node<K,V>[] table ;

HashMap’s default constructor:


/**
* Constructs an empty <tt> HashMap </tt> with the
default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this .loadFactor = DEFAULT_LOAD_FACTOR ; // all other
fields defaulted
}

So, to conclude, Hashmap internally uses an array of Nodes named as


table where each Node contains the calculated hash value, the key-value
pair and the address to the next node.
HashMap collisions: it is possible that mul ple keys will make the hash
func on generate the same index, this is called a collision. It happens
because of poor hashcode method implementa on.
One collision handling technique is called Chaining. Since every
element in the array is a linked list, the keys which have the same hash
func on will be appended to the linked list.
Performance improvement in Java 8 : It is possible that due to mul ple
collisions, the linked list size has become very large, and as we know,
searching in a linked list is O(n), it will impact the constant me
performance of hashmap’s get() method. So, in Java 8, if the linked list
size becomes more than 8, the linked list is converted to a binary search
tree which will give a be er me complexity of O(log n).
/**
* The bin count threshold for using a tree rather than
list for a
* bin. Bins are converted to trees when adding an
element to a
* bin with at least this many nodes. The value must be
greater
* than 2 and should be at least 8 to mesh with
assumptions in
* tree removal about conversion back to plain bins upon
* shrinkage.
*/
static final int TREEIFY_THRESHOLD = 8;
Program showing the default capacity:

Output:

Program showing that hashmap’s capacity gets doubled a er load


factor’s threshold value breaches :
Output:

Change the for loop condi on from i<13 to i<=13, see below:
Output:

Question 105: equals and hashCode method scenarios in


HashMap when the key is a custom class
Answer: equals and hashCode methods are called when we store and
retrieve values from hashmap. So, when the interviewer asks this
ques on, it is mostly asked with an example, where the hashmap’s key is
a custom class and you are given some situa ons where either equals()
is implemented or hashCode() is implemented, some mes properly,
some mes not. We will discuss all combina ons with programs below so
you can give the correct answer in any situa on.
Before we con nue, just remember if in your custom class, you are not
implemen ng equals() and hashCode(), then the Object class equals()
and hashCode() will be called, and also remember about the contract
between these 2 methods. It says when 2 objects are equal according to
equals() method, then their hashCode must be same, reverse may not
be true.
Scenario 1: when custom class does not implement both equals and
hashCode methods

Here, Employee class has not given equals() and hashCode() method
implementa on, so Object’s class equals() and hashCode() methods will
be used when we use this Employee class as hashmap’s key, and
remember, equals() method of Object class compares the reference.
TestHashMap.java:
Can you predict the output of this one?
Output:
Here, Employee objects e1 and e2 are same but they are both inserted
in the HashMap because both are created using new keyword and
holding a different reference, and as the Object’s equals() method
checks reference, they both are unique.
And as for objects e3 and e4, they both are poin ng to same reference
(e4 = e3), so they are equal according to Object’s equals() method hence
the value of e3 which was 300 gets replaced with the value 400 of the
same key e4, and finally size of HashMap is 3.

Scenario 2: when only equals() method is implemented by Employee


class
Now, can you predict the output of our TestHashMap class?
Let’s see the output:

Well, nothing’s changed here. Because even though e1 and e2 are equal
according to our newly implemented equals() method, they s ll have
different hashCode as the Object’s class hashCode() is used. So the
equals and hashCode contract is not followed and both e1, e2 got
inserted in HashMap.

Scenario 3: when only hashCode() method is implemented:


Let’s run our TestHashMap class again and see the output:
Well, now we have same hashCode for e1 and e2, but Object’s equals
method s ll checks the references and as references are different, both
are not equal and are inserted in the hashmap.

Scenario 4: When both equals and hashCode are implemented properly:


Output:
Here, both e1 and e2 are equals as we are comparing the contents of
them in our equals() method, so their hashCodes must be same, which
they are. So value of e1 which was 100 got replaced by 200, and size of
hashmap is 2.
You can be asked to write the equals() and hashCode() methods
implementa on by hand also, so you should pay a en on to how these
are implemented.

Question 106: How to make a HashMap synchronized?


Answer: Collec ons.synchronizedMap(map);

Question 107: What is Concurrent HashMap?


Answer: ConcurrentHashMap class provides concurrent access to the
map, this class is very similar to HashTable, except that
ConcurrentHashMap provides be er concurrency than HashTable or
even synchronizedMap.
Some points to remember:

- ConcurrentHashMap is internally divided into segments, by


default size is 16 that means, at max 16 threads can work at a
me
- Unlike HashTable, the en re map is not locked while
reading/wri ng from the map
- In ConcurrentHashMap, concurrent threads can read the value
without locking
- For adding or upda ng the map, the lock is obtained on
segment level, that means each thread can work on each
segment during high concurrency
- Concurrency level defines a number, which is an es mated
number of threads concurrently accessing the map
- ConcurrentHashMap does not allow null keys or null values
- put() method acquires lock on the segment
- get() method returns the most recently updated value
- iterators returned by ConcurrentHashMap are fail-safe and
never throw ConcurrentModifica onExcep on

Question 108: What is HashSet class and how it works


internally?
Answer: HashSet is a class in Java that implements the Set Interface and
it allows us to have the unique elements only. HashSet class does not
maintain the inser on order of elements, if you want to maintain the
inser on order, then you can use LinkedHashSet.

Internal implementa on of HashSet:


HashSet internally uses HashMap and as we know the keys are unique in
hashmap, the value passed in the add() method of HashSet is stored as
the key of hashmap, that is how Set maintains the unique elements.
/**
* Constructs a new, empty set; the backing <tt> HashMap
</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}

private transient HashMap<E,Object> map ;

Let’s see add() method’s Javadoc:


public boolean add(E e ) {
return map .put(e , PRESENT )==null ;
}
// Dummy value to associate with an Object in the
backing Map
private static final Object PRESENT = new Object();

So, when we call hashSet.add(element) method then


- map.put() is called where key is the element and value is the
dummy value (the map.put() method internal working has
already been discussed above)
- if value is added in the map then put method will return null
which will be compared with null, hence returning true from
hashSet.add() method indica ng the element is added
- however if the element is already present in the map, then the
value associated with the element will be returned which in
turn will be compared with null, returning false from
hashSet.add() method
set.contains() method:
public boolean contains(Object o ) {
return map .containsKey(o );
}
The passed object is given to map.containsKey() method, as the
HashSet’s values are stored as the keys of internal map.

NOTE: If you are adding a custom class object inside the HashSet, do
follow equals and hashCode contract. You can be asked the equals and
hashCode scenarios ques ons, just like we discussed in HashMap
(Ques on 105).

Question 109: Explain Java’s TreeMap


Answer: TreeMap class is one of the implementa on of Map interface.
Some points to remember:
- TreeMap entries are sorted based on the natural ordering of
its keys. This means if we are using a custom class as the key,
we have to make sure that the custom class is implemen ng
Comparable interface
- TreeMap class also provides a constructor which takes a
Comparator object, this should be used when we want to do a
custom sor ng
- TreeMap provides guaranteed log(n) me complexity for the
methods such as containsKey(), get(), put() and remove()
- TreeMap iterator is fail-fast in nature, so any concurrent
modifica on will result in ConcurrentModifica onExcep on
- TreeMap does not allow null keys but it allows mul ple null
values
- TreeMap is not synchronized, so it is not thread-safe. We can
make it thread-safe by using u lity method,
Collec ons.synchronizedSortedMap(treeMap)
- TreeMap internally uses Red-Black tree based NavigableMap
implementa on.

Red-Black tree algorithm has the following proper es:


- Color of every node in the tree is either red or black.
- Root node must be Black in color.
- Red node cannot have a red color neighbor node.
- All paths from root node to the null should consist the same
number of black nodes

Program 1: Using Wrapper class as key


Output:

Here, Integer class already implements Comparable interface, so the


keys are sorted based on the Integer’s natural sor ng order (ascending
order).
Let’s see, when key is a custom class:
Program 2:
Output:

We get ClassCastExcep on at run me. Now, let’s implement Comparable


interface in Employee class and provide implementa on of its
compareTo() method:

Program 3:
Here, we are sor ng based on Employee name,
Output:

Let’s look at a program where we pass a Comparator in the TreeMap


constructor, and sort the Employee object’s based on age in descending
order:
Program 4:
Output:

Here, in Employee class, I have not implemented equals() and


hashCode()
TreeMap’s Javadoc:
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable,
java.io.Serializable
{
/**
* The comparator used to maintain order in this tree
map, or
* null if it uses the natural ordering of its keys.
*
* @serial
*/
private final Comparator<? super K> comparator ;
private transient Entry<K,V> root ;

No-arg TreeMap constructor:


public TreeMap() {
comparator = null ;
}

TreeMap constructor which takes comparator object:


public TreeMap(Comparator<? super K> comparator ) {
this .comparator = comparator ;
}

TreeMap.put() method excerpt:


public V put(K key , V value ) {
Entry<K,V> t = root ;
if (t == null ) {
compare(key , key ); // type (and possibly null)
check
root = new Entry<>(key , value , null );
size = 1;
modCount ++;
return null ;
}
int cmp ;
Entry<K,V> parent ;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator ;
if (cpr != null ) {
do {
parent = t ;
cmp = cpr .compare(key , t .key );
if (cmp < 0)
t = t .left ;
else if (cmp > 0)
t = t .right ;
else
return t .setValue(value );
} while (t != null );
}

Question 110: Explain Java’s TreeSet


Answer: TreeSet class is one of the implementa on of Set interface
Some points to remember:

- TreeSet class contains unique elements just like HashSet


- TreeSet class does not allow null elements
- TreeSet class is not synchronized
- TreeSet class internally uses TreeMap, i.e. the value added in
TreeSet is internally stored in the key of TreeMap
- TreeSet elements are ordered using their natural ordering or
by a Comparator which can be provided at the set crea on
me
- TreeSet provides guaranteed log(n) me cost for the basic
opera ons (add, remove and contains)
- TreeSet iterator is fail-fast in nature

TreeSet Javadoc:
public class TreeSet<E> extends AbstractSet<E>
implements NavigableSet<E>, Cloneable,
java.io.Serializable
public TreeSet() {
this (new TreeMap<E,Object>());
}

public TreeSet(Comparator<? super E> comparator ) {


this (new TreeMap<>(comparator ));
}

Question 111: Difference between fail-safe and fail-fast


iterators
Answer: Iterators in Java are used to iterate over the Collec on objects.
Fail-fast iterators : immediately throw ConcurrentModifica onExcep on,
if the collec on is modified while itera ng over it. Iterator of ArrayList
and HashMap are fail-fast iterators.
All the collec ons internally maintain some sort of array to store the
elements, Fail-fast iterators fetch the elements from this array.
Whenever, we modify the collec on, an internal field called modCount is
updated. This modCount is used by Fail-safe iterators to know whether
the collec on is structurally modified or not. Every me when the
Iterator’s next() method is called, it checks the modCount. If it finds that
modCount has been updated a er the Iterator has been created, it
throws ConcurrentModifica onExcep on.
Program 1:
Output:

But they don’t throw the excep on, if the collec on is modified by
Iterator’s remove() method.
Program 2:
Output:

Javadoc:
arrayList.iterator() method:
public Iterator<E> iterator() {
return new Itr();
}
Itr is a private nested class in ArrayList:
private class Itr implements Iterator<E> {
int cursor ; // index of next element to return
int lastRet = -1; // index of last element returned;
-1 if no such
int expectedModCount = modCount ;
Itr() {}
public boolean hasNext() {
return cursor != size ;
}

Itr.next() method:
@SuppressWarnings ("unchecked" )
public E next() {
checkForComodification();
int i = cursor ;
if (i >= size )
throw new NoSuchElementException();
Object[] elementData = ArrayList.this .elementData ;
if (i >= elementData .length )
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData [lastRet = i ];
}

See the first statement is a call to checkForComodifica on():


final void checkForComodification() {
if (modCount != expectedModCount )
throw new ConcurrentModificationException();
}

On the other hand, Fail-safe iterators does not throw


ConcurrentModifica onExcep on , because they operate on the clone of
the collec on, not the actual collec on. This also means that any
modifica on done on the actual collec on goes unno ced by these
iterators. The last statement is not always true though, some mes it can
happen that the iterator may reflect modifica ons to the collec on a er
the iterator is created. But there is no guarantee of it.
CopyOnWriteArrayList, ConcurrentHashMap are the examples of fail-safe
iterators.
Program 1: ConcurrentHashMap example

Output:

Here, iterator is reflec ng the element which was added during the
itera on opera on.

Program 2: CopyOnWriteArrayList example


Output:

Question 112: Difference between Iterator and ListIterator


Answer:
- Iterator can traverse the collec on only in one direc on i.e.
forward direc on but ListIterator can traverse the list in both
direc ons, forward as well as backward, using previous() and
next() method
- Iterator cannot add element to a collec on while itera ng over
it, but ListIterator can add elements while itera ng over the list
- Iterator cannot modify an element while itera ng over a
collec on, but ListIterator has set(E e) method which can be
used to modify the element
- Iterator can be used with List, Set or Map, but ListIterator only
works with List
- Iterator has no method to obtain an index of the collec on
elements but ListIterator has methods like previousIndex() and
nextIndex() which can be used to obtain the index

Question 113: Difference between Iterator.remove and


Collection.remove()
Answer: Iterator.remove() does not throw
ConcurrentModifica onExcep on while itera ng over a collec on but
Collec on.remove() method will throw
ConcurrentModifica onExcep on.

Java Collec on framework is very important topic when preparing for


the interviews, apart from the above ques ons, you can read about
Stack, Queue topics also, but if you are short on me, what we have
discussed so-far should be enough for the interview.
Question 114: What is the difference between a Monolith
and Micro-service architecture?
Answer: In monolithic architecture, applica ons are built as one large
system, whereas in micro-service architecture we divide the applica on
into modular components which are independent of each other.
Monolithic architecture has some advantages:
- Development is quite simple
- Tes ng a monolith is also simple, just start the applica on and
do the end-to-end tes ng, Selenium can be used to do the
automa on tes ng
- These applica ons are easier to deploy, as only one single
jar/war needs to be deployed
- Scaling is simple when the applica on size is small, we just
have to deploy one more instance of our monolith and
distribute the traffic using a load balancer
- Network latency is very low/none because of one single
codebase
However, there are various disadvantages of monolith architecture as
well:

- Rolling out a new version means redeploying the en re


applica on
- Scaling a monolith applica on becomes difficult once the
applica on size increases. It also becomes difficult to manage
- The size of the monolith can slow down the applica on start-
up and deployment me
- Con nuous deployment becomes difficult
- A bug in any module can bring down the en re applica on
- It is very difficult to adopt any new technology in a monolith
applica on, as it affects the whole applica on, both in terms of
me and cost
Micro-service architecture gives following advantages:
- Micro-services are easier to manage as they are rela vely
smaller in size
- Scalability is a major advantage of using a micro-service
architecture, each micro-service can be scaled independently
- Rolling out a new version of micro-service means redeploying
only that micro-service
- A bug in micro-service will affect only that micro-service and its
consumers, not the en re applica on
- Micro-service architecture is quite flexible. We can choose
different technologies for different micro-services according to
business requirements
- It is not that difficult to upgrade to newer technology versions
or adopt a newer technology as the codebase is quite smaller
(this point is debatable in case the micro-service becomes very
large)
- Con nuous deployment becomes easier as we only have to re-
deploy that micro-service
Despite all these advantages, Micro-services also comes with various
disadvantages:
- As micro-services are distributed, it becomes complex
compared to monolith. And this complexity increases with the
increase in micro-services
- As micro-services will need to communicate with each other, it
increases the network latency. Also, extra efforts are needed
for a secure communica on between micro-services
- Debugging becomes very difficult as one micro-service will be
calling other micro-services and to figure out which micro-
service is causing the error, becomes a difficult task in itself
- Deploying a micro-service applica on is also a complex task, as
each service will have mul ple instances and each instance will
need to be configured, deployed, scaled and monitored
- Breaking down a large applica on into individual components
as micro-services is also a difficult task

We have discussed both advantages and disadvantages of monolith and


micro-services, you can easily figure out the differences between them,
however, I am also sta ng them below.
The differences are:
- In a monolithic architecture, if any fault occurs, it might bring
down the en re applica on as everything is ghtly coupled,
however, in case of micro-service architecture, a fault affects
only that micro-service and its consumers
- Each micro-service can be scaled independently according to
requirement. For example, if you see that one of your micro-
service is taking more traffic, then you can deploy another
instance of that micro-service and then distribute the traffic
between them. Now, with the help of cloud compu ng services
such as AWS, the applica ons can be scaled up and down
automa cally. However, in case of monolith, even if we want to
scale one service within the monolith, we will have to scale the
en re monolith
- In case of monolith, the en re technology stack is fixed at the
start. It will be very difficult to change the technology at a later
stage in me. However, as micro-services are independent of
each other, they can be coded in any language, taking the
advantage of different technologies according to the use-case.
So micro-services gives you the freedom to choose different
technologies, frameworks etc.
- Deploying a new version of a service in monolith requires more
me and it increases the applica on down me, however,
micro-services entails compara vely lesser down me

Question 115: What is Dependency Injection in Spring?


Answer: Dependency Injec on is the most important feature of Spring
framework. Dependency Injec on is a design pa ern where the
dependencies of a class are injected from outside, like from an xml file.
It ensures loose-coupling between classes.
In a Spring MVC applica on, the controller class has dependency of
service layer classes and the service layer classes have dependencies of
DAO layer classes.
Suppose class A is dependent on class B. In normal coding, you will
create an object of class B using ‘new’ keyword and call the required
method of class B. However, what if you can tell someone to pass the
object of class B in class A? Dependency injec on does this. You can tell
Spring, that class A needs class B object and Spring will create the
instance of class B and provide it in class A.
In the above example, we can see that we are passing the control of
objects to Spring framework, this is called Inversion of Control (IOC) and
Dependency injec on is one of the principles that enforce IOC.

Question 116: What are the different types of Dependency


Injection?
Answer: Spring framework provides 2 ways to inject dependencies:

- By Constructor
- By Se er
method
Constructor-based DI : when the required dependencies are provided as
arguments to the constructor, then it is known as constructor-based
dependency injec on, see the examples below:
Using XML based configura on:
Injec ng a dependency is done through the bean-configura on file, for
this <constructor-arg> xml tag is used:

In case of more than 1 dependency, the order sequence of constructor


arguments should be followed to inject the dependencies.
Java Class A:
Java Class B:

Using Java Based Configura on:


When using Java based configura on, the constructor needs to be
annotated with @Autowired annota on to inject the dependencies,
Our classes A and B will be annotated with @Component (or any other
stereotype annota on), so that they will be managed by Spring.

Before Spring version 4.3, @Autowired annota on was needed for


constructor dependency injec on, however, in newer Spring versions,
@Autowired is op onal, if the class has only one constructor.
But, if the class has mul ple constructors, we need to explicitly add
@Autowired to one of the constructors so that Spring knows which
constructor to use for injec ng the dependencies.

Se er-method injec on: in this, the required dependencies are provided


as the field parameters to the class and the values are set using se er
methods of those proper es. See the examples below.
Using XML based configura on:
Injec ng a dependency is done through the bean configura on file and
<property> xml tag is used where ‘name’ a ribute defines the name of
the field of java class.

Java class A:

Java class B:
Using Java based configura on:
The se er method needs to be annotated with @Autowired annota on.

There is also a Field injec on, where Spring injects the required
dependencies directly into the fields when those fields are annotated
with @Autowired annota on.

Question 117: Difference between Constructor and Setter


injection
Answer: The differences are:

- Par al dependency is not possible with Constructor based


injec on, but it is possible with Se er based injec on. Suppose
there are 4 proper es in a class and the class has se er
methods and a constructor with 4 parameters. In this case, if
you want to inject only one/two property, then it is only
possible with se er methods (unless you can define a new
parametrized constructor with the needed proper es)
- Cyclic dependency is also not possible with Constructor based
injec on. Suppose class A has dependency on class B and class
B has dependency on class A and we are using constructor
based injec on, then when Spring tries to create object of class
A, it sees that it needs class B object, then it tries to resolve
that dependency first. But when it tries to create object of
class B, it finds that it needs class A object, which is s ll under
construc on. Here Spring recognizes that a circular reference
may have occurred and you will get an error in this case. This
problem can easily be solved by using Se er based injec on
because dependencies are not injected at the object crea on
me
- While using Constructor injec on, you will have to remember
the order of parameters in a constructor when the number of
constructor parameters increases. This is not the case with
Se er injec on
- Constructor injec on helps in crea ng immutable objects,
because a bean object is created using constructor and once
the object is created, its dependencies cannot be altered
anymore. Whereas with Se er injec on, it’s possible to inject
dependency a er object crea on which leads to mutable
objects.
Use constructor-based injec on, when you want your class to not even
be instan ated if the class dependencies are not resolved because
Spring container will ensure that all the required dependencies are
passed to the constructor.

Question 118: What is @Autowired annotation?


Answer: @Autowired is a way of telling Spring that auto-wiring is
required. It can be applied to field, constructor and methods.

Question 119: What is the difference between


BeanFactory and ApplicationContext?
Answer: The differences are:
- BeanFactory is the most basic version of IOC containers which
should be preferred when memory consump on is cri cal
whereas Applica onContext extends BeanFactory, so you get
all the benefits of BeanFactory plus some advanced features
for enterprise applica ons
- BeanFactory instan ates beans on-demand i.e. when the
method getBean(beanName) is called, it is also called Lazy
ini alizer whereas Applica onContext instan ates beans at the
me of crea ng the container where bean scope is Singleton,
so it is an Eager ini alizer
- BeanFactory only supports 2 bean scopes, singleton and
prototype whereas Applica onContext supports all bean
scopes
- Applica onContext automa cally registers
BeanFactoryPostProcessor and BeanPostProcessor at startup,
whereas BeanFactory does not register these interfaces
automa cally
- Annota on based dependency injec on is not supported by
BeanFactory whereas Applica onContext supports it
- If you are using plain BeanFactory, features like transac ons
and AOP will not take effect (not without some extra steps),
even if nothing is wrong with the configura on whereas in
Applica onContext, it will work
- Applica onContext provides addi onal features like
MessageSource access (i18n or Interna onaliza on) and Event
Publica on
Use an Applica onContext unless you have a really good reason for not
doing so.

Question 120: Explain the life-cycle of a Spring Bean


Answer: Spring beans are java classes that are managed by Spring
container and the bean life-cycle is also managed by Spring container.
The bean life-cycle has below steps:
- Bean instan ated by container
- Required dependencies of this bean are injected by
container
- Custom Post ini aliza on code to be executed (if required)
- Bean methods are used
- Custom Pre destruc on code to be executed (if required)
When you want to execute some custom code that should be executed
before the bean is in usable state, you can specify an init() method and if
some custom code needs to be executed before the bean is destroyed,
then a destroy() method can be specified.
There are various ways to define these init() and destroy() method for a
bean:
By using xml file,
<bean> tag has 2 a ributes that can be used to specify its init and
destroy methods,

You can give any name to your ini aliza on and destroy methods, and
here is our Test class
By implemen ng Ini alizingBean and DisposableBean interfaces
Ini alizingBean interface has a erProper esSet() method which can be
used to execute some ini aliza on task for a bean and DisposableBean
interface has a destroy() method which can be used to execute some
cleanup task.
Here is our Test class,
And, in the xml file:

By using @PostConstruct and @PreDestroy annota ons


And, in the xml file:

Question 121: What are the different scopes of a Bean?


Answer: Spring framework supports 5 scopes,
singleton – only one bean instance per Spring IOC container
prototype – it produces a new instance each and every me a bean is
requested
request – a single instance will be created and made available during
complete life-cycle of an HTTP request
session – a single instance will be created and made available during
complete life-cycle of an HTTP session
global session – a single instance will be created during the life-cycle of a
ServletContext
@Scope annota on or scope a ribute of bean tag can be used to define
bean scopes in Spring.

Question 122: What is the Default scope of a bean?


Answer: Default scope of a bean is Singleton that means only one
instance per context.

Question 123: What happens when we inject a prototype


scope bean in a singleton scope bean?
Answer: When you define a bean scope to be singleton, that means only
one instance will be created and whenever we request for that bean,
that same instance will be returned by the Spring container, however, a
prototype scoped bean returns a new instance every me it is
requested.
Spring framework gets only one chance to inject the dependencies, so if
you try to inject a prototyped scoped bean inside a singleton scoped
bean, Spring will instan ate the singleton bean and will inject one
instance of prototyped scoped bean. This one instance of prototyped
scoped bean is the only instance that is ever supplied to the singleton
scoped bean.
So here, whenever the singleton bean is requested, you will get the
same instance of prototyped scoped bean.

Question 124: How to inject a prototype scope bean in a


singleton scope bean?
Answer: We have discussed in the previous ques on that when a
prototyped scoped bean is injected in a singleton scoped bean, then on
each request of singleton bean, we will get the same instance of
prototype scoped bean, but there are certain ways where we can get a
new instance of prototyped scoped bean also.
The solu ons are:

- Injec ng an Applica onContext in Singleton bean and then


ge ng the new instance of prototyped scoped bean from this
Applica onContext
- Lookup method injec on using @Lookup
- Using scoped proxy
Injec ng Applica onContext:
To inject the Applica onContext in Singleton bean, we can either use
@Autowired annota on or we can implement Applica onContextAware
interface,

Here, whenever the getPrototypeBean() method is called, it will return a


new instance of PrototypeBean.
But this approach contradicts with Spring IOC (Inversion of Control), as
we are reques ng the dependencies directly from the container.

Lookup Method Injec on using @Lookup:


Here, Spring will dynamically overrides getPrototypeBean() method
annotated with @Lookup and it will look up the bean which is the
return type of this method. Spring uses CGLIB library to do this.

Using Scoped Proxy

Spring uses CGLIB to create the proxy object and the proxy object
delegates method calls to the real object. In the above example, we are
using ScopedProxyMode.TARGET_CLASS which causes an AOP proxy to be
injected at the target injec on point. The default Proxy mode is
ScopedProxyMode.NO .
To avoid CGLIB usage, configure the proxy mode with
ScopedProxyMode.INTERFACES and it will use JDK dynamic proxy.

Question 125: Explain Spring MVC flow


Answer:

In Spring, DispatcherServlet acts as the front Controller. When a request


comes in Spring MVC applica on, below steps get executed,
- the request is first received by the DispatcherServlet
- DispatcherServlet will take the help of HandlerMapping and it
will get to know the specific Controller that is associated with
this request using @RequestMapping’s
- Now, the request gets transferred to its associated Controller,
the Controller will process this request by execu ng
appropriate methods and returns the ModelAndView object
back to the DispatcherServlet
- The DispatcherServlet will transfer this object to ViewResolver
to get the actual view page
- Finally, DispatcherServlet passes the Model object to the View
page which displays the result
Remember, in developing REST services, the Controller’s request
mapping methods are annotated with @ResponseBody annota ons, so
they don’t return a logical view name to DispatcherServlet, instead it
writes the output directly into the HTTP response body.

Question 126: What is the difference between


<context:annotation-config> and <context:component-
scan>?
Answer: The differences are:
<context:annota on-config> is used to ac vate annota ons in beans
that are already registered in the applica on context. So for example, if
you have a class A that is already registered in the context and you have
@Autowired, @Qualifier annota ons in the class A, then
<context:annota on-config> resolves these annota ons.
<context:component-scan> can also do what <context:annota on-
config> does, but component-scan also scans the packages for registering
the beans to applica on context. If you are using, component-scan , then
there is no need to use annota on-config .

Question 127: What is the difference between Spring and


SpringBoot?
Answer: SpringBoot makes it easy to work with Spring framework. When
using Spring framework, we have to take care of all the configura on
ourselves like, for making a web applica on, DispatcherServlet,
ViewResolver etc configura ons are needed. SpringBoot solves this
problem through a combina on of Auto Configura on and Starter
projects.

Question 128: What is auto-configuration in SpringBoot?


Answer: Spring applica ons have a lot of XML or Java Bean
Configura ons. Spring Boot Auto configura on looks at the jars available
on the CLASSPATH and configura on provided by us in the
applica on.proper es file and it tries to auto-configure those classes.
For example, if Spring MVC jar is on the classpath, then
DispatcherServlet will be automa cally configured and if Hibernate jar is
on the classpath, then a DataSource will be configured (Of course, we
will have to provide datasource url, username and password).

Question 129: What are SpringBoot starters?


Answer: Think of SpringBoot starters as a set of related jars that we can
use for our applica on, we don’t have to go out and add the
dependencies one by one and worry about which version will be
compa ble with the spring boot version that you are using, starters take
care of all that.
For example, when you want to make a web applica on, you simply will
add ‘spring-boot-starter-web’ as a dependency and you will have all the
jars that are needed for a web applica on, like, DispatcherServlet,
ViewResolver, Embedded Tomcat etc. Similarly, ‘spring-boot-starter-data-
jpa’ dependency can be used when you want to work with Spring and
JPA.

Question 130: What is @SpringBootApplication


Annotation?
Answer: @SpringBootApplica on is a combina on of 3 different
annota ons:
@Configura on: This annota on marks a class as a Configura on class in
Java-based configura on, it allows to register extra beans in the context
or import addi onal configura on classes
@ComponentScan: to enable component scanning
@EnableAutoConfigura on: to enable Spring Boot’s auto-configura on
feature
These 3 annota ons are frequently used together, so SpringBoot
designers bundled them into one single @SpringBootApplica on, now
instead of 3 annota ons you just need to specify only one annota on
on the Main class. However, if you don’t need one of these annota on
depending on your requirement, then you will have to use them
separately.

Question 131: Where does a Spring Boot application start


from?
Answer: The SpringApplica on class provides a convenient way to
bootstrap a Spring applica on that is started from a main() method. In
many situa ons, you can delegate to the sta c SpringApplica on.run
method, as shown in the following example:

Question 132: How to remove certain classes from getting


auto-configured in SpringBoot?
Answer: @SpringBootApplica on annota on accepts below parameters
that can be used to remove certain classes from taking part in auto-
configura on.
exclude: Exclude the list of classes from the auto-configura on
excludeNames: Exclude the list of class names from the auto-
configura on
Question 133: How to autowire a class which is in a
package other than SpringBoot application class’s package
or any of its sub-packages
Answer: When you specify @ComponentScan annota on on a class,
then it starts scanning for the components from that package and its
sub-packages, so if your class is in a different package altogether, then
you will have to explicitly tell Spring to look into your package,
@ComponentScan annota on has below parameters:
scanBasePackages : Base packages to scan for annotated components
scanBasePackageClasses: Type-safe alterna ve to scanBasePackages() for
specifying the packages to scan for annotated components. The package
of each class specified will be scanned.

Question 134: What is application.properties file in a


SpringBoot application?
Answer: SpringBoot allows us to configure our applica on configura on
using applica on.proper es file. In this file, we can define the
configura ons that will be needed to configure a datasource, like

We can define the logging level of certain packages, using


We can also define the port number that our embedded server will run
on, using
server.port=9000
We can have different applica on.proper es file for different
environments like dev, stage and prod.

Question 135: How to configure the port number of a


SpringBoot application?
Answer: By default, the embedded server starts on port 8080, however
you can change it by defining a property in applica on.proper es file,
like:
server.port=8082

You can also pass it as a vm argument:


java –jar C:\temp\app.jar –server.port=8082
or
java –jar –Dserver.port=8082 C:\temp\app.jar

Question 136: Which jar builds our springboot application


automatically whenever we change some code just like a
node.js application?
Answer: Devtools dependency, just add the below maven dependency in
your pom.xml,
Question 137: What default embedded server is given in
spring boot web starter and how we can change the
default embedded server to the one of our choice
Answer: The default embedded server is Tomcat, that comes with Spring
boot web starter, if you want to change this, then use <exclusion> tag in
web starter and add a separate dependency of the server that you want.

When you add the exclusion tag and save the pom.xml, you will see that
the tomcat dependency will be removed from Maven Dependencies,
then you can add another server’s dependency like the one below:

Question 138: Where should we put our html and


javascript files in a spring boot application?
Answer: If you are adding html and javascript files, then you should put
them in sta c folder in src/main/resources/ . You should also make
separate folders for html, css, javascript files.

Question 139: What are the different stereotype


annotations?
Answer: @Component, @Controller, @Service and @Repository
annota ons are called stereotype annota ons and they are present in
org.springframework.stereotype package.

Question 140: Difference between @Component,


@Controller, @Service, @Repository annotations?
Answer:
@Component: it is a general purpose stereotype annota on which
indicates that the class annotated with it, is a spring managed
component.
@Controller, @Service and @Repository are special types of
@Component, these 3 themselves are annotated with @Component,
see below
So, the classes annotated with these annota ons gets picked up in
Component scanning and they are managed by Spring.
@Controller: the classes annotated with @Controller will act as Spring
MVC controllers. DispatcherServlet looks for @RequestMapping in
classes that are annotated with @Controller. That means you cannot
replace @Controller with @Component, if you just replace it with
@Component then yes it will be managed by Spring but it will not be
able to handle the requests.
(Note: if a class is registered with Spring using @Component, then
@RequestMapping annota ons within class can be picked up, if the
class itself is annotated with @RequestMapping)
@Service: the service layer classes that contain the business logic should
be annotated with @Service. Apart from the fact that it is used to
indicate that the class contains business logic, there is no special
meaning to this annota on, however it is possible that Spring may add
some addi onal feature to @Service in future, so it is always good idea
to follow the conven on.
@Repository: the classes annotated with this annota on defines data
repositories. It is used in DAO layer classes. @Repository has one special
feature that it catches pla orm specific excep ons and re-throw them as
one of the Spring’s unified unchecked excep on i.e. DataAccessExcep on
.

Question 141: Difference between @Controller and


@RestController annotation
Answer: The differences are:
- @Controller annota on is used to mark a class as Spring MVC
controller where the response is a view name which will
display the Model object prepared by controller, whereas
@RestController annota on is a specializa on of @Controller
and it is used in RESTful web services where the response is
usually JSON/XML.
- @RestController is made up of 2 annota ons, @Controller and
@ResponseBody. @ResponseBody annota on is used to a ach
the generated output directly into the body of h p response.
- @Controller can be used with @ResponseBody which will have
same effect as @RestController. @ResponseBody annota on
can be used at the class level or at the individual methods
also. When it is used at the method level, Spring will use HTTP
Message Converters to convert the return value to HTTP
response body (serialize the object to response body).

Question 142: What is @RequestMapping and


@RequestParam annotation?
Answer: @RequestMapping annota on maps the incoming HTTP request
to the handler method that will serve this request, we can apply
@RequestMapping on a class as well as a method. When used on a
class, it maps a specific request path or pa ern onto a controller, then
the method level annota on will make the mappings more specific to
handler methods.
@RequestParam annota on is used to bind a web request parameter to
the parameter of handler method
In the above @RequestParam example, we have defined a value
parameter that just denotes that the incoming request parameter is by
the name ‘id’ which will be mapped to the variable name ‘requestId’,
you can define @RequestParam without this value a ribute also.
By default, all requests are assumed to be of type HTTP GET, however we
can specify the request type by using ‘method’ a ribute of
@RequestMapping annota on, like
Question 143: How to define a Get or Post endpoint?
Answer: You can either use the @RequestMapping and defining its
‘method’ a ribute as RequestMethod.GET, RequestMethod.POST or you
can also use the shortcuts like @GetMapping and @PostMapping
annota ons.

There are similar shortcut annota ons present for PUT, PATCH, DELETE
requests too.

Question 144: Which annotation is used for binding the


incoming json request to the defined pojo class?
Answer: @RequestBody annota on is used to bind the incoming json
request to the pojo class,
Behind the scenes, Spring uses Jackson library (that comes with spring-
boot-starter-web) to map the json request to the pojo class.
MappingJackson2H pMessageConverter is used when the incoming
request is of content-type applica on/json.

Question 145: What is @Qualifier annotation?


Answer: Let’s consider an example to understand @Qualifier annota on
be er. Suppose we have an interface called Shape and there are 2
classes Rectangle and Circle that are implemen ng this interface. We are
autowiring our Shape interface in our controller class using @Autowired,
now here a conflict will happen, because there are 2 beans of the same
type.
When you try to start your applica on, you will get:

Now, to resolve this you can give names to your Rectangle and Circle
class, like:

And you will use @Qualifier annota on to specify which bean should be
autowired, like:
Now, Spring will not get confused as to what bean it has to autowire.
NOTE , you can also use @Qualifier annota on to give names to your
Rectangle and Circle classes, like

Question 146: What is @Transactional annotation?


Answer: Spring provides Declara ve Transac on Management via
@Transac onal annota on. When a method is applied with
@Transac onal, then it will execute inside a database transac on.
@Transac onal annota on can be applied at the class level also, in that
case, all methods of that class will be executed inside a database
transac on.
How @Transac onal works:
When @Transac onal annota on is detected by Spring, then it creates a
proxy object around the actual bean object. So, whenever the method
annotated with @Transac onal is called, the request first comes to the
proxy object and this proxy object invokes the same method on the
target bean. These proxy objects can be supplied with interceptors.
Spring creates a Transac onInterceptor and passes it to the generated
proxy object. So, when the @Transac onal annotated method is called,
it gets called on the proxy object first, which in turn invokes the
Transac onInterceptor that begins a transac on. Then the proxy object
calls the actual method of the target bean. When the method finishes,
the Transac onInterceptor commits/rollbacks the transac on.
One thing to remember here is that the Spring wraps the bean in the
proxy, the bean has no knowledge of it. So, only the external calls go
through the proxy. As for the internal calls (@Transac onal method
calling the same bean method), they are called using ‘this’.
Using @Transac onal annota on, the transac on’s propaga on and
isola on can be set directly, like:

Also, you can specify a ‘rollbackFor’ a ribute and specify which


excep on types must cause a transac on rollback (a transac on with
Run me excep ons and errors are by default rolled back).
If your process() method is calling another bean method, then you can
also annotate that method with @Transac onal and set the propaga on
level to decide whether this method should execute in the same
transac on or it requires a new transac on.

Question 147: What is @ControllerAdvice annotation?


Answer: @ControllerAdvice annota on is used to intercept and handle
the excep ons thrown by the controllers across the applica on, so it is a
global excep on handler. You can also specify @ControllerAdvice for a
specific package,
Or a specific controller,

Or even a specific annota on,

@Excep onHandler annota on is used to handle specific excep ons


thrown by controllers, like,

Here, we have defined a global excep on handler using


@ControllerAdvice. If a SQLExcep on gets thrown from a controller, then
handleSQLExcep on() method will be called. In this method, you can
customize the excep on and send a par cular error page/error code.
Also, custom excep ons can be handled.
If you don’t want to create a global excep on handler, then you can also
define some @Excep onHandler methods in a par cular controller itself.
Question 148: What is @Bean annotation?
Answer: @Bean annota on is used when you want to explicitly declare
and register a bean into applica on context, so that it will be managed
by Spring.
Some points to remember:

- When using @Bean, you have the control over the bean
crea on logic.
- @Bean is a method level annota on, the body of the method
contains the logic for crea ng the bean instance and this
method returns the instance which will be registered in the
spring applica on context.
- Using @Bean, you can register the classes from 3rd party
libraries into the applica on context
- @Bean annota on is usually declared in configura on classes.

Question 149: Difference between @Component and


@Bean
Answer: The differences are:
- @Component auto-detects and configures the beans using
classpath scanning, whereas @Bean explicitly declares a single
bean rather than le ng Spring do it automa cally
- @Component is a class level annota on, whereas @Bean is a
method level annota on
- @Component has different specializa ons called stereotype
annota ons like @Controller, @Service and @Repository,
whereas @Bean has no specializa ons
- @Bean lets you create and configure beans exactly how you
choose it to be, whereas in @Component, Spring has the
control
- @Bean lets you configure classes from 3rd party libraries where
you are not the owner of the source code, but you can’t use
@Component in this case

Question 150: How to do profiling in a SpringBoot


application
Answer: Every applica on has many environments like DEV, STAGE, UAT,
Pre-PROD, PROD. And all these environments have different
configura ons like the database proper es, port number etc. In Spring
boot, we define all proper es in applica on.proper es file. But if we
want to maintain different profiles, then we can have mul ple
applica on.proper es file for each environment and at the run me, we
can specify which profile should be ac ve.
The profiles can be created using the syntax, applica on-
{profile_name}.proper es
For example,
applica on-dev.proper es for dev environment specific configs
applica on-uat.proper es for uat environment specific configs
applica on-prod.proper es for prod environment specific configs
How to ac vate a par cular profile:
There are many ways to ac vate a par cular profile, one of them is by
defining a property in our master applica on.proper es file, like

spring.profiles.active=uat
We can also set a par cular profile to be ac ve by passing it as a VM
argument, Maven se ngs, JVM system parameters, environment
variables. In xml, we use <beans profile=”uat”> to ac vate a profile.
There is one @Profile annota on which when applied to a component
tells that this component will be created only when a par cular profile is
set.

Question 151: What is RestTemplate?


Answer: RestTemplate is used to consume other REST services
programma cally. It can also bind the api response to the custom
domain classes. You must have used/seen the H pURLConnec on to
consume other services, it contains a lot of boilerplate code, like
configuring the HTTP request, execu ng it and then conver ng the HTTP
response to a Java object. But when using RestTemplate, all these things
happen in the background.

In this example, we are consuming a GET web service and conver ng the
response to the object of User class.
Similarly, there are other methods to consume POST, DELETE web
services like exchange() and delete() respec vely.

Question 152: What is Spring Data JPA?


Answer: Spring Data is part of Spring framework. It provides an
abstrac on to significantly reduce the amount of boilerplate code
required to implement data access layers. It does this by providing
Repositories. Repositories are just interfaces that do not have any
implementa on class and provide various database opera ons like save,
delete etc. We have several repositories to choose from,
CRUDRepository, JPARepository, PagingAndSor ngRepository.
If you are thinking how Spring Data JPA saves us from wri ng boilerplate
code, then consider an example, you have an Employee class and you
are wri ng its Dao interface and implementa on class for performing
some CRUD opera ons. You will also have other classes and similarly,
you will write CRUD opera ons logic for these classes as well. So, there is
a lot of boilerplate code here. Spring Data JPA takes care of this, and
provides you with a Repository interface which have all the common
DAO opera ons. We just have to extend these Repositories and Spring
Data JPA will provide the DAO implementa on at run me.
Spring Data JPA can also generate JPA queries on our behalf, we just
have to follow certain method naming conven ons and the database
query will be automa cally generated for us.
For example, let’s say we have a table named Employee with id, name
and age columns. If you want to write a query that will give the
Employee object by name, then you can use Spring Data JPA, like:
public EmployeeEn ty findByName(String name);
Spring Data JPA will transform this method into:
select * from Employee where name=’passed_name_value’;

Question 153: What is the difference between


JPARepository, CRUDRepository,
PagingAndSortingRepository and which one you have
used in your applications?
Answer: JPARepository extends the PagingAndSor ngRepository which in
turn extends the CRUDRepository. So if you are using JPARepository, you
can use all the methods of other two also.
CRUDRepository – this repo mainly provides methods related to CRUD
opera ons
PagingAndSor ngRepository – this repo extends the CRUDRepository
and provides methods to do pagina on and sor ng of records
JPARepository – this repo extends the PagingAndSor ngRepository and
provides JPA related methods like flushing the persistence context and
dele ng records in batches
You can extend any of these repository and make a customized repo to
have methods according to your need.

Question 154: What is Spring AOP?


Answer: Spring AOP (Aspect Oriented Programming) is one of the key
components of Spring framework.
Consider an example to understand the AOP be er, let’s say we have
100 methods and you have to maintain logs in these methods, like what
is the method name and what is the result of each method. One way is
to go in each method and write logger statements. What if this
requirement changes and you don’t need this logger anymore then you
will again go in each method and remove this logger. One thing is clear
from this example that Logging is a cross-cu ng concern.
A cross-cu ng concern is a concern that can affect the en re applica on
and should be centralized in one loca on as much as possible. Few
examples of cross-cu ng concerns are, transac on management,
logging, authen ca on etc.
AOP helps you to refactor the different necessary repea ng codes into
different modules. By doing this, the clu ering of code can be removed
and the focus can be applied on the actual business logic. AOP provides
modularity but here the key unit of modularity is Aspect. Using AOP, you
can add extra func onality before or a er a method execu on.

AOP terminology :
Aspect : Aspect is a class that implements the applica on concerns that
cuts across mul ple classes, such as transac on management and
logging. Aspects can be a normal class configured through Spring XML
configura on or we can use @Aspect annota on.
Join Point : a point during the execu on of a program, such as the
execu on of a method or the handling of an excep on. In Spring AOP, a
join point always represents a method execu on.
Advice : advices are ac ons that are taken for a par cular join point.
There are different types of advices, like, before, a er and around
advice.
Pointcut : pointcut is an expression that is matched with join points to
determine whether advice needs to be applied or not.
Target Object : objects on which advices are applied by one or more
aspects. Since Spring AOP is implemented using run me proxies, this
object will always be a proxied object.
AOP Proxy : an object created by the AOP framework in order to
implement the aspect contracts (advise method execu ons and so on).
In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a
CGLIB proxy.
Weaving : It is the process of linking aspects with other objects to create
the advised proxy objects. This can be done at compile me, load me
or at run me. Spring AOP performs weaving at the run me.

AOP Advice types:


Before advice : these advices run before the execu on of join point
methods. @Before annota on is used to mark a method as before
advice.
A er returning advice : Advice to be executed a er a join point
completes normally: for example, if a method returns without throwing
an excep on. @A erReturning annota on is used to mark a method as
a er returning advice.
A er throwing advice : Advice to be executed if a method exits by
throwing an excep on. @A erThrowing annota on marks a method as
a er throwing advice.
A er (finally) advice : An advice that gets executed a er the join point
method finishes execu ng, whether normally or by throwing an
excep on. @A er annota on is used to create an a er advice.
Around advice : this advice surrounds a join point. Around advice can
perform custom behavior before and a er the method invoca on. It is
also responsible for choosing whether to proceed to the join point or to
shortcut the advised method execu on by returning its own return value
or throwing an excep on. @Around annota on is used create around
advice methods.

Question 155: Have you used Spring Security in your


application
Answer: If you are giving a Spring/SpringBoot interview, then this
ques on will definitely be asked that how did you secure your
applica on APIs? Every applica on uses some type of security to protect
the unwanted access, so if you are working in a project and even if you
have not implemented the security part yourself, then I suggest, you
should try to understand how it is implemented and read more about it.
This will be be er than not answering the ques on. You can always say
you worked on it as a team or you have implemented it in some of your
personal projects

Question 156: What do you know about Spring Batch


framework?
Answer: Spring batch framework can be used when a task requires batch
processing, for example, genera ng financial reports at end of day or
month. Batch processing includes tasks like reading and wri ng to files,
transforming data, reading and wri ng to databases etc. These steps are
o en chained together and can also be executed sequen ally or in
parallel. If an error occurs in a batch job step, then it can be found out
that in which step, the error has occurred and it is also possible to
resume that job execu on from that failed step.

Question 157: Difference between SOAP and REST


Answer: The differences are:

- SOAP stands for Simple Object Access Protocol and REST stands
for Representa onal State Transfer
- SOAP is a protocol whereas REST is an architectural style
- SOAP cannot use REST because it is a protocol whereas REST
can use SOAP web services as the underlying protocol, because
it is just an architectural pa ern
- SOAP uses service interfaces to expose its func onality to client
applica ons whereas REST uses URI to expose its func onality
- SOAP defines standards that needs to be strictly followed for
communica on to happen between client and server whereas
REST does not follow any strict standard
- SOAP requires more bandwidth and resources than REST
because SOAP messages contain a lot of informa on whereas
REST requires less bandwidth than SOAP because REST
messages mostly just contains a simple JSON message
- SOAP only works with XML format whereas REST allows
different data formats like Plain text, HTML, XML, JSON etc.
- SOAP defines its own security whereas REST inherits the
security

Question 158: What is Restful api?


Answer: For an api to be Res ul, it has to sa sfy 6 constraints, they are:
Client-Server : This constraint operates on the concept that the client
and the server should be separate from each other and allowed to
evolve individually.
Stateless : REST is stateless, it means the client request should contain
all the informa on that is needed for a server to respond.
Cacheable : Cache constraint requires that response returned from a
server should be labelled as cacheable or non-cacheable either implicitly
or explicitly. If the response is defined as Cacheable then the client can
re-use the response data for future requests because a stateless API can
increase request overhead by handling large loads of incoming and
outgoing calls.
Uniform Interface : The key to decoupling the client from server is having
a uniform interface that allows independent evolu on of the applica on
without having the applica on’s services, or models and ac ons, ghtly
coupled to the API layer itself.
Layered System : REST APIs have different layers of their architecture
working together to build a hierarchy that helps create a more scalable
and modular applica on.
Code on Demand (Op onal) : this is an Op onal constraint. It allows
servers to temporarily extend the func onality of a client by
downloading and execu ng code in the form of applets or scripts.
Question 159: What is a stateless object?
Answer: An instance of a class without any instance fields is a stateless
object. The class can have fields but they can only be constants, sta c
final .

Question 160: What is the difference between Web Server


and Application Server?
Answer: The differences are:
- Web server provides environment to run only web related
applica ons, whereas Applica on Server provides environment
to run Java J2EE applica ons (enterprise applica ons)
- Web server is used to deliver sta c contents like sta c html
pages, whereas through Applica on server, you can deliver
dynamic content
- Web servers can be used for Servlets, JSPs whereas Applica on
servers can be used for Servlets, JSPs, EJBs, JMS etc.
(Applica on servers have an internal web server inside it to
serve web applica ons)
- Web servers support HTTP protocol, whereas Applica on
servers support HTTP as well as RPC/RMI protocols
- Web server consume less resources than Applica on servers
- Tomcat, Je y are examples of Web Servers, whereas GlassFish,
JBoss, WebLogic, WebSphere are some examples of Applica on
servers

Question 161: What do you know about


CommandLineRunner and ApplicationRunner?
Answer: SpringBoot provides us with two interfaces,
CommandLineRunner and Applica onRunner. Both these runners have a
run() method, which gets executed just a er the applica on context is
created and before SpringBoot applica on startup.
You just have to register these runners as beans in applica on context.
Then Spring will automa cally pick them up.
Both of these interfaces provide the same func onality and the only
difference between them is CommandLineRunner.run() method accepts
String arr[], whereas Applica onRunner.run() method accepts
Applica onArguments as argument.
Mul ple runners can be defined and we can order them as well, either
by extending the interface org.springframework.core.Ordered or via the
@Order annota on.
Knowing these topics well should be enough to crack a
Spring/SpringBoot interview, if you want to really impress the
interviewer, you can prepare the below topics as well:
- Spring Cloud, how different environment proper es are stored
in Git
- Eureka Naming Server
- Zuul
- Zipkin
- Hysterix

All the above topics are advanced but I will explain about them a li le,
so you can go out and read more about it.

Question 162: What do you know about Eureka Naming


Server?
Answer:
To know about the need of Eureka Naming Server and what it does, let’s
consider an example. Suppose you have 5 micro-services which are
experiencing heavy traffic and you want to deploy mul ple instances of
these 5 micro-services and use a load balancer to distribute the traffic
among these instances. Now, when you create new instances of your
micro-service, you have to configure these in your load balancer, so that
load balancer can distribute traffic properly. Now, when your network
traffic will reduce then you will most likely want to remove some
instances of your micro-service, means you will have to remove the
configura on from your load balancer. I think, you see the problem here.
This manual work that you are doing can be avoided by using Eureka
naming server. Whenever a new service instance is being
created/deleted, it will first register/de-register itself to the Eureka
naming server. Then you can simply configure a Ribbon client (Load
Balancer) which will talk with Eureka Naming server to know about the
currently running instances of your service and Ribbon will properly
distribute the load between them. Also, if one service, serviceA wants to
talk with another service, serviceB then also Eureka Naming server will
be used to know about the currently running instances of serviceB.
You can configure Eureka Naming Server and Ribbon client in SpringBoot
very easily.

Question 163: What do you know about Zuul?


Answer:
Zuul is an API gateway server. It handles all the requests that are coming
to your applica on. As it handles all the requests, you can implement
some common func onali es of your micro-services as part of Zuul
server like Security, Monitoring etc. You can monitor the incoming traffic
to gain some insights and also provide authen ca on at a single place
rather than repea ng it in your services. Using Zuul, you can dynamically
route the incoming requests to the respec ve micro-services. So, the
client doesn’t have to know about the internal architecture of all the
services, it will only call the Zuul server and Zuul will internally route the
request.

Question 164: What do you know about Zipkin?


Answer:
To understand Zipkin use-case, let’s consider an example. Suppose you
have a chain of 50 micro-services where first micro-service is calling the
second and second calling third and so on. Now, if there is an error in,
say 35th micro-service, then how you will be able to iden fy your request
that you made to the first micro-service, from all the logs that gets
generated in all 35 micro-services. I know this is an extreme example
Zipkin helps in distributed tracing, especially in a micro-service
architecture. It assigns an ‘id’ to each request and gives you a
dashboard, where you can see the complete request and a lot more
details, like the en re call-chain, how much me one micro-service took
and which service failed etc.
Question 165: What do you know about Hysterix?
Answer:
Hysterix is a library that makes our micro-service, fault-tolerant.
Suppose, you have a chain of 10 micro-services calling each other and
the 6th one fails for some reason, then your applica on will stop working
un l the failed micro-service is fixed.
You can use Hysterix here and provide a fallback method in case of a
service failure.

If the GET service is ge ng failed, then the fallback method will be


executed.
JPA / Hibernate

Question 166: What is JPA?


Answer: JPA stands for Java Persistence API. It is a specifica on which
gives a standard API for accessing databases within java applica ons. As
JPA is just a specifica on, it does not perform any opera on by itself. It
requires an implementa on, there are many JPA implementa ons
available like Hibernate, iBa s, TopLink, EclipseLink etc. Hibernate ORM
is the most popular implementa on of JPA.

Question 167: What is Hibernate?


Answer: Hibernate is an Object Rela onal Mapping tool (ORM tool), that
maps the Java objects to the database tables and vice-versa.
Some points to remember:

- Hibernate framework provides the facility to create database


tables automa cally
- Hibernate framework provides us object-oriented version of
SQL known as HQL (Hibernate Query Language). It generates
the database independent queries. So, even if our database
gets changed, we don’t have to change our SQL queries
according to the new database
- Using Hibernate, we can define rela onships between our
En es (tables), that makes it easy to fetch data from mul ple
tables
- Hibernate supports Caching, that improves the performance of
our applica on
- Using Hibernate, we can generate the Primary key of our tables
automa cally

Question 168: Difference between JPA and Hibernate


Answer: JPA is just a specifica on i.e. it defines a set of concepts that can
be implemented by any tool or framework, and Hibernate is one of the
implementa on of JPA.

Question 169: What is @Entity?


Answer: @En ty annota on defines that a class can be mapped to a
database table. The class fields will be mapped to the columns of the
table.

Question 170: How to give a name to a table in JPA?


Answer: @Table annota on can be used to give name to a table

Question 171: What is @Id, @GeneratedValue?


Answer: @Id annota on defines the primary key of a table and
@GeneratedValue annota on is used to specify the primary key
genera on strategy to use. If the strategy is not specified, the default
strategy AUTO will be used.

Question 172: How to use a custom database sequence in


Hibernate to generate primary key values?
Answer: JPA specifica on provides a set of annota ons to define the
primary key genera on strategy. To use a custom sequence, we have to
set the Genera onType to SEQUENCE in @GeneratedValue annota on,
this tells Hibernate to use a database sequence to generate the primary
key value. If we don’t provide any other informa on, Hibernate will use
its default sequence.
To define the name and schema of the database sequence, there is an
annota on @SequenceGenerator.
See the code snippet for doing all this:

If you persist a new en ty, then Hibernate will use ‘user_seq’.

Question 173: How to give names to the columns of a JPA


Entity
Answer: @Column annota on can be used to give names to a column

Question 174: How to define a @OneToMany relationship


between entities
Answer: Here the interviewer will ask you about the syntax, so to define
a One to Many rela onship, consider a class Cart that can have mul ple
items represented by class Item . So, one Cart -> many Items.
Cart En ty:
In mappedBy a ribute, the field name of Item en ty is passed, see
below,
Item En ty:

@JoinColumn annota on is used to define the name of foreign key


column that represents the en ty associa on.

Question 175: Why annotations should be imported from


JPA and not from Hibernate?
Answer: If you see @En ty annota on, it is present in javax.persistence
package and also in org.hibernate.annota ons package:
You should choose the one from javax.persistence package, because if
you choose the Hibernate one, then in future, if by any chance, you have
to remove Hibernate and use some other JPA implementa on, like iBa s,
then you will have to change your code (imports). As you remember, JPA
is just a specifica on, like an interface. You can plug any of its
implementa ons as long as you use the imports from javax.persistence
package.

Question 176: What is the difference between get() and


load() method of Hibernate Session?
Answer: Hibernate Session class provides two methods to access object,
session.get() and session.load()
The differences are:

- get() method involves a database hit, if the object does not


exist in Session cache and it returns a fully ini alized object
which may involve several database calls, whereas load()
method returns a proxy object and it only hit the database if
any method other than getId() is called on the en ty object
- load() method results in slightly be er performance as it can
return a proxy object, it will only hit the database when a non-
iden fier ge er method is called, whereas get() method
returns a fully ini alized object when it does not exist in
Session cache which may involve mul ple database calls based
on en ty rela onships
- get() method returns null if the object is not found in the cache
as well as the database whereas load() method will throw
ObjectNotFoundExcep on but never return null
- If you are not sure whether the object exists or not, then use
get() as it will return null but if you are sure that the object
exists, then use load() method as it is lazily ini alized
Question 177: What is the difference between save(),
saveOrUpdate() and persist() method of Hibernate
Session?
Answer: Hibernate Session class provides various methods to save an
object into the database, like save(), saveOrUpdate() and persist()
The difference between save() and saveOrUpdate() method is that save()
method saves the record into the database by INSERT query, generates a
new iden fier and returns the Serializable iden fier back, while
saveOrUpdate() method either INSERT the record or UPDATE the record
if it already exists, so it involves extra processing to find whether the
record already exists in the table or not.
Similar to save(), persist() method is also used to save the record into
the database table.
The differences between save() and persist() are:

- Return type of persist() method is void while return type of


save() method is Serializable object
- Both persist() and save() methods makes a transient instance
persistent. But persist() method does not guarantee that the
iden fier value will be assigned to the persistent instance
immediately, the assignment might happen at flush me
- Both behave differently when they are executed outside the
transac on boundaries. persist() method ensures that it will
not execute an INSERT when it is called outside of a transac on
boundary whereas save() method does not guarantee this, it
returns an iden fier and if an INSERT query has to be executed
to get the iden fier then this INSERT happens immediately and
it does not ma er if the save() is called inside or outside of a
transac on
- persist() method is useful in long-running conversa on with an
extended Session context because it does not execute an
INSERT outside of a transac on. On the other hand, save()
method is not good in a long-running conversa on with an
extended Session context
Question 178: What is Session and SessionFactory in
Hibernate?
Answer: SessionFactory creates and manages the Session objects.
Some points about SessionFactory:

- it is one instance per datasource/database


- it is thread-safe
- it is an immutable and heavy-weight object as it maintains
Sessions, mappings, hibernate configura ons etc.
- SessionFactory provides second level cache in hibernate also
called applica on-level cache
Some points about Session:

- Session objects are created using


sessionFactory.openSession()
- It is one instance per client/thread/transac on
- It is not thread-safe
- It is light-weight
- Session provides first level cache, which is short-lived

Question 179: What is First Level and Second Level Cache


in Hibernate?
Answer: Hibernate framework provides caching at two levels, first-level
cache which is at the Session level and second-level cache which is at the
applica on level.
The first level cache minimizes the database access for the same object if
it is requested from the same Session. The first level cache is by default
enabled. When you call session.get() method then it hits the database,
and while returning, it also saves this object in the first-level cache. So,
the subsequent requests for this same object from the same session will
not hit the database and the object from cache will be used.
But, since this cache is associated with the Session object, which is a
short-lived object in Hibernate, as soon as the session is closed, all the
informa on held in the cache is also lost. So, if we try to load the same
object using the get() method, Hibernate will go to the database again
and fetch the record.
This poses a significant performance challenge in an applica on where
mul ple sessions are used, Hibernate provides second-level cache for
this and it can be shared among mul ple sessions.
The second level cache is maintained at the SessionFactory level, this
cache is by default disabled, to enable second level cache in hibernate,
it needs to be configured in hibernate configura on file, i.e.
hibernate.cfg.xml file. There are various providers of second level cache,
like EhCache, OSCache etc.
Once second level cache is configured, then object request will first go to
the first-level cache, if it is not found there, then it will look for this
object in second-level cache, if found then it will be returned from the
second-level cache and it will also save a copy in first-level cache.
But, If the object is not found in the second-level cache also, then
it will hit the database and if it present in database, this object will be
put into both first and second level cache, so that if any other session
requests for this object then it will be returned from the cache.

Question 180: What is session.flush() method in


Hibernate?
Answer: Flushing the session forces Hibernate to synchronize the in-
memory state of the Session with the database. By default, Hibernate
will flush changes automa cally for you:

- before some query execu ons


- when a transac on is commi ed
- when session.flush is called
explicitly
It is also possible to define a flushing strategy, by using FlushMode class.
It provides below modes:

- ALWAYS: the session is flushed before every query


- AUTO: the Session is some mes flushed before query execu on
in order to ensure that queries never return stale state
- COMMIT: the Session is flushed when Transac on.commit() is
called
- MANUAL: the Session is only ever flushed when Session.flush()
is explicitly called by the applica on

Question 181: How can we see the SQL query that gets
generated by Hibernate?
Answer: If you are using hibernate.cfg.xml file, then use the below
property:

< property name ="show_sql" > true</ property


>
If you are using Spring Data JPA, then you can set this property in
applica on.proper es file, like:
spring.jpa.show-sql=true

Question 182: What is Hibernate Dialect and why we need


to configure it?
Answer: The Dialect specifies the type of database that our applica on is
using. As we know, Hibernate is database agnos c and it can work with
many databases. However, each database has some varia ons and
standard implementa ons. We need to tell Hibernate about it, so that
Hibernate can generate the database specific SQL wherever it is
necessary.
You can configure this dialect in hibernate.cfg.xml file, like:
< property name ="dialect" >
org.hibernate.dialect.PostgreSQLDialect</ property >
And in SpringBoot, you can configure it in applica on.proper es file,
like:
spring.jpa.database-
platform=org.hibernate.dialect.MySQL5InnoDBDialect

Question 183: What do you know about


hibernate.hbm2ddl.auto property in Hibernate?
Answer: hibernate.hbm2ddl.auto automa cally validates or exports
schema DDL to the database when the SessionFactory is created.
This property takes various values:

- create: creates the schema, but previous data will be lost


- validate: validates the schema
- update: updates the schema. This does not drop any exis ng
table, so we don’t lose any exis ng data. If any column is
added in hibernate en ty, it will add this column in database
table or if any new en ty has been added, it will create a new
table in database
- create-drop: when this value is given, then the schema is first
created at SessionFactory startup and gets dropped at
SessionFactory shutdown
- none: it does nothing with the Schema, makes no changes to
the database
You can configure this property in hibernate.cfg.xml file, like:
< property name ="hbm2ddl.auto" > create</
property >
And in SpringBoot, you can configure it in applica on.proper es file,
like:

spring.jpa.hibernate.ddl-auto=update
Maven
Question 184: What is Maven?
Answer: Maven is a tool that is used for building and managing any Java
based project. It is a powerful project management tool that is based on
POM (Project Object Model). It simplifies the build process.

Question 185: What is pom.xml?


Answer: POM stands for Project Object Model, it is an xml file which
contains the configura on informa on related to the project. Maven
uses this file to build the project. We specify all the dependencies that
are needed for a project, the plugins, goals etc. By using <packaging>
tag, we can specify whether we need to build the project into a
JAR/WAR etc.

Question 186: What is local repo and central repo?


Answer: Local repo : Local repository is a directory on the developer
machine. This repository contains all the dependencies that are
downloaded by Maven. The dependencies are downloaded only once
even if it is used in mul ple projects. By default, local repository loca on
is, C:/Users/USER_NAME/.m2
Central Repo : if any dependency that is required by a project is not
found in local repository then Maven looks in central repository for this
dependency, then Maven downloads this dependency into the local
repository.
We also have one Remote repository , which resides on a server from
which Maven can download the dependencies into the local repository.
It is mainly used in organiza ons, to share the dependencies within the
organiza on teams.

Question 187: Where we define our local repo path?


Answer: se ngs.xml file is used to define a local repository loca on.
This file is also used to define proxies, remote repository server
loca ons, plugin groups, profiles etc. By default, it is present in
~/.m2/se ngs.xml

Question 188: Where do we define proxies so that maven


can download jars from the internet in a corporate
environment?
Answer: se ngs.xml file is used to define proxies which helps in
connec ng to a network while working in a corporate environment.

Question 189: Explain Maven build life-cycle


Answer: Maven build life-cycle is made up of below phases,
- validate: validate the project is correct and all necessary
informa on is available
- compile: compile the source code of the project
- test: test the compiled source code using a suitable unit tes ng
framework. These tests should not require the code to be
packaged or deployed
- package: take the compiled code and package it in its
distributable format, such as a JAR
- verify: run any checks on results of integra on tests to ensure
quality criteria’s are met
- install: install the package into the local repository, for using as
a dependency in other projects locally
- deploy: done in the build environment, copies the final
package to the remote repository for sharing with other
developers and projects
Maven will first validate the project, then it will try to compile the
sources, run the tests against the compiled code, package the binaries
(e.g. jar), run integra on tests against that package, verify the
integra on tests, install the verified package to the local repository and
then deploy the installed package to a remote repository.
mvn command can be used to execute these build life-cycle phases. If
you run mvn verify, then it will execute all the phases in order, validate,
compile, test, package before calling the verify . We only need to call the
last build phase.
mvn clean command is used to delete all the project jars that are built
by Maven (/target directory of a project). Generally, this clean command
is used with install/deploy phase, like mvn clean deploy to cleanly build
and deploy ar facts into the shared repository.
Database
Question 190: What do you know about SQL Joins?
Answer: SQL joins are used to combine rows from two or more tables,
based on a related column between them.
There are 4 types of Joins in SQL:

1. Inner join : Inner join selects all records from Table A and
Table B, where the join condi on is met.

Syntax:
SELECT Table1.column1, Table1.column2, Table2.column1, …..
FROM Table1
INNER JOIN Table2
On Table1.MatchingColumnName = Table2.MatchColumnName;
(Note: Use either INNER JOIN or JOIN for this opera on)
2. Le Join : Le join selects all records from Table A along with
records of Table B for which the join condi on is met.

Syntax:
SELECT Table1.column1, Table1.column2, Table2.column1, …..
FROM Table1
LEFT JOIN Table2
On Table1.MatchingColumnName = Table2.MatchColumnName;
3. Right Join : Right join selects all records from Table B along
with records of Table A for which the join condi on is met.

Syntax:
SELECT Table1.column1, Table1.column2, Table2.column1, …..
FROM Table1
RIGHT JOIN Table2
On Table1.MatchingColumnName = Table2.MatchColumnName;
4. Full Join : Full join selects all records from Table A and Table
B, regardless of whether the join condi on is met or not.
Syntax:
SELECT Table1.column1, Table1.column2, Table2.column1, …..
FROM Table1
FULL JOIN Table2
On Table1.MatchingColumnName = Table2.MatchColumnName;

Question 191: Difference between TRUNCATE & DELETE


statements
Answer: The differences are:

- TRUNCATE is a DDL command, whereas DELETE is a DML


command
- TRUNCATE removes all rows from a table, whereas DELETE can
also remove all rows but it can be used with a ‘WHERE’ clause
to remove specific rows from a table
- TRUNCATE command records very li le entry in the transac on
log, whereas DELETE statement removes rows one at a me
and records an entry in the transac on log for each deleted
row, because of this reason TRUNCATE is faster than DELETE
- To use TRUNCATE on a table, you need at least ALTER
permission on the table, whereas to use DELETE, you need
DELETE permission on the table
- TRUNCATE uses less transac on space than DELETE
- TRUNCATE opera on cannot be rolled back, whereas DELETE
opera on can be rolled back
TRUNCATE command Syntax:
TRUNCATE TABLE employee;

DELETE command Syntax:


DELETE FROM employee; -- delete all rows of the table
DELETE FROM employee WHERE name = ‘Mark’; -- delete record
where emp_name is Mark
Question 192: Difference between Function and Stored
Procedure
Answer: The differences are:

- Func on must return a value, whereas Stored Procedure can


return zero or n values
- A Func on can have only input parameters, whereas a Stored
Procedure can have both input and output parameters
- Func ons can be called from a Stored Procedure, but a Stored
Procedure cannot be called from a Func on
- In a Func on, DML statements cannot be used, whereas DML
statements can be used in a Stored Procedure
- Func ons does not allow the usage of try-catch blocks,
whereas in Stored Procedure, try-catch block can be used for
excep on handling
- Transac ons are not allowed within Func ons, whereas
transac ons can be used within Stored Procedure
- In a Func on, we can only use the table variables, it does not
allow using the temporary tables, whereas in a Stored
Procedure, both table variables and temporary tables can be
used
- Func ons can be used in SELECT statement, WHERE clause,
HAVING clause, whereas Stored Procedure cannot be used with
these
- A Func on can be used in JOIN clause as a result set, whereas a
Stored Procedure cannot be used in JOIN clause

Question 193: What is DDL, DML statements?


Answer: DDL: DDL stands for Data Defini on Language. These statements
are used to define the database structure or schema. DDL commands are
auto-commi ed.
Examples of DDL commands are: CREATE, ALTER, DROP, TRUNCATE
DML: DML stands for Data Manipula on language. These statements
allows us to manage the data stored in the database. DML commands
are not auto-commi ed, so they can be rolled back.
Examples of DML commands are: INSERT, UPDATE, DELETE, SELECT

Question 194: How to find the nth highest salary from


Employee table
Answer: The query to find nth highest salary is:
SELECT name, salary
FROM Employee e1
WHERE N-1 = (SELECT COUNT(DISTINCT salary) FROM Employee e2
WHERE e2.salary > e1.salary);
Here, to find the 3rd highest salary, replace N with 3, for 5th highest
salary, replace N with 5 and so on.
The DISTINCT keyword is used to deal with the duplicate salaries in the
table. The highest salary means no salary is higher than it, the second
highest salary means only one salary is higher than it, and similarly Nth
highest salary means N-1 salaries are higher than it. This is a generic
solu on and works in all databases, however it is a li le slow because
the inner query will run for every row processed by the outer query.

Question 195: Difference between UNION and UNION ALL


commands in SQL
Answer: Both UNION and UNION ALL are used to combine results of two
separate queries, it could be on a same table or a different table but
number of columns should be same in both queries.
The Key difference between them is UNION removes duplicates, whereas
UNION ALL keeps the duplicates. Because of this, UNION ALL takes less
me, as there is no extra step of removing duplicate rows.
Question 196: Difference between Unique Key and
Primary Key in SQL
Answer: Both Unique and Primary keys uniquely iden fies each row of a
table.
The differences between them are:

- There can be only one primary key in a table, whereas there


can be mul ple unique keys in the table
- Primary key cannot be null, whereas Unique Keys can be null
- In Primary key, default index is clustered, whereas in Unique
key, default index is non-clustered

Question 197: What is the difference between Primary and


Foreign key in SQL?
Answer: Primary key is used to uniquely iden fy a row in the table. A
table can have only one primary key. Primary key is of two types, simple
and composite primary key. A Simple Primary key is made up of just one
column, whereas a composite primary key is made up of more than one
column.
Primary key also enforces some constraints, like UNIQUE and NOT NULL,
which means a table cannot have duplicate primary keys and the key
cannot be null.
A Foreign key in a table is the primary key of another table. For example,
consider 2 tables, Employee & Department. Department table have a
primary key dept_id and this primary key can be used as foreign key in
Employee table to iden fy that this employee belongs to this
department.
The differences between Primary key and Foreign key are given below:
- Primary key uniquely iden fy a record in the table, whereas
Foreign key is the field in the table that is the primary key of
another table
- By default, a clustered index is created on primary key,
whereas foreign key do not automa cally create an index
- We can have only one primary key in a table, whereas we can
have more than one foreign key in a table
- Primary keys does not allow duplicate or Null values, whereas
Foreign keys allows both

Question 198: What is the difference between clustered


and non-clustered index?
Answer: Indexes are used to speed-up the data retrieval performance.
There are 2 types of indexes, clustered and non-clustered index and the
difference between them,
- A clustered index defines the order in which data is physically
sorted in a table, whereas non-clustered index does not sort
the physical data inside the table
- By default, clustered index is automa cally created on primary
key, whereas non-clustered index can be created on any key
- There can be only one clustered index in a table, whereas
there can be any number of non-clustered index in a table
- Data Retrieval is faster using Clustered index than non-
clustered index
- Data Update is faster using Non-clustered index than clustered
index
- Clustered index does not need any extra space, whereas non-
clustered index requires extra space to store the index
separately
- The size of clustered index is quite large as compared to non-
clustered index
Syntax of crea ng a custom clustered index:
CREATE CLUSTERED INDEX index_name
ON table_name (column_name ASC);
For example, crea ng a clustered index on Employee table, where data
should be stored in ascending order of age column:
CREATE CLUSTERED INDEX employee_asc_age_index
ON employee(age ASC);

Syntax of crea ng a custom non-clustered index:


CREATE NONCLUSTERED INDEX index_name
ON table_name (column_name ASC);
For example, crea ng a non-clustered index on Employee table, where
data should be stored in ascending order of name column:
CREATE NONCLUSTERED INDEX employee_asc_name_index
ON employee(name ASC);

Question 199: What is the difference between WHERE and


HAVING clause in SQL
Answer: The differences are:
- WHERE clause can be used with SELECT, INSERT, UDPATE and
DELETE statements, whereas HAVING clause can only be used
with SELECT statement
- WHERE clause is used for filtering the rows and it applies on
each and every row, whereas HAVING clause is used to filter
groups
- WHERE clause is used before GROUP BY clause, whereas
HAVING clause is used a er GROUP BY clause. It means that
WHERE clause is processed before GROUP BY clause while
HAVING clause is executed a er groups are created
- Aggregate func ons cannot be used in WHERE clause, whereas
we can use aggregate func ons in HAVING clause

Question 200: How to change the gender column value


from Male to Female and Female to Male using single
Update statement
Answer: This is also a very common interview ques on. Mostly, this
ques on is asked in a telephonic round. The ques on is like, you are
given a table, say Employee which has a column named ‘Gender’ having
only “Male” or “Female” strings as values. You have to swap these values
like wherever the value is Male, it should become Female, and wherever
the value is Female, it should become Male. And you have to do this by
wri ng only one Update statement.
The Update query for this is:
UPDATE Employee SET Gender =
CASE Gender WHEN ‘Male’ THEN ‘Female’ WHEN ‘Female’ THEN ‘Male’
ELSE Gender END;

Other than these common ques ons, you can be asked to write a lot of
queries which mostly contains Joins, so you should also prepare for
those types of database queries.
Question 201: Find first 3 largest numbers in an array
In this ques on, the interviewer will most probably ask you to not use
sor ng and pick the first/last 3 numbers from the array. Instead he will
ask you to use one “for loop” to solve this problem.

Output:

Three largest elements are: 78, 33, 20


Here, the idea is to have 3 numbers and then itera ng through the array
and finding where the current element of array fits in.
At first, we check whether the current element is greater than first, if
true, assign the current element to first number by swapping the values
of first, second and third.
When the first condi on is not true, then we compare the current
element with second largest number to find whether the current
number is second largest or not, same goes for third condi on.

Question 202: Move all negative numbers at the


beginning of an array and all positive numbers at the end
Here, also the interviewer will ask not to use any addi onal data
structure like an extra array and this ques on can be asked in two ways,
whether the sequence of original array elements should be maintained
or not, so let’s see the program where the sequence is not maintained:
Output:

-9 -2 -15 -3 1 7 12 5 2
Here, the idea is to iterate through the array and when a nega ve
number is found, then bring that number to the beginning of the array
by swapping it with the first posi ve number.
As you can see that the output is not maintaining the original sequence
of array elements.
Now, let’s take a look at the solu on which maintains the element
sequence:
Output:
-9 -2 -15 -3 5 1 7 12 2

Now, the output is maintaining the original sequence of array elements.

There are many programma c, puzzle problems that the interviewer can
ask. If you are a beginner, then prepare for programs like Palindrome,
Fibonacci, Array problems, String problems, Linked list programs etc.
First, try to solve them with whatever brute-force solu on that comes to
your mind, then try to find its me and space complexity, then try to
op mize it. If you are not able to think of a be er solu on, no problem,
look for the op mal solu on on the internet.
About the Author
Ja n Arora is a Computer Science graduate. He holds an exper se in Java
& SpringBoot. He has worked on a variety of interes ng projects across
different domains like Inventory management, DevOps, cloud & financial
domain.

You might also like