Hibernate MCQ

Hibernate, a powerful Object-Relational Mapping (ORM) framework, has been a game-changer in the Java ecosystem. It offers a way to bridge the gap between object-oriented domain models and relational database systems. If you're starting with Hibernate or looking to test your foundational knowledge, this quiz is for you!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. What is Hibernate in Java?

a) Web framework
b) Database
c) ORM framework
d) A Java library for graphics

Answer:

c) ORM framework

Explanation:

Hibernate is an Object-Relational Mapping (ORM) framework that allows Java applications to interact seamlessly with relational databases.

2. Which interface is considered the primary interface in Hibernate?

a) SessionFactory
b) Configuration
c) Session
d) Transaction

Answer:

c) Session

Explanation:

The Session interface in Hibernate is the main interface used to interact with the database. It represents a single unit of work with the database.

3. What is HQL?

a) Hibernate Quality Language
b) Hibernate Queued Language
c) Hibernate Query Language
d) Hibernate Quick Language

Answer:

c) Hibernate Query Language

Explanation:

HQL stands for Hibernate Query Language. It is an object-oriented query language, similar to SQL, but operates on objects rather than directly on database tables.

4. Which of these is not a state of an object in Hibernate?

a) Persistent
b) Detached
c) Transitional
d) Transient

Answer:

c) Transitional

Explanation:

In Hibernate, object states include Transient, Persistent, and Detached, but not Transitional.

5. Which configuration file is used by Hibernate?

a) hibernate.xml
b) hibernate.cfg.xml
c) config-hibernate.xml
d) hibernate-config.xml

Answer:

b) hibernate.cfg.xml

Explanation:

Hibernate uses the hibernate.cfg.xml file for its configuration.

6. Which annotation is used to mark a class as an entity in Hibernate?

a) @EntityClass
b) @EntityBean
c) @HibernateEntity
d) @Entity

Answer:

d) @Entity

Explanation:

The @Entity annotation is used in Hibernate to mark a class as an entity, indicating that it's a JPA entity and should be mapped to a database table.

7. What does ORM stand for?

a) Object-Relational Mapping
b) Object-Related Model
c) Organized Relational Model
d) Object-Referenced Method

Answer:

a) Object-Relational Mapping

Explanation:

ORM stands for Object-Relational Mapping. It's a programming technique to map application objects to database tables.

8. Which method is used to store an object in a database in Hibernate?

a) save()
b) put()
c) store()
d) insert()

Answer:

a) save()

Explanation:

The save() method in Hibernate is used to store an object in the database.

9. Which of the following represents a unidirectional association in Hibernate?

a) One-to-One
b) One-to-Many
c) Many-to-One
d) All of the above

Answer:

d) All of the above

Explanation:

All the mentioned associations can be unidirectional in nature in Hibernate.

10. In which directory should the hibernate.cfg.xml file be placed?

a) /WEB-INF/classes/
b) /WEB-INF/
c) /src/
d) /lib/

Answer:

a) /WEB-INF/classes/

Explanation:

The hibernate.cfg.xml file should be placed in the /WEB-INF/classes/ directory.

11. What is the primary key annotated with in Hibernate?

a) @Key
b) @PrimaryKey
c) @Id
d) @UniqueKey

Answer:

c) @Id

Explanation:

In Hibernate, the @Id annotation is used to specify the primary key of an entity.

12. Which method is used to start a transaction in Hibernate?

a) startTransaction()
b) begin()
c) start()
d) initiate()

Answer:

b) begin()

Explanation:

The begin() method of the Session interface is used to start a transaction in Hibernate.

13. What is the Cache used for in Hibernate?

a) Data validation
b) Reducing the number of database hits
c) Data transformation
d) Generating queries

Answer:

b) Reducing the number of database hits

Explanation:

Cache in Hibernate is primarily used to reduce the number of database hits, thereby improving performance.

14. Which of these is not a type of cache in Hibernate?

a) First-level cache
b) Second-level cache
c) Query-level cache
d) Session-level cache

Answer:

d) Session-level cache

Explanation:

Hibernate provides a First-level cache, Second-level cache, and a Query-level cache. Session-level cache isn't a type of cache in Hibernate.

15. Which API is used for criteria queries in Hibernate?

a) JDBC
b) JPA Criteria
c) HQL
d) SQL

Answer:

b) JPA Criteria

Explanation:

JPA Criteria API is used for creating type-safe and dynamic queries in Hibernate.

By now, you should have a clearer understanding of your current knowledge on Hibernate. This ORM framework has a lot to offer, and mastering it can be immensely rewarding for Java developers, especially when dealing with complex database operations. We encourage you to delve deeper into the vast world of Hibernate and continue honing your skills. Happy coding!



Comments