Java Development: Key Concepts and Best Practices
ArrayList vs. TreeSet
When are the differences between an ArrayList and a TreeSet?
A: An ArrayList allows duplicate elements and maintains the insertion order, while a TreeSet does not allow duplicate elements and does not maintain the insertion order.
Object-Relational Mapping (ORM)
Explain what Object-Relational Mapping is.
A: It is a sophisticated mechanism for mapping Java objects in memory to tables in a relational database. It allows you to persist objects, enables complex queries, and each instance of an object corresponds to a record in the database.
Enterprise JavaBeans (EJBs)
When should EJBs be used in an application?
A:
- When the application must be scalable, i.e., when it has a large number of users or when the application is spread across multiple machines.
- When transactions are necessary to ensure data integrity.
- When applications have a large number of clients.
Session Beans vs. MessageDrivenBeans
Explain what a Session Bean and a MessageDrivenBean are.
A:
- Session Beans are not persistent. They are often used to manage interactions between entity beans and represent a flow of work or a business process.
- MessageDrivenBeans are used with other applications through a messaging service (JMS). They receive messages asynchronously.
Persistence Unit in JPA
Explain what a unit of persistence in JPA is.
A: It is a set of classes mapped to a relational database. This mapping is done through a deployment descriptor file called persistence.xml
, which is stored in the META-INF
folder and can contain the configuration of multiple persistence units.
CRUD Operations
CRUD is a set of operations that can be performed in a data repository. What does the acronym CRUD stand for?
A:
- Create:
INSERT
- Retrieve (Read):
SELECT
- Update:
UPDATE
- Delete:
DELETE
Plain Old Java Objects (POJOs)
Explain what a POJO is.
A: Plain Old Java Objects are objects following a simplified design, as opposed to EJBs, for example. A JavaBean is a POJO following rigid definitions of structure (a default constructor without arguments and methods following the standard getters and setters for its attributes).
Persistence Context
Explain the context of persistence (Persistence Context).
A: It is an environment (meeting) that involves the set of instances of entities managed by the Entity Manager.
Stateful vs. Stateless Session Beans
Explain the differences between a Stateful Session Bean and a Stateless Session Bean.
A:
- Stateful Session Bean: Maintains a conversational state between calls from a client to the server. It cannot maintain pools of instances.
- Stateless Session Bean: Does not maintain a conversational state between the client and the server. It maintains pools of instances.
Java Development: Q&A
Implementing Listeners
1. What import is required to implement Listeners?
a. import javax.swing.*;
b. import java.awt.*;
c. import javax.awt.*;
d. import java.awt.event.*;
Loading JDBC Drivers
2. What is the method used to load the JDBC driver?
a. Connection.createStatement()
b. Class.forName()
c. Class.loadDriver
d. Connection.DriverManager()
Local Session Bean Methods
3. What annotation should be used in a Session Bean’s interface to define that its methods will be available locally?
a. @Local
b. @Stateless
c. @Remote
d. @Stateful
EJB Servers
4. Which of the servers below is an example of an EJB server used in the business layer?
a. Internet Information Service
b. PersistServer
c. Java Business Application Server
d. JBoss
Persistent Classes
5. What annotation should be used to declare a class as a persistent class?
a. @AutoNumber
b. @Entity
c. @Identity
d. @GeneratedValue
Session Bean Types
6. What types of Session Beans are there?
a. EntityBeans and MessageDriven
b. Persist and Transient
c. Stateful and Stateless
d. Temporary and Permanent
Undoing Database Changes
7. What command is used to undo changes in the database?
a. em.getTransaction().save()
b. em.getTransaction().commit()
c. em.getTransaction().close()
d. em.getTransaction().rollback()
EJB Server Layer
8. In which layer is the EJB server?
a. Client Layer
b. Business Layer
c. EIS Layer
d. Web Layer
TreeSet Characteristics
9. Regarding the TreeSet collection, we can say that:
a. It is an ordered set of elements in which there are repeated elements.
b. It is a set of unordered elements in which there are no repeated elements.
c. It is a set of ordered elements. There are repeated elements.
d. It is a set of unordered elements. There are repeated elements.
TreeSet Characteristics (Simplified)
10. Regarding the TreeSet, we can say:
a. It is a set of ordered items. There are no repeated elements.
b. It is a set of unordered elements. There are no repeated elements.
c. It is a set of ordered items. There are repeated elements.
d. It is a set of unordered elements. There are repeated elements.
Sorting a List
11. What is used to sort a list?
a. Collections.sort(list)
b. Collections.random(list)
c. Collections.shuffle(list)
d. Collections.retainAll(list)
persistence.xml
Configuration
12. What does the command <property name="toplink.ddl-generation" value="create-tables" />
declared in persistence.xml
cause?
a. Creates a new table in the database, overwriting the old one.
b. Creates a new table in the database when it does not exist; if it exists, it simply updates it.
c. In all runs, it is just a disclosure statement.
d. Generates a script of the table to update it in the database.
Retrieving Records as Objects
13. What method is used to return a record from the database and turn it into an object, based on its target entity?
a. Entity e = em.find(Entity.class, key)
b. Entity e = em.findEntity(Entity.class, key)
c. Entity e = em.find(key, Entity.class)
d. Entity e = em.localFind(Entity.class, key)
Transient Attributes
14. What annotation is used to define an attribute that is not stored in the database?
a. @Id
b. @Managed
c. @Transient
d. @Removed
persistence.xml
Unit Name
15. What does the command <persistence-unit name="persistencia" />
declared in persistence.xml
cause?
a. Defines the kind of persistence unit.
b. Defines the name of the persistence unit to be used by the method createEntityManagerFactory()
, which contains the settings of the database.
c. Defines the name of the persistence unit to be used by the method createEntityManager()
, which contains the settings of the database.
d. Defines the name of the persistence unit to be used by the persistent class.
Deleting Entities
16. What command is used to delete an entity?
a. em.removeEntity(e)
b. em.elimina(e)
c. em.remove(e)
d. em.eliminaEntity(e)
Stateless Session Bean Behavior
17. A Stateless Session Bean does not maintain a state of conversation with the client; the state is only maintained during the call, then it is withdrawn:
a. Stateless Session Bean
b. Stateful Session Bean
c. Entity Session Bean
d. MessageDriven Session Bean
Creating Entity Manager Factories
18. Which of the following commands creates a factory of entity managers?
a. EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
b. EntityManager emf = emf.createEntityManager();
c. EntityTransaction trans = em.getTransaction();
d. Student a = new Student();
Database Access with JPA
19. What options do we have to access a database using JPA?
a. Native SQL
b. Native SQL and JPA QL
c. Native SQL, JPA QL, and the find
method
d. Native SQL, JPA QL, the find
method, and the executeQuery
method
Justifications for Using EJB
20. Which of the following statements is NOT a justification for the use of EJB?
a. When the application should be implemented in three layers, facilitating maintenance and distribution.
b. When the application should be scalable, that is, have a growing number of users, and the application can be distributed on multiple machines.
c. When transactions are necessary to ensure data integrity.
d. When the application has a high number of customers. As the business logic is on the server-side components, maintenance is facilitated.
Entity Lifecycle States
21. What are the states of the lifecycle of an entity?
a. New, Managed, Persisted, Refreshed
b. New, Managed, Removed, Detached
c. New, Removed, Recalled, Committed
d. Merged, Removed, Persisted, New
Entity Manager Interface Methods
22. What are the methods of the Entity Manager interface?
a. createQuery()
, createNamedQuery()
, delete()
, cancelTransaction()
b. begin()
, create()
, refresh()
, delete()
c. commit()
, rollback()
, merged()
, detached()
d. refresh()
, merge()
, persist()
, remove()
Detached Entity State
23. Within the persistence context, when a person is NOT being controlled by the EntityManager, meaning changes in the entity will not be reflected in the database, we say it is in the state:
a. DETACHED
b. MANAGED
c. NEW
d. CONTROLLED