JPA MCQ Test
20 random JPA MCQs with answers: entities, the EntityManager, persistence context, entity states, relationships, fetching, JPQL and transactions.
- 20 random questions
- 20 minutes
- 27 in the question bank
No sign-up needed. Answers and explanations are shown at the end.
About this test
JPA questions separate people who use an ORM from people who understand it. Expect questions on entity states, what the persistence context does, lazy vs eager fetching and how relationships are mapped.
The same rules apply when you use Spring Data JPA, so this test pays off in Spring Boot interviews too.
Each attempt picks new questions at random from the question bank. You can go back and change answers until you finish or the timer runs out. At the end you see your score, the right answer and a short explanation for every question, and the lessons to revise for the ones you missed.
Sample questions with answers
1. What is JPA?
- A specification (API) for object-relational mapping
- A database
- A Spring module
Show answer
A. A specification (API) for object-relational mapping — Hibernate is the most popular implementation.
2. Where does persistence.xml live?
- src/main/resources/META-INF/
- The project root
- src/main/java
Show answer
A. src/main/resources/META-INF/ — The unit name must match createEntityManagerFactory.
3. Which enum mapping is safe when constants are reordered?
- ORDINAL
- Either
- STRING
Show answer
C. STRING — STRING stores the name.
4. A managed entity whose field changes inside a transaction…
- Is ignored
- Needs em.update()
- Gets an UPDATE at commit automatically
Show answer
C. Gets an UPDATE at commit automatically — Dirty checking compares with a snapshot.
5. Why does a second em.find for the same id run no SQL?
- It's served from the persistence context (first-level cache)
- The second-level cache is on by default
- find never runs SQL
Show answer
A. It's served from the persistence context (first-level cache) — One managed object per row per context.
6. Which side owns a bidirectional one-to-many?
- The @OneToMany side
- The @ManyToOne side holding the foreign key
- Both
Show answer
B. The @ManyToOne side holding the foreign key — mappedBy marks the inverse side.
7. JPQL queries use…
- Table and column names
- Entity and field names
- JSON paths
Show answer
B. Entity and field names — select c from Course c where c.price < :max.
8. Which strategy stores a hierarchy in one table?
- JOINED
- SINGLE_TABLE
- TABLE_PER_CLASS
Show answer
B. SINGLE_TABLE — A discriminator column tells rows apart.
9. What does @Version provide?
- Optimistic locking: stale updates fail
- A database backup
- Faster reads
Show answer
A. Optimistic locking: stale updates fail — The version is checked in the UPDATE's WHERE clause.