EJB Interview Questions and Answers for internship
-
What is EJB?
- Answer: EJB stands for Enterprise JavaBeans. It's a server-side component architecture for building distributed, transactional, and secure enterprise applications using Java. It simplifies development by handling many low-level details like transaction management, security, and persistence.
-
What are the different types of EJBs?
- Answer: There are three main types: Session Beans (stateless, stateful, singleton), Message-Driven Beans (MDBs), and Entity Beans (now largely replaced by JPA).
-
Explain Stateless Session Beans.
- Answer: Stateless Session Beans (SLSBs) don't maintain conversational state between method invocations. Each method call is treated independently. They are highly scalable and easily deployed.
-
Explain Stateful Session Beans.
- Answer: Stateful Session Beans (SFSBs) maintain conversational state between method calls for a specific client. The bean instance is associated with a particular client. They provide a more personalized experience but are less scalable than SLSBs.
-
What are Singleton Session Beans?
- Answer: Singleton Session Beans provide only one instance of the bean across the entire application. They are useful for managing application-wide resources or caches.
-
What are Message-Driven Beans (MDBs)?
- Answer: MDBs are asynchronous beans that process messages from JMS (Java Message Service) queues or topics. They are ideal for handling asynchronous tasks and decoupling components.
-
What is the role of the EJB container?
- Answer: The EJB container is responsible for managing the lifecycle of EJBs, providing services like transaction management, security, concurrency control, and persistence.
-
What is the difference between local and remote interfaces in EJBs?
- Answer: Local interfaces are used for communication within the same JVM as the EJB container. Remote interfaces allow communication across JVMs, usually using RMI (Remote Method Invocation).
-
Explain EJB deployment descriptors.
- Answer: Deployment descriptors (usually `ejb-jar.xml`) are XML files that contain metadata about EJBs, specifying properties like transaction attributes, security roles, and resource references.
-
What are transaction attributes in EJBs?
- Answer: Transaction attributes define how EJB methods interact with transactions. Common attributes include Required, RequiresNew, Mandatory, NotSupported, and Supports.
-
Explain the concept of dependency injection in EJBs.
- Answer: Dependency Injection (DI) is a design pattern where dependencies are provided to an EJB through its constructor or setter methods, rather than the EJB creating them itself. This improves testability and maintainability.
-
How are EJBs secured?
- Answer: EJBs can be secured using various mechanisms, including role-based security, declarative security using annotations, and programmatic security using JAAS (Java Authentication and Authorization Service).
-
What is a JNDI lookup?
- Answer: JNDI (Java Naming and Directory Interface) lookup is a mechanism to retrieve references to EJBs and other resources from the application server's JNDI namespace.
-
What are the advantages of using EJBs?
- Answer: Advantages include simplified development, improved scalability, enhanced security, robust transaction management, and ease of deployment.
-
What are the disadvantages of using EJBs?
- Answer: Disadvantages can include complexity (especially for beginners), potential performance overhead, and vendor lock-in.
-
Explain the lifecycle of a stateless session bean.
- Answer: The container manages the lifecycle. Beans are pooled, created when needed, and removed from the pool when idle, after a timeout, or when the container decides.
-
Explain the lifecycle of a stateful session bean.
- Answer: The container manages the lifecycle. A bean is created when a client invokes its methods and remains in existence until the client removes it or the bean times out.
-
How do you handle concurrency in EJBs?
- Answer: Concurrency is handled through bean pooling (for SLSBs), container-managed concurrency, and proper synchronization within bean methods (though generally discouraged due to the container's superior handling).
-
What is the difference between container-managed persistence and bean-managed persistence?
- Answer: CMP (Container-Managed Persistence) was an older approach where the container handled persistence details. BMP (Bean-Managed Persistence) required the bean to handle its own persistence, now largely obsolete with JPA's preference.
-
How does EJB interact with JPA?
- Answer: EJBs often use JPA (Java Persistence API) for persistence. EJBs can inject JPA entities and use the EntityManager to interact with the database.
-
What is the role of an EJB timer service?
- Answer: The EJB timer service allows scheduling the execution of EJB methods at specific times or intervals.
-
Explain the concept of interceptors in EJBs.
- Answer: Interceptors are methods that are invoked before and/or after the invocation of a bean's business methods, allowing for cross-cutting concerns like logging or auditing.
-
What are some best practices for developing EJBs?
- Answer: Best practices include using the appropriate bean type, keeping methods short and focused, using dependency injection, handling exceptions properly, and utilizing the container's services.
-
What are some common EJB exceptions?
- Answer: Common exceptions include `EJBException`, `FinderException`, `CreateException`, `RemoveException`, and various checked exceptions related to database operations or transactions.
-
How do you handle transactions in EJBs?
- Answer: Transaction management is typically handled declaratively using annotations or programmatically using the UserTransaction interface.
-
What is the difference between a business interface and a home interface? (Older EJB versions)
- Answer: In older EJB versions, the home interface defined methods for creating and removing beans, while the business interface defined the bean's business methods. This distinction is less relevant in newer EJB versions.
-
What are some tools used for developing and deploying EJBs?
- Answer: Tools include IDEs like Eclipse or IntelliJ IDEA, application servers like WildFly, GlassFish, or JBoss EAP, and build tools like Maven or Gradle.
-
How can you test EJBs?
- Answer: EJBs can be tested using unit testing frameworks like JUnit, mocking frameworks like Mockito, and integration testing within the application server.
-
Explain the concept of passivation and activation in stateful session beans.
- Answer: Passivation is the process of storing the bean's state to secondary storage to free up resources. Activation is the process of restoring the bean's state from storage.
-
What are the different deployment options for EJBs?
- Answer: EJBs are typically deployed as part of an EAR (Enterprise Archive) file, which can contain multiple modules, including EJBs, WAR (Web Application Archive) files, and JAR (Java Archive) files.
-
How can you manage resources like databases connections in EJBs?
- Answer: Resources are typically managed using JNDI lookups to obtain connection pools provided by the application server.
-
What are the different scopes available for EJBs?
- Answer: The main scopes are application (for singletons) and conversation (for stateful session beans). Stateless session beans are not tied to a specific scope.
-
What is the purpose of the `@Local` and `@Remote` annotations?
- Answer: These annotations specify the type of interface exposed by the EJB: `@Local` for local access (within the same JVM), `@Remote` for remote access (across JVMs).
-
What is the role of the `@TransactionAttribute` annotation?
- Answer: This annotation specifies the transaction attributes for an EJB method, defining how it should interact with transactions.
-
How do you handle exceptions that occur within an EJB method?
- Answer: Exceptions should be handled gracefully, potentially logged, and appropriate responses (or exceptions) returned to the client.
-
What are some common performance tuning techniques for EJB applications?
- Answer: Techniques include optimizing database queries, using appropriate bean types, utilizing connection pooling efficiently, and optimizing code for performance.
-
Explain the difference between a deployment descriptor and annotations in EJB development.
- Answer: Both specify EJB metadata. Annotations are more concise and preferred in modern development, while deployment descriptors provide a more comprehensive and structured approach.
-
What are some security considerations when developing EJBs?
- Answer: Security considerations include authentication, authorization, data encryption, secure coding practices, and protection against common vulnerabilities.
-
How do you handle asynchronous operations in EJBs?
- Answer: Asynchronous operations are typically handled using Message-Driven Beans (MDBs), which process messages from JMS queues or topics.
-
Describe your experience with any application server used for deploying EJBs. (If applicable)
- Answer: (This answer should be tailored to the candidate's experience.) For example: "I have experience deploying EJBs to WildFly using Maven. I'm familiar with the deployment process, managing configurations, and troubleshooting deployment issues."
-
Explain a challenging situation you faced while working with EJBs and how you overcame it. (If applicable)
- Answer: (This answer should be tailored to the candidate's experience.) For example: "I encountered an issue with transaction management where a method was not rolling back correctly. By carefully reviewing the transaction attributes and logging the exception stack trace, I identified the problem was a missing annotation."
-
What are some common design patterns used with EJBs?
- Answer: Common patterns include Singleton, Facade, Data Access Object (DAO), and Service Locator patterns.
-
How do you debug EJB applications?
- Answer: Debugging techniques include using logging frameworks, remote debugging with IDEs, and analyzing application server logs.
-
What is your understanding of the EJB specification versions?
- Answer: The candidate should mention familiarity with major version differences (e.g., significant changes in persistence management, introduction of annotations, etc.).
-
How does EJB support distributed transactions?
- Answer: EJB supports distributed transactions through JTA (Java Transaction API) and participation in two-phase commit protocols.
-
What are the advantages of using annotations over XML deployment descriptors?
- Answer: Annotations provide a more concise and readable way to configure EJBs, reducing XML configuration boilerplate.
-
What is the role of the `@PostConstruct` and `@PreDestroy` annotations?
- Answer: `@PostConstruct` is executed after bean creation and initialization, while `@PreDestroy` is executed before the bean is destroyed by the container.
-
Explain the concept of pooling in EJB containers.
- Answer: Pooling reuses bean instances to reduce the overhead of bean creation and improve performance, particularly for stateless session beans.
-
How does EJB handle resource management?
- Answer: EJBs utilize the application server's resource management capabilities, including connection pooling and resource injection. The container handles the lifecycle and release of resources.
-
What is your understanding of the differences between EJB and Spring?
- Answer: EJBs are part of the Java EE specification, focusing on server-side components and container-managed services. Spring is a lightweight framework providing various features including dependency injection, aspect-oriented programming, and transaction management.
-
Are you familiar with any other technologies related to EJBs? (e.g., JMS, JTA, JPA)
- Answer: The candidate should demonstrate knowledge of technologies that integrate tightly with EJBs, such as JMS for messaging, JTA for transaction management, and JPA for persistence.
-
What are some alternative technologies to EJBs for building enterprise applications?
- Answer: Alternatives include Spring, Micronaut, Quarkus, and other frameworks and technologies that provide similar functionalities.
-
What are your salary expectations for this internship?
- Answer: (This should be a researched answer based on industry standards and location).
Thank you for reading our blog post on 'EJB Interview Questions and Answers for internship'.We hope you found it informative and useful.Stay tuned for more insightful content!