0% found this document useful (0 votes)
12 views7 pages

Interview Questions

The document discusses interview questions related to Java programming. It covers topics like object-oriented programming concepts in Java like classes, objects, inheritance and polymorphism. It also discusses multi-threading concepts like thread states, synchronization. The document also talks about Stream API in Java.

Uploaded by

suneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Interview Questions

The document discusses interview questions related to Java programming. It covers topics like object-oriented programming concepts in Java like classes, objects, inheritance and polymorphism. It also discusses multi-threading concepts like thread states, synchronization. The document also talks about Stream API in Java.

Uploaded by

suneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Interview Questions

1. What is the objective of the project? (What are you trying to solve?)
2. What is the team size and your role in the team
3. Technologies you have used (IDEs, Version Control, CICD, Deployment platform)
4. What are some coding standards followed while developing
o Linting - pylint, flake8
o Formatting – black
o Type-checking – mypy
o Logging - logging, log4j
o Docstrings
o Testing – unit tests
o

Core Java
Which version of Java you have used?

Can an instance variable be declared as static? Explain


An instance variable cannot be declared as static. If an instance variable is declared as static, then that
variable becomes a class variable.

Explain the differences between a constructor and an ordinary method.


There are several differences between a constructor and an ordinary method. These are as follows:

 A constructor has the same name as the class name, an ordinary method can have any name.
 A constructor does not have a return type, not even void. A method has a return type. If a
method does not return any value, then the keyword void needs to be specified.
 A constructor is automatically invoked when an object of the class is created. A method needs
to be invoked explicitly.
 If a class does not have a constructor, Java automatically creates a default constructor. If a
class does not have a method, Java does not automatically add a method.

What is an inner class? What are different types of inner classes?


An inner class is any class defined inside the body of another class. There are 4 types of inner classes
as follows:

 Nested class: Class is defined within the body of another class. It has access to all the
members of the outer class.
 Static nested class: A nested class that is static is known as a static nested class. It cannot
access non–static members of the outer class.
 Method local inner class: A class that is defined inside a method of another class is known as
a method local inner class. Such an inner class can only access final members of the outer
class.
 Anonymous inner class: A class that has no name and can be instantiated only once when it
is defined is known as an anonymous inner class. It does not have a constructor as it does not
have a name. It cannot be static. The class definition ends with a semicolon.
Write a code sample that demonstrates how you can instantiate an inner class from an
outer class

Explain the different types of Threads in Java


There are 2 types of threads–User defined Threads and Daemon threads
1. User defined threads are those that are created programmatically by a user. These are high
priority threads. The JVM waits for these threads to finish.
2. Daemon threads are mostly created by the JVM, although a user–defined thread can be
explicitly set to be a daemon thread as well. Daemon threads are usually used for background
processes such as Garbage Collection. As soon as all Non–Daemon threads stop running, the
JVM stops running and does not wait for the Daemon threads to stop.

Explain thread states


Thread exists in five states. They are as follows:
1. New: A thread is in new state when a new Thread is created but the start method is not
invoked.
2. Runnable: A Thread is in the Runnable state after the start() method is invoked but the
Thread scheduler has not yet started executing the thread.
3. Running: When the thread scheduler starts executing the thread, it is in the Running state.
The code within the body of the run() method gets executed.
4. Waiting / Blocking: If a thread is alive, but not eligible to run, it is said to be in the blocking
state.
5. Dead: When the thread completes its execution, it enters the dead state.

What is multithreading
Multithreading is a situation in which multiple threads are executed simultaneously. Multithreading
consumes less memory. As threads are lightweight, they enhance the performance of an application.

What is difference between a process and a thread?


A program in the execution is called the process. Thread is a subset of the process.

 Processes are independent, but the threads are the subset of a process.
 Processes have different address space in the memory. However, the threads contain a shared
address space of the process.
 The inter-thread communication is faster and easier than the inter-process communication.
 The context switching is faster between the threads as compared to the processes.
A thread is a lightweight sub-process. It is a separate path of execution because each thread runs in a
different stack frame. A process may contain multiple threads. Threads share the process resources,
but still, they execute independently.

Advantages of multithreading
 The thread is lightweight. So, it enhances the application performance.
 Multithreading reduces more servers, as one server can execute multiple threads at a time.
 Even though some background task is running, multithreading allows an application to be
always reactive to accept an input.
 As multithreading includes execution of multiple threads independently, it allows the faster
execution of tasks.
 The threads share the common memory resources. So, multithreading provides better
utilization of cache memory.
What is Synchronization?
When multiple threads try to access a shared resource at the same time, then they need some way for
the resource to be accessed by only one Thread at a time. The process which helps to achieve this is
called as synchronization. Java provides a keyword called synchronized which helps to achieve this.

What happens when a synchronized method is invoked?


Whenever a synchronized method is executed, the thread that invokes the method acquires a lock and
holds the lock under the end of the method. Only after the current thread completes executing the
method it releases the lock. So other threads can then acquire the lock and execute the method. Thus,
the synchronized keyword prevents multiple threads from simultaneously executing the code within
the synchronized method.

Difference between synchronous and asynchronous programming


 Synchronous: A thread is assigned to complete a task. Once the thread starts working on the
task, it is only available for other tasks after the completion of the assigned task.
 Asynchronous programming: One job can be completed by more than one threads. It
enhances the application performance and hence provides maximum usability.

What is a functional interface? How can you create a functional interface? Examples
(used in Lambda’s – Function, Predicate, Consumer etc.)
A functional interface is an interface that has only one abstract method. To create a functional
interface, you simply need to create an interface that has just one abstract method as shown below:
@FunctionalInterface
Public interface Multiplier {
Public int multiply(int a, int b);
}

This code defines an interface called Multiplier. It has only a multiply method. It has the
@FunctionalInterface annotation specified. This annotation is optional, it marks the
interface as a functional interface. So, if you try to add another abstract method to the interface, the
code causes a compilation error.

The java.util.Function is a new package added by Java 8. It has a lot of built–in functional interfaces.
Some of the interfaces in this package are as follows:
1. Predicate: This can be used to test a condition. It returns a Boolean value which indicates
whether the condition is true or false. It accepts an argument of any data type.
2. Consumer: This can be used to operate on a value. It accepts an argument of any data type
and operates on it. It returns a void.
3. Function: This can be used to transform an input value. It accepts an argument of any data
type, transforms it and returns a result.
4. Supplier: This can be used to produce a value. It does not accept any argument but produces a
result of any data type.

Have you used lambda expressions? If so, where, and how?

Explain the types of Stream operations


Stream operations can be categorized as follows:
1. Intermediate: Intermediate operations operate on Streams and produce a Stream output. Since
intermediate operations produce a Stream, they can be chained to perform a series of
operations. Some examples of intermediate operations are filter, map, sorted.
2. Terminal: Terminal operations operate on Streams but produce a non–stream result. So, they
can produce a result of any data type. Terminal operations cannot be chained. When several
Stream operations are chained, a terminal operation is typically the last operation. Some
examples of terminal operations are count, anyMatch, allMatch, collect.

What is the difference between a Stream and a Collection?


There are several differences between a Stream and a Collection. These are as follows:
 Collections store the data. A stream on the other hand does not store any data, it just performs
some operations on the underlying data.
 When you perform an operation on a Collection, the Collection gets modified. When you
perform an operation on a Stream obtained on a Collection, the underlying Collection does
not get modified.
 A Collection can be modified after it is created. A Stream on the other hand cannot be
modified after it is constructed.
 You can traverse a Collection any number of times. However, you can traverse a Stream only
once. If you wish to traverse over a Stream again it is not possible, you need to create a new
Stream from the underlying source and traverse it again.

What are different categories of Java design patterns


One can categorize the design patterns as:

 Creational patterns: These patterns are used to define and describe the ways of creating the
objects. They describe in depth on how objects are created at class instantiation time.
o Factory method
o Abstract factory
o Builder
o Prototype
o Singleton
 Structural Patterns: The structural design patterns are concerned about how the classes and
objects can be composed together to form a larger structure. They offer an efficient solution
for the class compositions and object structures. The patterns are dependent on the concept of
inheritance, which allows multiple classes or objects to work together as a single working
unit. They simplify the structure by identifying the relationships in between them. These
patterns focus on how the classes are inherited from each other and how they are composed
from other classes
o Adapter
o Bridge
o Filter
o Composite
o Decorator
o Façade
o Flyweight
o Proxy
 Behavioural patterns: The behavioural design patterns care about the interaction and
responsibility of objects. The patterns focus on the ease in the interaction between the objects.
However, the patterns make sure the communicating objects will be loosely coupled to avoid
hardcoding.
o Interpreter
o Template method/pattern
o Chain of responsibility
o Command pattern
o Iterator pattern
o Strategy pattern
o Visitor pattern
o

Authentication: JWT & OAuth2


 OAuth 2.0 is an authorization protocol and NOT an authentication protocol. As such, it is
designed primarily as a means of granting access to a set of resources, for example, remote
APIs or user data.
 OAuth 2.0 uses Access Tokens. An Access Token is a piece of data that represents the
authorization to access resources on behalf of the end-user. OAuth 2.0 doesn’t define a
specific format for Access Tokens.
 OAuth 2.0 defines a protocol, i.e., specifies how tokens are transferred.
 JWT defines a token format.
 OAuth 2.0 and "JWT authentication" have similar appearance when it comes to the (2nd)
stage where the Client presents the token to the Resource Server: the token is passed in a
header.
 But "JWT authentication" is not a standard and does not specify how the Client obtains the
token in the first place (the 1st stage). That is where the perceived complexity of OAuth
comes from: it also defines various ways in which the Client can obtain an access token from
something that is called an Authorization Server.
 The real difference is that JWT is just a token format, OAuth 2.0 is a protocol (that may use a
JWT as a token format).
 To authenticate a user, a client application must send a JSON Web Token (JWT) in the
authorization header of the HTTP request to your backend API. API Gateway validates the
token on behalf of your API, so you don't have to add any code in your API to process the
authentication.
 One final piece of advice, even if you don't need to go full OAuth 2.0, I would strongly
recommend on passing your access token within the Authorization header instead of going
with custom headers

RxJava

WebFlux

Testing

Junit/Mocito

Spring
In Spring framework, which Dependency Injection is better? Constructor-based DI or
Setter-based DI?
Spring framework provides support for both Constructor-based and Setter-based Dependency
Injection. There are different scenarios in which these options can be used.
It is recommended to use Constructor-based DI for mandatory dependencies. Whereas Setter-based DI
is used for optional dependencies.

What design-patterns are used in Spring framework?


Spring framework uses many Design patterns. Some of these patterns are:
 Singleton – By default beans defined in spring config files are singleton. These are based on
Singleton pattern.
 Template – This pattern is used in many classes like JdbcTemplate, RestTemplate,
JmsTemplate, JpaTemplate etc.
 Dependency Injection – This pattern is the core behind the design of BeanFactory and
ApplicationContext.
 Proxy – Aspect Oriented Programming (AOP) heavily uses proxy design pattern.
 Front Controller – DispatcherServlet in Spring is based on Front Controller pattern to ensure
that incoming requests are dispatched to other controllers.
 Factory pattern – To create an instance of an object, BeanFactory is used. This is based on
Factory pattern.
 View Helper – Spring has multiple options to separating core code from presentation in
views. Like- Custom JSP tags, Velocity macros etc.

RestTemplate

How will you handle exceptions in Spring MVC Framework?


Spring MVC Framework provides following mechanisms to help us achieve exception handling:
 Controller Based: A developer can define exception handler methods in a Controller class.
To do so, they must annotate the methods with @ExceptionHandler annotation.
 Global Exception Handler: Spring provides @ControllerAdvice annotation for exception
handling as cross-cutting concern. We can mark any class as global exception handler by
using this annotation.
 HandlerExceptionResolver implementation: Spring Framework provides
HandlerExceptionResolver interface that can be implemented to create a global exception
handler.

Microservices Architecture

MySQL/Postgres/MongoDB

What are the Transient, Persistent and Detached states of an object in Hibernate?
 When an object is just instantiated using the new operator but is not associated with a
Hibernate Session, then the object is in Transient state.
 In Transient state, object does not have a persistent representation in database. Also, there is
no identifier assigned to an object in Transient state.
 An object in Transient state can be garbage collected if there is no reference pointing to it.
 An object is in detached state if it was persistent earlier, but its Session is closed now.
 Any reference to this object is still valid. We can even update this object. Later on, we can
even attach an object in detached state to a new session and make it persistent.
 Detached state is very useful in application transactions where a user takes some time to
finish the work.

https://www.geeksforgeeks.org/hibernate-lifecycle/

Redis Cache/In memory Cache

Git

Docker

Kubernetes

OpenShift

You might also like