IBM Java Developer Interview 0–3 years of Experience 2024.
Hello Folks, In this article we will go through recent interview experience of a java developer having experience of 0–3 years. This will help java developers who are preparing for the interview.
Are you preparing for a job interview as a Java developer?
Find my book Guide To Clear Java Developer Interview here Gumroad(PDFFormat) and Amazon (Kindle eBook).
Guide To Clear Spring-Boot Microservice Interview here Gumroad (PDFFormat) and Amazon (Kindle eBook).
Guide To Clear Front-End Developer Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).
Download the sample copy here:
Guide To Clear Java Developer Interview[Free Sample Copy]
Guide To Clear Spring-Boot Microservice Interview[Free Sample Copy]
Guide To Clear Front-End Developer Interview [Sample_Copy]
Ready for personalized guidance? Book your 1-on-1 session with me here: https://topmate.io/ajay_rathod11
What are the Spring bean scopes and their use cases?
Spring Framework supports several bean scopes, which define the lifecycle and visibility of a bean within the application contexts. Here are the main scopes:
- Singleton: This is the default scope. A single instance of the bean is created and shared across the entire application context. It is stateless in nature. Use case: Service classes, DAOs, Repositories, etc.
- Prototype: A new instance of the bean is created each time it is requested from the container. It is stateful in nature. Use case: Beans that are stateful or not thread-safe.
- Request: A new instance of the bean is created for each HTTP request. This scope is specific to web applications. Use case: Beans that contain user-specific state and are not thread-safe.
- Session: A new instance of the bean is created for each HTTP session. This scope is also specific to web applications. Use case: Beans that need to maintain user session data.
- Application: A single instance of the bean is shared across all servlet contexts in the application. Use case: Beans that need to be shared at the application level, like global configuration data.
- WebSocket: A new instance of the bean is created for each WebSocket connection. This is specific to applications using WebSocket. Use case: Beans that are specific to a WebSocket user session.
Each scope serves different use cases depending on the bean’s required lifecycle and visibility across the application. Singleton and Prototype are the most commonly used scopes, while the others are more specific to web applications.
In how many ways we can create a thread?
In Java, there are two primary ways to create a thread:
- Extending the
Thread
class:
- Create a new class that extends the
Thread
class. - Override the
run()
method to define the code that should execute in the new thread. - Create an instance of the class and call the
start()
method to run the new thread.
2. Implementing the Runnable
interface:
- Create a new class that implements the
Runnable
interface. - Implement the
run()
method to define the code that should execute in the new thread. - Create an instance of the class and pass it to a
Thread
object, then call thestart()
method on theThread
object to run the new thread. - Here’s a brief example of each method:
Extending the Thread
class:
class MyThread extends Thread {
public void run() {
// Code to execute in new thread
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // Starts the new thread
}
}
Implementing the Runnable
interface:
class MyRunnable implements Runnable {
public void run() {
// Code to execute in new thread
}
}
public class Main {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread t = new Thread(myRunnable);
t.start(); // Starts the new thread
}
}
Both methods are valid, but implementing the Runnable
interface is more flexible, allowing the class to extend another class if needed.
What is the output of below code ?
thread.run();
thread.run();
thread.start();
Create a HashSet of type String and add duplicate strings? what be the output of the program given below?
hs.add("Abcd");
hs.add("efg");
hs.add("abcd");
String s = new String("Abcd");
hs.add(s);
hs.add("Abcdefg");
System.out.println(hs);
What is the benefit of using Micro-service architecture?
Microservice architecture offers several benefits, particularly for complex, large-scale, and evolving applications. Here are some of the key advantages:
- Modularity: It breaks down complex applications into smaller, manageable pieces that are easier to develop, maintain, and understand.
- Scalability: Individual components can be scaled independently, allowing for more efficient use of resources and improving the application’s overall performance.
- Flexibility in Technology: Different microservices can be written in different programming languages, use different data storage technologies, and adopt new technologies without affecting the entire system.
- Faster Deployment: Smaller, independent services can be deployed faster, which accelerates the release cycle and enables continuous delivery and deployment.
- Resilience: Failure in one service does not necessarily bring down the whole system. The modular nature of microservices allows for better fault isolation and recovery.
- Improved Fault Isolation: Since each microservice is independent, issues can be isolated quickly to the specific service, making troubleshooting and recovery faster.
- Easier to Scale Development Teams: Microservices allow for distributed development teams to work on separate services simultaneously without much coordination overhead.
- Optimized for the Cloud: Microservices are well-suited for cloud environments, which offer elasticity, automation in deployment, and scaling.
- Enhanced Business Agility: By enabling quicker updates and improving the speed of introducing new features, microservices architecture supports business agility, allowing companies to adapt to market changes more rapidly.
In micro-services communication, which protocol is lightweight and commonly used?
HTTP protocol based microservices are commonly used, let me know if you find any useful one.
Which micro-service pattern is used for to prevent failures from cascading to others service?
The microservice pattern used to prevent failures from cascading to other services is the Circuit Breaker pattern.
The Circuit Breaker pattern aims to detect failures and encapsulate the logic of preventing a failure from constantly recurring, thereby protecting the system from further damage.
When a microservice component becomes unhealthy, the circuit breaker trips, and further calls to the component are blocked or redirected, usually returning a fallback response. After a certain period, the circuit breaker allows a limited number of test requests to pass through.
If these requests succeed, the circuit breaker resumes normal operation; otherwise, it continues to block requests. This pattern helps maintain system stability and resilience in distributed systems.
In how many ways we can make a REST API call in Micro services?
In microservices architecture, REST API calls can be made in several ways, depending on the technology stack and requirements. Here are some common methods:
HTTP Client Libraries:
- Java: Use
HttpClient
(Java 11 and above),RestTemplate
orWebClient
(Spring Framework). - Python: Use
requests
orhttp.client
. - JavaScript (Node.js): Use
axios
,fetch
, or thehttp
andhttps
modules.
Feign Client (Spring Cloud): A declarative REST client for microservices communication in Spring applications. It simplifies writing HTTP clients and integrates with Ribbon and Eureka for load balancing.
Retrofit (Java): A type-safe HTTP client for Android and Java applications. It allows for synchronous and asynchronous HTTP requests directly to REST endpoints.
gRPC: A high-performance, open-source universal RPC framework that can run in any environment. It uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, load balancing, and more.
GraphQL: An alternative to REST, GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data. It allows clients to request exactly the data they need, making it efficient for complex systems and microservices.
Message Brokers (e.g., RabbitMQ, Apache Kafka): Though not RESTful, message brokers can be used for asynchronous communication between microservices, supporting event-driven architectures.
Each method has its use cases, advantages, and disadvantages, depending on the requirements for communication, data format, performance, and the complexity of the services involved.
What is the @Restcontroller annotation in Spring Boot?
The @RestController
annotation in Spring Boot is a convenience annotation that combines @Controller
and @ResponseBody
. It is used at the class level and indicates that the class is a controller where every method returns a domain object instead of a view. It simplifies the creation of RESTful web services.
@Controller
: Marks the class as a web controller, capable of handling HTTP requests.@ResponseBody
: Indicates that the return value of a method should be used as the response body of the request.
When you annotate a class with @RestController
, Spring treats it as a controller, and the return value of each method in the class is automatically serialized into JSON or XML and written into the HttpResponse object. This makes it ideal for building RESTful web services with Spring Boot.
@RestController
public class ExampleController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
In this example, a GET request to /hello
will return a plain text response with the content "Hello, World!". The @RestController
annotation ensures that this string is directly written to the response body.
Which of annotations is used to specify a bean as a candidate for dependency injection in Spring Boot?
The annotation used to specify a bean as a candidate for dependency injection in Spring Boot is @Component
. This annotation marks a Java class as a bean, making it eligible for auto-detection and auto-configuration when using annotation-based configuration and classpath scanning.
Additionally, there are several specialized forms of @Component
for specific purposes:
@Service
: Indicates that an annotated class is a "Service" (e.g., a business service facade).@Repository
: Indicates that an annotated class is a "Repository" (e.g., a Data Access Object).@Controller
: Indicates that an annotated class is a "Controller" (e.g., a web controller).
All these annotations make the annotated class eligible for auto-detection and dependency injection.
Why to use Spring Boot actuator?
Spring Boot Actuator is used for monitoring and managing your application when it’s pushed to production. It provides a series of built-in endpoints allowing you to monitor and interact with your application. Key reasons to use Spring Boot Actuator include:
- Health Check: It provides detailed health information about your application, which can be used to check the status of your application in production.
- Metrics Collection: Actuator gathers application metrics, such as HTTP requests and response statistics, database metrics, cache statistics, and more, which can be crucial for diagnosing issues and understanding application behavior.
- Application Info: It can expose application information, including Git version information, build information, and custom application info.
- Dynamic Logging Levels: Actuator allows changing log levels of your application at runtime without restarting the application.
- Thread Dump: It can generate a thread dump, which can be very useful for diagnosing deadlock situations in your application.
- Environment Information: It provides details about the environment properties, configuration properties, system properties, and environment variables.
- Audit Events: If your application is configured to record audit events, Actuator can expose information about security-related events like user login/logout, access denied, etc.
- Custom Endpoints: Beyond the built-in endpoints, Actuator allows you to define custom endpoints to expose specific functionality or data relevant to your application.
Spring Boot Actuator is a powerful tool for application insights and management, making it easier to maintain and troubleshoot applications in a production environment.
Which annotations is used to declare a Spring Boot application’s main method?
The annotation used to declare a Spring Boot application’s main method is @SpringBootApplication
. This annotation is typically placed on the main class and serves several purposes:
- It enables auto-configuration, allowing Spring Boot to automatically configure your application based on the dependencies present on the classpath.
- It enables component scanning, allowing Spring to automatically discover and register beans within the application context.
- It enables configuration properties, allowing for externalized configuration.
Here’s an example of how it’s used:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Which design pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable?
The design pattern used to define a family of algorithms, encapsulate each one, and make them interchangeable is the Strategy Pattern. This pattern allows the algorithm to vary independently from clients that use it by defining a family of algorithms, encapsulating each one into its own class, and making them interchangeable through the use of a common interface.
Which design pattern is used to add new functionality to an object dynamically?
The design pattern used to add new functionality to an object dynamically is the Decorator Pattern. This pattern allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often used for extending (decorating) the functionality of a class in a flexible and reusable way.
How cloud side load balancing is done in spring boot?
Cloud-side load balancing in Spring Boot, often referred to as server-side load balancing, is typically managed by the cloud infrastructure or a dedicated load balancer appliance rather than by the application itself. This approach leverages the capabilities of cloud providers (like AWS, Azure, Google Cloud) or dedicated load balancers (like NGINX, HAProxy) to distribute incoming application traffic across multiple instances of the application, thereby improving the responsiveness and availability of applications.
Which keyword is used to handle exceptions in java?
The keyword used to handle exceptions in Java is try-catch
. Additionally, finally
can be used alongside try-catch
for code that must execute regardless of whether an exception is thrown or not.
Can we throw exception from static block?
Yes, you can throw an exception from a static block in Java. However, since a static block is executed when the class is loaded, you can only throw a checked exception if you declare it with the throws
keyword on the class declaration, which is not allowed in Java. Therefore, you can only throw unchecked exceptions (runtime exceptions) from a static block without needing to declare them. Here's an example:
public class MyClass {
static {
// Throwing an unchecked (runtime) exception
throw new RuntimeException("Exception from static block");
}
}
Attempting to throw a checked exception directly from a static block without handling it inside the block will result in a compilation error.
Can we have 2 finally block for 1 try catch?
No, in Java, you cannot have two finally
blocks for one try-catch
block. Each try
block can be followed by zero or more catch
blocks and only one finally
block. The finally
block is optional but, if present, must come after the try
and any catch
blocks.
Difference between read time out and connection timed out?
In the context of network operations, including HTTP requests, the terms “read timeout” and “connection timeout” refer to two different timeout behaviors:
- Connection Timeout: This is the time limit for establishing a connection between two systems. It specifies the maximum amount of time a client will wait for a connection to be established with a server. If the connection cannot be established within this time frame (due to network issues, the server being down, etc.), the attempt is aborted, and a connection timeout error is thrown.
- Read Timeout: Once a connection has been established, the read timeout specifies the maximum amount of time the client will wait for a response from the server after sending a request. This includes waiting for data to start being received, not the total time to download the response. If the server takes longer than this time to respond, a read timeout error occurs, indicating that the server is taking too long to send the data.
Both timeouts are crucial for robust network programming, allowing applications to handle network delays and unavailability gracefully.
Which HTTP method is typically used for safe and idempotent retrieval of information without modifying resources?
The HTTP method typically used for safe and idempotent retrieval of information without modifying resources is GET
.
Looking for more transcript !! here are the links
Synechron Java Interview Questions and answers for( 2–4 Exp)
Citi Bank Java developer Interview Question for 5+ Experienced Professional
HSBC Java Interview Experience with Q&A for 0–5 years of Experience
Cisco Java Developer Interview Transcript 2024(Java,Spring-Boot,Hibernate)
Thanks for reading
- 👏 Please clap for the story and follow me 👉
- 📰 Read more content on my Medium (60 stories on Java Developer interview)
Find my books here: