Hibernate Online Test - MCQ Questions

Welcome to the Hibernate Online Test! We will present 30 MCQs (Multiple-Choice Questions) to test your knowledge of the Hibernate ORM framework.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What is Hibernate in Java?

a) A web framework
b) A database
c) An ORM framework
d) A compilation tool

2. Which file is commonly used for Hibernate configuration?

a) hibernate.cfg.xml
b) hibernate.properties
c) config.hibernate.java
d) Both a and b

3. What is the purpose of the @Entity annotation in Hibernate?

a) To create a table in the database
b) To drop a table in the database
c) To mark a class as an entity bean
d) To synchronize the database schema

4. What does the @Table annotation do in Hibernate?

a) Specifies the table name for the entity
b) Deletes a table in the database
c) Updates the schema of the existing table
d) None of the above

5. What is a Session in Hibernate?

a) A single-threaded object that represents the connection to the database
b) A multi-threaded object that represents a cache of the database
c) An interface that defines a unit of work
d) Both a and c

6. How do you obtain a SessionFactory in Hibernate?

a) By configuring SessionBuilder
b) Using SessionFactoryBuilder.build()
c) Using new SessionFactory()
d) By calling buildSessionFactory() on a Configuration object

7. What is the role of the @Id annotation in Hibernate?

a) To define a sorting mechanism
b) To specify a transient property
c) To designate a field as the primary key of an entity
d) To mark a method as synchronized

8. What does the @GeneratedValue annotation specify in Hibernate?

a) The value is generated by a user-defined method
b) The primary key value is generated automatically
c) The value is generated by the Hibernate framework
d) Both b and c

9. Which Hibernate annotation is used to define a one-to-many relationship?

a) @OneToMany
b) @ManyToOne
c) @ManyToMany
d) @OneToOne

10. What is HQL?

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

11. Which is a core advantage of Hibernate?

a) It simplifies database migrations.
b) It provides detailed control over the database behavior.
c) It supports polymorphism and inheritance.
d) It directly allows raw SQL execution for complex queries.

12. What is the purpose of the @Column annotation in Hibernate?

a) To specify the constructor of an entity
b) To specify the SQL column that a property maps to
c) To specify the validation rules for a property
d) To change the datatype of a column in the database

13. What does the @Transient annotation do?

a) Marks a field to be transient to Hibernate
b) Marks a field to be saved by Hibernate
c) Marks a field to be indexed
d) Marks a field to be validated

14. Which strategy does Hibernate use to load the associated collections and entities by default?

a) Eager fetching
b) Lazy fetching
c) Concurrent fetching
d) Pre-fetching

15. What is the difference between get() and load() methods in Hibernate?

a) get() returns the object by fetching it from the database, whereas load() might return a proxy without hitting the database
b) get() always hits the database, whereas load() checks the cache first
c) get() throws an exception if the object is not found, whereas load() returns null
d) There is no difference between the two

16. What is a NamedQuery in Hibernate?

a) A way to name a database query
b) A query that is compiled once and stored for later use
c) A query stored in a mapping file rather than in the code
d) All of the above

17. How do you enable second-level caching in Hibernate?

a) By marking the entity class with @Cacheable and specifying a cache strategy
b) By using the @Cache annotation on each method
c) By setting hibernate.cache.use_second_level_cache to true in the configuration
d) Both a and c

18. What does the @Version annotation do in Hibernate?

a) It indicates the version of Hibernate to be used
b) It facilitates optimistic concurrency control
c) It specifies the version number of the database
d) It tracks changes to entities

19. Which transaction management type does Hibernate natively support?

a) JTA
c) Both a and b
d) Hibernate does not support transaction management

20. How can you improve the performance of Hibernate applications?

a) By using indexing
b) By optimizing query performance and using caching
c) By increasing the session timeout
d) All of the above

21. What does the @NaturalId annotation do in Hibernate?

a) Marks an ID as a primary key
b) Provides a natural key alternative to the primary key
c) Automatically generates IDs using the database auto-increment feature
d) None of the above

22. What is the significance of the cascade attribute in entity relationships?

a) It ensures that changes are cascaded from parent to child entities
b) It automatically deletes orphaned entities
c) It is used to validate entity relationships
d) Both a and b

23. Which Hibernate feature enables writing database-independent queries?

b) Native SQL
c) Criteria API
d) HQL (Hibernate Query Language)

24. In Hibernate, what is the effect of setting the hibernate.show_sql property to true?

a) It formats the SQL that Hibernate generates.
b) It shows the SQL in the console for debugging purposes.
c) It saves all generated SQL to a file for review.
d) It allows Hibernate to generate SQL only on demand.

25. What Hibernate caching mechanism is associated with the lifecycle of a session?

a) First-level cache
b) Second-level cache
c) Query cache
d) Connection pool

26. What does the @Fetch annotation do?

a) It specifies the fetching strategy for a collection or association.
b) It determines how data should be loaded from the database.
c) It triggers the fetching of data when a session is opened.
d) It prioritizes the loading of certain properties over others.

27. How do you map an inheritance hierarchy in Hibernate using the 'table per class' strategy?

a) @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
b) @Inheritance(strategy = InheritanceType.JOINED)
c) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
d) @DiscriminatorColumn(name = "type")

28. What is the function of the @DiscriminatorColumn annotation in a single table inheritance strategy?

a) It denotes the column used to distinguish among different entity types.
b) It specifies the primary key for the inheritance table.
c) It determines which subclass is associated with which database row.
d) It provides a default column name for all child entities.

29. How is a lazy-loaded association initialized in Hibernate when accessed for the first time?

a) By using a proxy that fetches the data when any method of the associated object is called.
b) Automatically upon session creation.
c) By calling the initialize() method manually on the association.
d) Through a configuration setting that must be enabled at startup.

30. How can you map an enum type in Hibernate with a custom column specifying the enum code?

a) Using the @Enumerated annotation with EnumType.ORDINAL
b) Using the @Enumerated annotation with EnumType.STRING
c) Using the @Enum annotation
d) By implementing a custom Hibernate UserType