JPA Quiz - MCQ - Multiple Choice Questions

Welcome to our JPA (Jakarta Persistence API) quiz! In this blog post, we present a set of 10+ Multiple Choice Questions (MCQs) to test your knowledge of Jakarta Persistence API (JPA) concepts. 

JPA is a standard specification that defines the Java interfaces and annotations for object-relational mapping (ORM) in Java applications. Let's put your understanding of JPA to the test and enhance your skills in Java persistence.

Learn everything about JPA: JPA Tutorial - Java Persistence API


The answer and explanation of each question have given at the end of this post.

1. What is JPA? 

a) A programming language 
b) A database management system 
c) A specification for ORM in Java applications 
d) A scripting language 

2. Which of the following is true about JPA? 

a) It is specific to a particular database management system 
b) It provides a way to define database schemas 
c) It simplifies object-oriented programming in Java 
d) It is primarily used for front-end web development

3. Which JPA annotation is used to specify the primary key column in an entity class? 

a) @PrimaryKey 
b) @Id 
c) @GeneratedValue 
d) @Column

4. Which annotation is used to mark a class as an entity in JPA? 

a) @Entity 
b) @Table 
c) @Persistent 
d) @EntityClass

5. What is the purpose of the @JoinColumn annotation in JPA? 

a) To join multiple tables in the query 
b) To specify the foreign key column in a relationship 
c) To define a primary key column 
d) To create an index on a column

6. Which annotation is used to define a named query in JPA? 

a) @Query 
b) @NamedQuery 
c) @Named 
d) @NamedNativeQuery

7. Which annotation is used to map an enumerated type property in JPA? 

a) @Enumerated 
b) @Enum 
c) @EnumType 
d) @Type 

8. What is the purpose of the @Embedded annotation in JPA? 

a) To define a relationship between entities
b) To specify a primary key column 
c) To embed an object within an entity 
d) To create a composite index on multiple columns

9. Which annotation is used to specify a discriminator column for entity inheritance in JPA? 

a) @DiscriminatorColumn 
b) @Inheritance 
c) @Discriminant 
d) @Discriminator

10. What is the FetchType.LAZY attribute used for in JPA? 

a) To fetch associated entities eagerly 
b) To fetch associated entities lazily 
c) To specify the join type in a query 
d) To define the cascade type for an association

11. What is the purpose of JPA's PersistenceContext? 

a) To manage database connections 
b) To define database schemas 
c) To store managed entities and their state 
d) To execute database queries

Answers and Explanations

Question 1

Answer: 
c) A specification for ORM in Java applications.

Explanation: 
JPA is a standard specification that defines the Java interfaces and annotations for object-relational mapping (ORM) in Java applications. It provides a unified way to interact with relational databases using object-oriented programming concepts. 

Question 2

Answer: 
c) It simplifies object-oriented programming in Java. 

Explanation: 
JPA simplifies database access by abstracting away the complexities of JDBC (Java Database Connectivity) and providing an object-oriented approach to interacting with databases.

Question 3

Answer: 
b) @Id. 

Explanation:
The @Id annotation is used to specify the primary key column in an entity class. It marks a field or property as the identifier of the entity.

Question 4

Answer: 
a) @Entity. 

Explanation:
The @Entity annotation is used to mark a class as an entity in JPA. It indicates that the class should be treated as a persistent entity and mapped to a database table.

Question 5

Answer: 
b) To specify the foreign key column in a relationship. 

Explanation:
The @JoinColumn annotation is used to specify the foreign key column in a relationship. It allows developers to customize the column name, nullable behavior, and other attributes of the foreign key.

Question 6

Answer: 
b) @NamedQuery. 

Explanation:
The @NamedQuery annotation is used to define a named query in JPA. It allows developers to pre-define queries with a specific name and reuse them throughout the application.

Question 7

Answer: 
a) @Enumerated. 

Explanation:
The @Enumerated annotation is used to map an enumerated type property in JPA. It allows developers to specify how the enumeration values should be persisted in the database. 

Question 8

Answer: 
c) To embed an object within an entity. 

Explanation:
The @Embedded annotation is used to specify that an object should be embedded within an entity. It allows developers to create complex types by composing multiple attributes into a single embedded object.

Question 9

Answer: 
a) @DiscriminatorColumn. 

Explanation:
The @DiscriminatorColumn annotation is used to specify a discriminator column for entity inheritance in JPA. It allows developers to define a column that discriminates between different types of entities in an inheritance hierarchy.

Question 10

Answer: 
b) To fetch associated entities lazily. 

Explanation:
The FetchType.LAZY attribute is used to specify that associated entities should be fetched lazily, i.e., only when they are accessed for the first time.

Question 11

Answer: 
c) To store managed entities and their state. 

Explanation:
The PersistenceContext in JPA represents a cache of managed entities and their state. It is used to track changes made to entities and synchronize those changes with the database.

Conclusion

Congratulations on completing our JPA quiz! We hope you found it informative and enjoyed testing your knowledge of JPA concepts. 
Keep exploring JPA and practicing its various features and annotations to strengthen your skills in Java persistence.

Comments