❮ Previous Quiz
Next Quiz ❯
Welcome to our JPA (Jakarta Persistence API) quiz! In this blog post, we present a set of 20+ 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.Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills.
1. What does JPA stand for?
a) Java Persistence API
b) Java Programming API
c) Java Platform API
d) Java Processing API
Answer:
a) Java Persistence API
Explanation:
JPA stands for Java Persistence API. It is a specification for managing relational data in Java applications.
2. Which annotation is used to mark a class as an entity in JPA?
a) @Table
b) @Entity
c) @Persistence
d) @Data
Answer:
b) @Entity
Explanation:
The @Entity annotation is used to mark a class as an entity in JPA. This class will be mapped to a table in the database.
3. Which annotation is used to specify the primary key of an entity in JPA?
a) @PrimaryKey
b) @Id
c) @Key
d) @Primary
Answer:
b) @Id
Explanation:
The @Id annotation is used to specify the primary key of an entity in JPA.
4. Which annotation is used to map a field to a column in a database table in JPA?
a) @Column
b) @Field
c) @Data
d) @Map
Answer:
a) @Column
Explanation:
The @Column annotation is used to map a field in a JPA entity to a column in the database table.
5. What is the default fetch type for @OneToMany and @ManyToMany relationships in JPA?
a) EAGER
b) LAZY
c) IMMEDIATE
d) NONE
Answer:
b) LAZY
Explanation:
The default fetch type for @OneToMany and @ManyToMany relationships in JPA is LAZY, meaning the related entities are loaded only when accessed.
6. What is the purpose of the @GeneratedValue annotation in JPA?
a) To specify a generated value for a column
b) To specify a custom generator for primary keys
c) To specify that a field should not be persisted
d) To map a relationship between entities
Answer:
a) To specify a generated value for a column
Explanation:
The @GeneratedValue annotation in JPA is used to specify that the value of the primary key should be generated automatically, typically by the database.
7. Which strategy is used by default for @GeneratedValue in JPA?
a) AUTO
b) IDENTITY
c) SEQUENCE
d) TABLE
Answer:
a) AUTO
Explanation:
The default strategy for @GeneratedValue in JPA is AUTO, which allows the persistence provider to select an appropriate strategy for the specific database.
8. Which of the following annotations is used to specify a one-to-one relationship in JPA?
a) @OneToOne
b) @OneToMany
c) @ManyToOne
d) @ManyToMany
Answer:
a) @OneToOne
Explanation:
The @OneToOne annotation is used to specify a one-to-one relationship between two entities in JPA.
9. Which JPA annotation is used to specify a mapping to a join table?
a) @JoinTable
b) @JoinColumn
c) @MappedSuperclass
d) @Embedded
Answer:
a) @JoinTable
Explanation:
The @JoinTable annotation is used in JPA to specify a mapping to a join table in the database, typically used in many-to-many relationships.
10. What does the @Transient annotation do in JPA?
a) It maps a transient field to a database column
b) It indicates that a field should not be persisted to the database
c) It marks a field as the primary key
d) It specifies a temporary table
Answer:
b) It indicates that a field should not be persisted to the database
Explanation:
The @Transient annotation in JPA indicates that a field should not be persisted to the database and will not be included in the entity's state.
11. Which annotation is used to define a composite primary key in JPA?
a) @Id
b) @IdClass
c) @EmbeddedId
d) Both b) and c)
Answer:
d) Both b) and c)
Explanation:
In JPA, both @IdClass and @EmbeddedId annotations can be used to define a composite primary key.
12. What is the purpose of the EntityManager in JPA?
a) To manage database connections
b) To perform CRUD operations on entities
c) To handle transactions
d) All of the above
Answer:
d) All of the above
Explanation:
The EntityManager in JPA is responsible for managing database connections, performing CRUD operations, and handling transactions.
13. How do you start a transaction in JPA?
a) EntityManager.begin()
b) Transaction.begin()
c) EntityTransaction.begin()
d) EntityManagerFactory.begin()
Answer:
c) EntityTransaction.begin()
Explanation:
In JPA, you start a transaction using the EntityTransaction.begin() method.
14. Which method is used to persist an entity in JPA?
a) save()
b) persist()
c) merge()
d) store()
Answer:
b) persist()
Explanation:
The persist() method in JPA is used to persist an entity, making it managed and persistent in the database.
15. What does the merge() method do in JPA?
a) It saves a new entity
b) It updates an existing entity or saves a new one if it doesn't exist
c) It deletes an entity
d) It retrieves an entity from the database
Answer:
b) It updates an existing entity or saves a new one if it doesn't exist
Explanation:
The merge() method in JPA updates an existing entity or saves a new one if it doesn't already exist in the database.
16. Which annotation is used to define a named query in JPA?
a) @Query
b) @NamedQuery
c) @QueryDef
d) @JPQL
Answer:
b) @NamedQuery
Explanation:
The @NamedQuery annotation is used to define a named query in JPA, which can be referenced by its name when executing the query.
17. Which JPA feature allows for automatically generating schema definitions?
a) SchemaTool
b) SchemaGen
c) SchemaExporter
d) Schema Generation
Answer:
d) Schema Generation
Explanation:
JPA's Schema Generation feature allows for automatically generating database schema definitions based on entity mappings.
18. What is JPQL in JPA?
a) A Java-based query language for operating on entities
b) A SQL-based query language for operating on tables
c) A JSON-based query language
d) A procedural query language for stored procedures
Answer:
a) A Java-based query language for operating on entities
Explanation:
JPQL (Java Persistence Query Language) is a Java-based query language used in JPA to query and manipulate entity objects.
19. Which of the following is a valid JPQL query?
a) SELECT e FROM Employee e WHERE e.salary > 50000
b) SELECT * FROM Employee WHERE salary > 50000
c) SELECT e.name, e.salary FROM Employee e
d) SELECT e.name, e.salary FROM Employee
Answer:
a) SELECT e FROM Employee e WHERE e.salary > 50000
Explanation:
Option a) is a valid JPQL query. JPQL operates on entity objects and fields, unlike SQL, which operates directly on database tables and columns.
20. What is the default cascade type for relationships in JPA?
a) ALL
b) PERSIST
c) MERGE
d) NONE
Answer:
d) NONE
Explanation:
The default cascade type for relationships in JPA is NONE, meaning no cascading operations are performed unless explicitly specified.
Conclusion
Congratulations on completing our JPA quiz! We hope you found it informative and enjoyed testing your knowledge of JPA concepts. Learn everything about JPA: JPA Tutorial - Java Persistence API.
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills.
Keep exploring JPA and practicing its various features and annotations to strengthen your skills in Java persistence.
❮ Previous Quiz
Next Quiz ❯
Comments
Post a Comment
Leave Comment