Accenture interview 4Yrs experience — Java, Spring boot, Micro services, Kafka — Part 2

Amarmasote
4 min readApr 23, 2024

--

This is part 2 of this series. If you haven’t read part 1. do visit this

4. Explain the difference between checked and unchecked exceptions.

In Java, exceptions are classified into two main categories: checked exceptions and unchecked exceptions.

Checked Exceptions:

  • In general, checked exceptions represent errors outside the control of the program
  • Checked exceptions are exceptions that the Java compiler requires you to handle explicitly using try-catch blocks or by declaring them in the method signature using the throws keyword.
  • These exceptions typically represent conditions that a well-behaved application should anticipate and recover from. Examples include file not found exceptions, SQL exceptions, or checked socket exceptions.
  • Checked exceptions are subclasses of Exception (but not subclasses of RuntimeException) and are usually related to external factors beyond the control of the program, such as I/O errors or network failures.
  • By requiring handling or declaration, checked exceptions help ensure that code is more robust and forces developers to acknowledge potential failure points in their code
public class IncorrectFileNameException extends Exception {
public IncorrectFileNameException(String errorMessage) {
super(errorMessage);
}
}

Unchecked Exceptions:

  • Unchecked exceptions, also known as runtime exceptions, are exceptions that do not need to be declared in a method’s signature or handled explicitly using try-catch blocks.
  • These exceptions typically represent programming errors or logical errors within the application and are not expected to be recoverable at runtime. Examples include NullPointerException, ArrayIndexOutOfBoundsException, or ClassCastException.
  • Unchecked exceptions are subclasses of RuntimeException. They are often caused by bugs in the code, such as attempting to access an element of an array beyond its bounds or attempting to invoke a method on a null object reference.
  • Since unchecked exceptions are not required to be caught or declared, they can propagate up the call stack until they are caught by an appropriate catch block or result in the termination of the program if not handled
public class NullOrEmptyException extends RuntimeException {
public NullOrEmptyException(String errorMessage) {
super(errorMessage);
}
}

5. What are advantages of spring boot over spring framework

  1. Auto-configuration: Spring Boot’s auto-configuration feature automatically configures the Spring application based on the dependencies present in the classpath. It scans the classpath and automatically configures the beans required for various functionalities such as database access, messaging, and web applications.
  2. Standalone Applications: Spring Boot allows you to create standalone applications with an embedded HTTP server (like Tomcat, Jetty, or Undertow). This means you can package your application as a JAR file, including the web server, and run it as an independent process, without the need for a separate application server like traditional Spring applications
  3. Simplified Dependency Management: Spring Boot simplifies dependency management by providing a set of starter dependencies. These starter dependencies include all the necessary dependencies for common tasks such as web development, data access, security, and more
  4. Production Readiness: Spring Boot promotes best practices for building production-ready applications. It includes features such as health checks, metrics, and externalized configuration, making it easier to monitor and manage applications in production environments

6. Explain the difference between @Component, @Service, @Repository, and @Controller annotations.

In Spring Framework, @Component, @Service, @Repository, and @Controller are stereotype annotations used to denote the roles of various Java classes within an application. While all these annotations are technically similar and can be used interchangeably in many cases, they convey specific meanings and intentions to developers and tools

‘@component’

  • is the base annotation that other stereotype annotations like @Service, @Repository, and @Controller are built upon.
  • is a generic stereotype annotation used to indicate that a class is a Spring component
  • It can be used to annotate classes that are intended to be automatically detected and registered as Spring beans during the component scanning process

‘@service’

  • is a specialization of @Component and is used to annotate classes that perform service tasks in a Spring application.
  • It’s typically used to mark classes that contain business logic, perform operations, or implement business processes.
  • Using @Service helps in better organizing and clarifying the role of the annotated class in the application architecture.

‘@Repository’

  • is a specialization of @Component and is used to annotate classes that encapsulate data access logic or repository access logic.
  • It’s typically used to mark classes that interact with a database, such as performing CRUD operations (Create, Read, Update, Delete) on persistent entities.
  • @Repository annotations facilitate exception translation, converting exceptions thrown by data access technologies (like Hibernate or JPA) into Spring's DataAccessException hierarchy.

‘@Controller’

  • is used to annotate classes in a Spring MVC application to indicate that the annotated class serves as a controller.
  • Controllers in Spring MVC are responsible for handling user requests, processing inputs, invoking business logic, and returning a response, often in the form of a view.
  • Typically, @Controller classes contain methods annotated with @RequestMapping or other request mapping annotations to handle different HTTP requests.

7. Explain the role of ZooKeeper in Kafka.

  • ZooKeeper is used in Kafka for distributed coordination, synchronization, and management of Kafka brokers and clusters.
  • It maintains metadata about Kafka brokers, topics, partitions, and consumer groups.
  • ZooKeeper helps in leader election, detecting broker failures, and maintaining cluster state consistency

8. What are the different message delivery semantics in Kafka?

Kafka supports three message delivery semantics:

  • At most once: Messages are delivered to consumers but may be lost if the consumer fails to process them.
  • At least once: Messages are guaranteed to be delivered to consumers but may be processed and delivered multiple times in case of failures.
  • Exactly once: Messages are delivered to consumers exactly once, ensuring both message delivery and processing semantics.

stay tuned for part 3. Do not hesitate to clap 👏 if you liked the content.

--

--