Guide To Clear Java Developer Interview Free Sample
Guide To Clear Java Developer Interview Free Sample
Guide To Clear Java Developer Interview Free Sample
Introduction .......................................................................................................................... 3
How to Prepare for a Java Developer Interview ..................................................... 4
How to introduce yourself ............................................................................................... 6
Tell me about yourself, tell me about skills? ....................................................... 6
Tell me about your project & architecture please explain it? Draw the
architecture, framework and technology used?.................................................. 6
Best practices to follow while developing an application? .............................. 7
What challenging things you have done it of spring and hibernate and
database?........................................................................................................................... 8
Explain the code and flow of the project in and out of it? .............................. 8
How do you build your code, how to do packaging? ........................................ 9
Object Oriented Programming ..................................................................................... 11
Tell me about OOPS concept in java? ................................................................... 11
Four principle of OOP? Abstraction, encapsulation, inheritance
polymorphism? .............................................................................................................. 11
Inheritance vs composition? ..................................................................................... 11
Abstraction vs polymorphism? ................................................................................ 12
Abstract class vs interface? ...................................................................................... 12
Abstract class with constructor, what is the use of constructor if we
don't instantiate abstract class? ............................................................................. 13
How to check if we can see the current code about aggregation
composition and inheritance, what is the practical use of it? ..................... 14
What are Composition and Aggregation with examples?.............................. 14
Explain Multilevel inheritance? ................................................................................ 15
What is abstraction, how it is different from encapsulation? ...................... 16
When to you use encapsulation and abstraction in your project?............. 17
How to achieve encapsulation? ............................................................................... 18
What is polymorphism and how can we achieve it? ....................................... 19
What is Method Overloading/Overriding? ........................................................... 20
Method overloading and overriding? ..................................................................... 21
Method overriding with access specifiers with example? .............................. 23
Methods Overriding with exception handling with example? ...................... 24
Can we have overloaded methods with different return type?................... 26
Can we override the static method? Why Can't we do that? ...................... 26
AJAY RATHOD 1
What is SOLID principles with example? ............................................................. 27
Cohesion vs Coupling, Explain that? ..................................................................... 28
What is the use of static keywords in java? ....................................................... 28
What is the Covariant type? ..................................................................................... 29
What exactly is abstraction what are the advantage of it? Concrete class
doing the same what is the advantage over concrete class? ...................... 30
What is default method its use? ............................................................................. 31
Can interface have no method in it? ..................................................................... 32
Exception rules for overriding? ................................................................................ 32
What is Static keyword?............................................................................................. 34
Difference between static variable and instance variables? ........................ 36
How to make class immutable? .............................................................................. 36
Different types of access modifiers? ..................................................................... 37
What is the use of protected members?.............................................................. 39
AJAY RATHOD 2
Introduction
Welcome to the Ultimate Guide to Mastering Java Developer Interviews!
The book also dives into the intricate world of Multithreading, an area that
many interviews focus on to assess your proficiency in concurrent
programming.
After you've explored the depths of this guide, I am confident that you'll
walk into your interview room with newfound confidence and expertise.
The knowledge you'll gain from these pages will undoubtedly set you
apart from the competition.
Best Regards,
Ajay Rathod
AJAY RATHOD 3
How to Prepare for a Java
Developer Interview
I will be sharing my preparation strategy for techies who want to hunt for
their next tech job. Over the course of three months, I extensively
prepared for a Java developer role. This preparation helped me clear
many interviews, including those with BIG FOUR companies. Developers
with 0–10 years of experience can apply this strategy.
Manager Round
HR round
Usually, if you can clear the technical rounds, you are well on your way to
receiving an offer letter, as the manager and HR rounds primarily involve
discussions. Hence, our focus should be on preparing for the technical
rounds.
AJAY RATHOD 4
model employed. Many developers are unaware of the deployment
process, so this is a crucial aspect to understand.
Garbage Collection
Java Generics
AJAY RATHOD 5
How to introduce yourself
Tell me about yourself, tell me about skills?
"Tell me about yourself" and "Tell me about your skills" are common
interview questions that can be challenging to answer. Here are some tips
on how to tackle these questions:
Be concise: Keep your answers brief and to the point. Don't ramble on or
share irrelevant information. Stick to the main points and be clear and
concise.
Highlight your skills: When asked about your skills, provide specific
examples of how you have used them in the past to achieve success. Talk
about your strengths and how they will benefit the company. Be sure to
include both technical and soft skills, such as problem-solving,
communication, and teamwork.
Start with an overview: Begin by giving a brief overview of the project and
the business problem it was designed to solve. This will help provide
context for the architecture you will describe.
AJAY RATHOD 6
Discuss the architecture: Provide an explanation of the underlying
architecture that was used in the project. This should include a high-level
overview of the components and how they interact with each other. You
can also discuss the rationale for choosing this architecture and any trade-
offs that were made.
Describe the design decisions: Talk about the design decisions that
were made during the project. This could include how the architecture was
designed to meet specific performance requirements, how the system was
designed to be scalable, or how it was designed to be maintainable.
Highlight your role: Be sure to discuss your role in the project and how
you contributed to the architecture design and implementation. This could
include any specific tasks you performed or any technical challenges you
helped overcome.
Use visual aids: If possible, use diagrams or other visual aids to help
illustrate the architecture and design decisions. This can help the
interviewer better understand your explanation and provide a more
comprehensive answer.
Plan and prioritize: Before starting development, make sure to plan the
project thoroughly and prioritize tasks based on their importance and
urgency.
Use version control: Use a version control system, such as Git, to manage
and track changes to the codebase.
AJAY RATHOD 7
Test early and often: Test the software early and often to catch bugs and
errors before they become more difficult to fix.
Write clean and modular code: Write clean, modular, and maintainable
code to make it easier to maintain and extend the software over time.
Explain the code and flow of the project in and out of it?
When asked to explain the code and flow of a project, it's important to
provide a clear and concise overview of the project and how it works. Here
are some tips to help you answer this question:
Start with an overview: Begin by giving a brief overview of the project and
its purpose. This will help provide context for the code and flow you will
describe.
AJAY RATHOD 8
Describe the code flow: Describe how the code flows through the different
components of the project. This should include an explanation of the
different modules, functions, and classes that make up the codebase.
Explain the logic: Explain the logic behind the code and how it implements
the functionality of the project. This should include an explanation of the
algorithms and data structures used in the code.
Use visual aids: If possible, use diagrams or other visual aids to help
illustrate the code flow and architecture. This can help the listener better
understand your explanation and provide a more comprehensive answer.
Provide examples: Provide specific examples of how the code and flow
work in different scenarios. This can help illustrate the functionality of the
project and provide a more concrete understanding of how it works.
Build process: Start by explaining the process you follow to build your
code. This should include a high-level overview of the tools and
technologies you use to compile and build the codebase. For example, you
might use Maven, Gradle, or Ant to build your code.
Testing: Explain how you ensure the quality of your code through testing.
This should include an explanation of the different types of tests you use,
such as unit tests, integration tests, and acceptance tests, and how you
automate and run these tests.
Packaging: Describe how you package your code for deployment. This
should include an explanation of how you package the code into a
deployable artifact, such as a JAR or WAR file. You should also explain any
AJAY RATHOD 9
steps you take to optimize the package size and reduce any unnecessary
dependencies.
Version Control: Explain how you use version control systems like Git to
manage and maintain the codebase, and how you ensure that the code is
always in a deployable state.
Documentation: Explain how you document the code and the build
process, and how you ensure that the documentation stays up-to-date as
the codebase evolves.
By covering these key aspects of your build and packaging process, you
can demonstrate your knowledge and expertise in software development,
and show how you ensure the quality and maintainability of your code. Be
sure to provide specific examples and use cases wherever possible to
support your answer.
AJAY RATHOD 10
Object Oriented Programming
Tell me about OOPS concept in java?
Four principle of OOP? Abstraction, encapsulation, inheritance
polymorphism?
The four principles of Object-Oriented Programming (OOP) are:
Inheritance vs composition?
Inheritance allows a class (called a subclass) to inherit properties and
behaviors from another class (called a superclass). The subclass can then
add or modify these properties and behaviors as needed. It's useful for
creating hierarchies of related classes and sharing code and functionality.
For example, if we have an Animal class, a Mammal class, and a Cat class,
the Cat class can inherit properties and behaviors from both Animal and
Mammal classes while adding its own specific methods.
AJAY RATHOD 11
Abstraction vs polymorphism?
Abstraction is the process of identifying common patterns and extracting
essential features of objects to create classes. It allows us to simplify
complex systems by creating higher-level concepts that can be used in
different contexts. Abstraction involves defining interfaces, classes, and
objects that provide the necessary details for the system's use case while
hiding unnecessary implementation details.
For example, consider an abstract class called "Shape" with properties like
color, abstract methods for area and perimeter calculations, and a
toString method.
For instance, a "Drawable" interface can have methods like draw and fill,
which must be implemented by any class that implements the interface.
AJAY RATHOD 12
a "Circle" class can implement the "Drawable" interface with its own
implementations for the draw and fill methods.
Although you cannot instantiate an abstract class directly, you can still
use its constructor indirectly when you create an instance of a subclass.
When you create an instance of a subclass, the constructor of the abstract
class is automatically called before the constructor of the subclass. This
allows you to set up any common state or behaviour that the subclasses
will share.
this.name = name;
In this example, the Shape class is abstract and has an instance variable
name and a constructor that takes a String parameter. The constructor
initializes the name variable with the value passed in. The Shape class
also has two abstract methods, getArea and getPerimeter, that must be
implemented by any concrete subclass.
AJAY RATHOD 13
So even though you cannot instantiate an abstract class directly, the
constructor can still be useful for initializing common state or behaviour
that subclasses will share.
Inheritance: Look for classes that extend or inherit from other classes.
This is typically indicated by the extends keyword in Java, for example:
public class Car extends Vehicle {...}. Inheritance is used to create a
hierarchy of classes where subclasses inherit properties and methods from
their parent classes.
AJAY RATHOD 14
an object of the wheel class, engine class, and transmission class as its
member variables.
class Animal {
void eat() {
System.out.println("Eating...");
void bark() {
System.out.println("Barking...");
void guard() {
System.out.println("Guarding...");
AJAY RATHOD 15
}
In this example, Animal is the base class, Dog is a derived class from
Animal, and Bulldog is a derived class from Dog.
Animal has a single method eat(). Dog inherits eat() from Animal and
adds a new method bark(). Bulldog inherits both eat() and bark() from
Dog and adds a new method guard().
Now, an instance of Bulldog can access all the methods of its immediate
superclass (Dog), as well as all the methods of its ancestor superclass
(Animal). For example:
AJAY RATHOD 16
identifying the essential characteristics of an object or system, while
encapsulation is concerned with hiding the implementation details of an
object or system from the outside world.
AJAY RATHOD 17
How to achieve encapsulation?
Encapsulation is achieved in Java through the use of access modifiers and
getter and setter methods.
Private: Private variables and methods can only be accessed within the
same class.
return name;
this.name = name;
return age;
AJAY RATHOD 18
public void setAge(int age) {
if (age < 0) {
this.age = age;
In this example, the Person class has two private variables, name and
age. These variables are not directly accessible from outside the class,
which means that other classes cannot modify or access them directly.
Note that we can also add validation logic to the setter methods to ensure
that the values being set are valid. In this example, the setAge method
throws an exception if the age is negative.
By using access modifiers and getter and setter methods, we can achieve
encapsulation in Java. This allows us to protect the data and behavior of
our objects and prevent other objects from accessing or modifying them
directly, which makes our code more robust and maintainable.
Method overloading is when a class has two or more methods with the
same name, but different parameters. When a method is called, the
compiler determines which method to call based on the number and types
of the arguments passed to it.
return x + y;
AJAY RATHOD 19
}
return x + y;
In this example, the Calculator class has two methods named add, one
that takes two integers and one that takes two doubles. When the add
method is called, the compiler determines which version of the method to
call based on the types of the arguments passed to it.
System.out.println("Animal speaks");
System.out.println("Dog barks");
In this example, the Animal class has a method named speak. The Dog
class extends the Animal class and provides its own implementation of the
speak method. When we call the speak method on a Dog object, the Dog
version of the method is called instead of the Animal version.
AJAY RATHOD 20
Method overloading is the ability of a class to have multiple methods with
the same name but with different parameters. This is also known as
"compile-time polymorphism" or "function overloading" in some
languages. For example, a class Calculator might have multiple methods
with the name add, but with different parameters such as add (int a, int
b) and add (double a, double b).
return x + y;
return x + y + z;
return x + y;
AJAY RATHOD 21
}
In this example, Calculator defines three different add() methods with the
same name but different parameters. The first method takes two int
arguments, the second takes three int arguments, and the third takes two
double arguments. The compiler decides which method to call based on
the number and type of arguments passed to it.
@Override
AJAY RATHOD 22
method taking precedence over the superclass's method when called on
an object of the subclass.
Access specifiers determine the visibility of a method, and they can also
be used when overriding methods. When overriding a method, the access
specifier of the overriding method cannot be more restrictive than the
access specifier of the overridden method. In other words, if the
overridden method is public, the overriding method must also be public or
less restrictive.
System.out.println("Animal speaks");
System.out.println("Animal eats");
@Override
System.out.println("Dog barks");
@Override
System.out.println("Dog eats");
AJAY RATHOD 23
}
In this example, the Animal class has a method named speak that is
public, and a method named eat that is protected. The Dog class extends
the Animal class and provides its own implementations of the speak and
eat methods.
The speak method in the Dog class overrides the speak method in the
Animal class and is also public. The eat method in the Dog class overrides
the eat method in the Animal class and is also protected. Since the eat
method in the Animal class is also protected, the access specifier of the
eat method in the Dog class can be the same or less restrictive, but not
more restrictive.
System.out.println("Animal speaks");
System.out.println("Animal eats");
@Override
System.out.println("Dog barks");
AJAY RATHOD 24
}
@Override
System.out.println("Dog eats");
In this example, the Animal class has two methods: speak and eat. Both
methods are declared to throw an Exception.
The Dog class extends the Animal class and overrides both the speak and
eat methods.
The speak method in the Dog class overrides the speak method in the
Animal class and throws an IOException. The IOException is a subclass of
Exception, so this is allowed.
The eat method in the Dog class overrides the eat method in the Animal
class but does not throw any exceptions.
When calling these methods, we can catch the exceptions that are thrown.
For example:
try {
animal.speak();
} catch (IOException e) {
} catch (Exception e) {
try {
animal.eat();
} catch (Exception e) {
AJAY RATHOD 25
}
Since the speak method in the Dog class throws an IOException, we catch
that exception specifically and print out its message. If the speak method
in the Dog class threw a different type of exception, such as
RuntimeException, it would not be caught by this catch block.
The eat method in the Dog class does not throw any exceptions, so the
catch block for Exception will not be executed. If the eat method in the
Dog class did throw an exception, it would be caught by this catch block.
return x + y;
In this example, we have two add() methods with the same parameters,
but different return types (int and double). This will cause a compilation
error because the compiler cannot determine which method to call based
on the return type alone.
AJAY RATHOD 26
When a subclass defines a static method with the same signature as a
static method in the superclass, the subclass method is said to hide the
superclass method. This is known as method hiding. The subclass method
is not considered an override of the superclass method, it is considered a
new method and it hides the superclass method, but it doesn't override it.
AJAY RATHOD 27
smaller and more specific interfaces. For example, instead of having a
single Payment interface that includes all payment methods, we can have
separate interfaces like CreditCardPayment, PayPalPayment, etc.
In general, software design principles strive for high cohesion and low
coupling, as this leads to code that is more modular, maintainable, and
easier to understand and change.
AJAY RATHOD 28
Additionally, a static block is a block of code which is executed only once
when the class is loaded in memory by JVM, before the creation of any
instance of the class, and can be used to perform any one-time
initialization.
class A { }
class B extends A { }
A a = new A();
B b = new B();
a = b; // valid
In the above example, the variable "a" is of type A, and the variable "b" is
of type B. However, the assignment "a = b" is valid, because B is a
subclass of A.
class A { }
class B extends A { }
class C {
AJAY RATHOD 29
B getB() { return new B(); }
In the above example, the method getA() returns an object of type A, and
the method getB() returns an object of type B. Because B is a subclass of
A, the method getB() can be overridden to return B instead of A.
AJAY RATHOD 30
class can implement, interfaces provide a standard way of interacting with
objects, making it easier to reuse components in different parts of a
system.
AJAY RATHOD 31
boolean contains(T item);
int size();
return size() == 0;
This interface defines three methods for adding items to the collection,
checking if an item is contained in the collection, and getting the size of
the collection. It also defines a default method, isEmpty(), that returns
true if the size of the collection is 0.
Classes that implement this interface are not required to provide their own
implementation for isEmpty(), because a default implementation is
already provided in the interface. However, they can override the default
implementation if they need to provide a different behavior.
However, classes that implement the Serializable interface gain the ability
to be serialized and deserialized, which is the behavior that the
Serializable marker interface indicates.
AJAY RATHOD 32
The overriding method can throw the same exceptions as the overridden
method, or any subset of those exceptions.
The overriding method can also throw unchecked exceptions, even if the
overridden method does not.
The overriding method cannot throw checked exceptions that are not in
the same class hierarchy as the exceptions thrown by the overridden
method. This means that the overriding method cannot throw checked
exceptions that are more general than those thrown by the overridden
method. However, it can throw more specific checked exceptions or
unchecked exceptions.
If the overridden method does not throw any exceptions, the overriding
method cannot throw checked exceptions.
class Parent {
AJAY RATHOD 33
}
void foo() {
The fourth override is not valid, since SQLException is not in the same
class hierarchy as IOException.
The fifth override is valid, since it does not throw any exceptions.
AJAY RATHOD 34
public static int count = 0;
Static methods: A static method is a method that belongs to the class and
can be called without creating an instance of the class. Static methods are
declared using the static keyword and are often used for utility methods
that do not depend on the state of an instance.
System.out.println(message);
Static blocks: A static block is a block of code that is executed when the
class is loaded. Static blocks are used to initialize static variables or to
perform other one-time initialization tasks.
static {
In this example, the static block is executed when the Example class is
loaded and prints a message to the console.
AJAY RATHOD 35
Difference between static variable and instance variables?
Static variables and instance variables are both types of variables used in
programming, but they differ in their scope and lifetime.
Static variables are declared using the "static" keyword and are shared
across all instances of a class. They are initialized only once, when the
class is loaded, and retain their value throughout the execution of the
program. Static variables are typically used to store data that is common
to all instances of a class, such as a constant or a count of objects
created.
Instance variables, on the other hand, are declared without the "static"
keyword and are unique to each instance of a class. They are initialized
when an object is created and are destroyed when the object is destroyed.
Instance variables are typically used to store data that is specific to each
instance of a class, such as the name or age of a person.
Scope: Static variables have class scope, while instance variables have
object scope.
Lifetime: Static variables are initialized once and retain their value
throughout the execution of the program, while instance variables are
created and destroyed with the objects they belong to.
Usage: Static variables are used to store data that is common to all
instances of a class, while instance variables are used to store data that is
specific to each instance of a class.
Provide only getter methods for the instance variables, and make sure
they only return the value without allowing modification.
Do not provide any setter methods or any other methods that can modify
the state of the object.
AJAY RATHOD 36
Ensure that the class cannot be subclassed by declaring it as final or
making its constructor private.
this.name = name;
this.age = age;
return name;
return age;
public: The public access modifier is the most permissive access level, and
it allows access to a class, method, or variable from any other class,
regardless of whether they are in the same package or not.
AJAY RATHOD 37
accessed from within the same package, but not from outside the
package.
private: The private access modifier is the most restrictive access level,
and it allows access to a class, method, or variable only from within the
same class. It cannot be accessed from any other class, even if they are
in the same package.
int defaultVar;
// ...
// ...
void defaultMethod() {
// ...
// ...
In this example, the MyClass class has four instance variables and four
instance methods, each with a different access modifier. The publicVar
and publicMethod() can be accessed from any other class, while
protectedVar and protectedMethod() can be accessed from any subclass
and from within the same package. defaultVar and defaultMethod() can be
accessed from within the same package only, while privateVar and
privateMethod() can be accessed only from within the same class.
AJAY RATHOD 38
What is the use of protected members?
Protected members in Java are used to provide access to class members
within the same package and to subclasses of the class, even if they are
in a different package. The protected access modifier is more restrictive
than public, but less restrictive than private.
The protected access modifier is useful when you want to expose certain
methods or variables to subclasses, while still hiding them from other
classes in the same package or outside the package. For example, you
might have a superclass with some variables and methods that are not
intended to be used outside the class or package, but should be accessible
to subclasses. In this case, you can declare those variables and methods
as protected.
package com.example.package1;
// ...
package com.example.package2;
import com.example.package1.Superclass;
protectedVar = 42;
protectedMethod();
AJAY RATHOD 39
}
AJAY RATHOD 40