38 Spring and Hibernet MCQ
38 Spring and Hibernet MCQ
Sr.No
Project Name YouTube Link
11 Tour and Travel System Project version 3.0 https://youtu.be/Dm7nOdpasWg?si=P_Lh2gcOFhlyudug
12 Gym Management system Project https://youtu.be/J8_7Zrkg7ag?si=LcxV51ynfUB7OptX
13 Online Driving License system Project https://youtu.be/3yRzsMs8TLE?si=JRI_z4FDx4Gmt7fn
14 Online Flight Booking system Project https://youtu.be/m755rOwdk8U?si=HURvAY2VnizIyJlh
15 Employee management system project https://youtu.be/lD1iE3W_GRw?si=Y_jv1xV_BljhrD0H
16 Online student school or college portal https://youtu.be/4A25aEKfei0?si=RoVgZtxMk9TPdQvD
17 Online movie booking system project https://youtu.be/Lfjv_U74SC4?si=fiDvrhhrjb4KSlSm
18 Online Pizza Delivery system project https://youtu.be/Tp3izreZ458?si=8eWAOzA8SVdNwlyM
19 Online Crime Reporting system Project https://youtu.be/0UlzReSk9tQ?si=6vN0e70TVY1GOwPO
20 Online Children Adoption Project https://youtu.be/3T5HC2HKyT4?si=bntP78niYH802I7N
pg. 4 contact us on 8007592194 / 9284926333 www.codewitharrays.in
MCQ on Spring and Hibernate
Q1. Robert is working on a web application, where Spring is being used. He wants to apply
prototype scope in the bean named as ‘Person’. Which one of the following is the correct option
where the ‘Person’ bean works within the prototype scope :
9 4
(D) <bean id="person" class="com.test.Person" Scope="prototype"/>
21
59
07
A1. B is correct. Option ‘A’ and ‘D’ has syntactical errors. Option ‘C’ has PrototypeScope which is not
80
Q2. Spring framework offers us multiple ways to define the scope of a bean. Which one of the
ys
(A) @RequestScope
w
(C) @Scope("request")
public class Product {
// some properties & methods
}
Q3. Select the option that is not a correct approach to create bean life cycle methods in Spring
framework?
4
(D) By using XML configuration
9
21
59
07
A3. C is correct. Spring doesn’t provide any class to extend in order to create life cycle methods, but
it provides interfaces.
80
Q4. Which interface out of the following options will you use to perform destruction of beans in the
.in
(A) InitializingBean
ith
w
de
(B) PostConstruct
co
(C) DisposableBean
(D) PreDestroy
A4. C is correct. Spring allows your bean to perform destroy callback method by implementing the
DisposableBean interface. Option B & D are the annotations, not the interface.
Q5. Who is capable of maintaining a registry of different beans and their dependencies in a Spring
based web application?
(A) BeanFactory class
A5. B is correct. A BeanFactory is the interface which is capable of maintaining a registry of different
beans and their dependencies.
9 4
21
Q6. Select the incorrect statement about BeanFactory in Spring Framework?
59
07
(A) BeanFactory holds bean definitions
80
.in
A6. D is correct. BeanFactory does not support the Annotation-based dependency injection, whereas
ApplicationContext, a superset of BeanFactory does.
Q7. Four annotations given below, are used in Spring Boot based application. Which one is the
annotation of Spring Boot that is an alternative to Spring’s standard @Configuration annotation?
(A) @EnableAutoConfiguration
(B) @SpringBootConfiguration
(C) @ConfigurationProperties
(D) @ConfigurationPropertiesScan
Q8. What is the purpose of @SpringBootConfiguration annotation in a Spring Boot web application?
9 4
21
(B) scans on the package where the application is located
59
07
(C) enables Spring Boot’s auto-configuration mechanism
80
A8. A is correct. @SpringBootConfiguration enables registration of extra beans in the context or the
ith
Q9. Johnson is a developer. He is working in a Hibernate based application. He defines a class, called
de
‘InvoiceId’. The fields ‘category’ and ‘name’ will represent a unique InvoiceId as below:
co
@Embeddable
public class InvoiceId implements Serializable {
Which of the following option represents that the ‘Invoice’ entity has a composite key?
(A) @Entity
public class Invoice {
@Id
@Embedded
private InvoiceId id;
private String description;
4
private Double amount;
9
21
//standard getters and setters
}
59
07
(B) @Entity
80
@EmbeddedId
private InvoiceId id;
ys
}
w
de
(C) @Entity
co
(D) @Entity
public class Invoice {
@EmbeddableId
private InvoiceId id;
private String description;
private Double amount;
//standard getters and setters
}
A9. B is correct. @EmbeddedId is the correct annotation to represent that an entity has a composite
key.
Q10. Suppose you are working on a Hibernate-based application. You have two classes as below.
You want to create a table into the database using hibernate ORM concept.
4
private Name name;
9
private double salary;
21
// getters & setters
59
}
07
public class Name {
80
}
ra
ar
ith
w
de
You want to create a table named ’employee’ with 4 columns: Id, firstName, lastName, salary. Which
co
option is correct to create the aforesaid table. Assume that table & column names are not case-
sensitive.
@Embeddable
public class Name {
private String firstName;
private String lastName;
// getters & setters
}
(B) @Entity
public class Employee {
@Id
private int id;
@Embedded
private Name name;
private double salary;
// getters & setters
}
49
@Embedded
21
public class Name {
59
private String firstName;
private String lastName; 07
// getters & setters
80
}
.in
ys
(C) @Entity
ra
@Embedded
w
@Entity
public class Name {
private String firstName;
private String lastName;
// getters & setters
}
(D) @Entity
public class Employee {
@Id
private int id;
@Embeddable
private Name name;
private double salary;
// getters & setters
}
@Entity
public class Name {
private String firstName;
private String lastName;
// getters & setters
}
9 4
A10. C is correct. In Option A , @Entity is missing in Employee class. In Option B, @Embedded is
21
disallowed at class level and In Option D, @Embeddable is disallowed at field level.
59
Q11. Mary is working in a Hibernate based application as a web-application developer. She has two
07
entities in her application: Student & Address. She is implementing a relation where One student can
80
have multiple addresses and one address can accommodate multiple students in a bidirectional
relationship. Mary doesn’t want hibernate to create one additional table. Which of the following
.in
annotation and its attribute will she use to fulfill the given requirement?
ys
ra
ar
A11. C is correct. As per the problem statement, only @OneToMany or @MantToOne can be used.
Hence, Options A & D are incorrect. MappedBy attribute is disallowed in @ManyToOne, that makes
Option B also incorrect. Hence, option C is correct. If you wish to learn entity relationship concept in
detail, kindly visit a separate article on Entity Relationship In JPA/Hibernate/ORM.
Q12. In the context of Access types in Hibernate, if @Id is located at the getter method of an entity,
what does it mean?
(A) Entity access behavior is Field Access
(D) From the given information, we can't determine the Access behavior
A12. B is correct. If we apply @Id on the getter method of an entity/class, it means property access
behavior is enabled. If we apply @Id on field, it means field access behavior is enabled.
9 4
21
Q13. Suppose you are working on a web application that uses Hibernate as an ORM tool. At which
level/(s) @Access is allowed to be used in an Entity?
59
07
80
A13. D is correct. @Access can be used at all three levels : Field, Method & Class.
Q14. In JPA, which one of the following annotation converts the date and time values from Java
object to compatible database type and retrieves back to the application.
(A) @Timestamp
(B) @Time
(C) @Temporal
(D) @Date
A14. C is correct. @Temporal annotation converts the date and time values from Java object to
compatible database type and retrieves back to the application.
Q15. Jacob is working on a web application where JPA is being used in data layer. He wrote a POJO
class and wants this class to work as an entity to implement the ORM concept. What are the
minimum required annotations he must use at the class to create a table in the database?
9 4
21
(B) @Table, @Entity, @Id
59
07
(C) @Column, @Entity, @Table
80
.in
A15. A is correct. In order to create a table in the database, @Entity & @Id annotations are
mandatory, otherwise JPA will throw an exception.
w
de
(A) Session
(B) Request
(C) Prototype
(D) Singleton
A16. D is correct. A bean has singleton scope by default, whether we declare it in the bean definition
or not.
Q17. Robin is working in a Spring based web application. He declares the scope of a bean by using
annotations. Select the option which is incorrect in declaring the scope.
9 4
21
(A) @Component
@Scope("request")
59
public class Product { 07
//some methods and properties
80
}
.in
(B) @Component
ys
@Scope("session")
ra
}
w
de
(C) @Component
co
@Sessionscope
public class Product {
//some methods and properties
}
(D) @Component
@SessionScope
public class Product {
//some methods and properties
}
A17. C is correct. Option C has incorrect annotation. It should be ‘@SessionScope’ in place of
‘@Sessionscope’. All other options are correct.
Q18. Suppose you are working on a Spring based Web Application. Generally, there are three ways
to implement the core features of Spring framework in your application:
Which option is correct if you want to define Bean Lifecycle methods in your application:
9 4
21
(C) Option (2) & Option (3)
59
07
(D) All three
80
.in
ys
A18. D is correct. There are three ways to define bean life cycle methods: Annotation or XML
ra
(C) First default method and then custom method will be called
(D) First custom method and then default method will be called
9 4
21
59
A20. C is correct. Default methods provided by Spring always take precedence in a bean life cycle.
07
Q21. There are two classes ‘Product’ and ‘ProductCategory’ in a Spring based Project. You want to
80
inject ProductCategory into Product class by means of constructor injection. Which of the below
.in
(A) @Component
ith
@Autowired
de
(B) @Component
public class Product {
private int id;
private ProductCategory category;
public Product(ProductCategory category) {
this.category = category;
}
public ProductCategory getCategory() {
return category;
}
@Autowired
public void setCategory(ProductCategory category) {
this.category = category;
}
}
(C) @Component
public class Product {
94
private int id;
21
private ProductCategory category;
59
@Autowired
public Product(ProductCategory category) {
07
this.category = category;
80
}
public ProductCategory getCategory() {
.in
return category;
ys
}
ra
this.category = category;
ith
}
w
}
de
co
(D) @Component
public class Product {
private int id;
private ProductCategory category;
public Product(ProductCategory category) {
this.category = category;
}
@Autowired
public ProductCategory getCategory() {
return category;
}
public void setCategory(ProductCategory category) {
this.category = category;
}
}
A21. C is correct. @Autowired annotation will be applied on constructor as the question is talking
about constructor injection.
Q22. Suppose you are working on a Student library system which is developed in the Spring
framework. There are four classes as shown below.
@Component
public class Student {
4
private int id;
9
private Address address;
21
}
59
@Component
public interface Address {
07
// some fields & Methods
80
}
.in
@Component
public class PermanentAddress implements Address {
ys
}
ar
@Component
ith
}
co
You want to inject dependency of PermanentAddress class into Student class. Which option
represents the correct use of dependency injection?
(A) @Component
public class Student {
private int id;
@Autowired
private Address address;
}
(B) @Component
public class Student {
private int id;
@Autowired
@Qualifier("PermanentAddress")
private Address address;
}
(C) @Component
public class Student {
private int id;
@Autowired
@Qualifier
private Address address;
}
9 4
21
(D) @Component
59
public class Student { 07
private int id;
80
@Autowired
@Qualifier("permanentAddress")
.in
}
ra
ar
ith
w
A22. D is correct. Spring container by default considers bean name same as the class name, but with
de
the first letter in lower case. In order to get more idea on @Qualifier, visit the article on @Qualifier
co
Annotation.
Q23. Which option is true for the role of BeanFactory in Spring Framework:
A23. A is correct. Options B, C, D are the roles of ApplicationContext, not the BeanFactory.
Q24. Select the option which is true about BeanFactory & ApplicationContext in the Spring
Framework:
4
(B) An ApplicationContext is a complete superset of a BeanFactory
9
21
59
(C) BeanFactory builds on top of the ApplicationContext.
07
80
A24. B is correct. According to Spring Framework documentation, option B is correct. For more
details, kindly refer the Spring Official Documentation.
ith
w
(A) @SpringBootConfiguration
(B) @EnableAutoConfiguration
(C) @Configuration
(D) @ComponentScan
A25. A is correct. @SpringBootConfiguration is not the part of this combination, but the
@Configuration is. For more details, visit @SpringBootApplication annotation.
Q26. Consider the below code where the ‘Product’ entity has ‘Category’ as @EmbeddedId and other
fields related to a product. A ‘Category’ tells Hibernate that the ‘Product’ entity has a composite key.
@Entity
public class Product implements Serializable {
@EmbeddedId
private Category id;
private String name;
//getters & setters
}
9 4
21
What will be the structure of the Person entity in the context of the above composite key?
59
07
80
(A) @Embedded
public class Category implements Serializable {
.in
}
ith
w
(B) @Component
de
(C) @Entity
public class Category implements Serializable {
private String name;
private String description;
//getters & setters
}
(D) @Embeddable
public class Category implements Serializable {
private String name;
private String description;
//getters & setters
}
A26. D is correct. We represent a composite primary key in Hibernate by using the @Embeddable
annotation on a class.
Q27. In a Hibernate based application, in order to interact with the database, we make use of various
methods provided by EntityManager API. Which statement is false in the context of these methods?
9 4
(A) We can make use of the persist() method in order to have an object associated w
21
59
(B) We can use the findReference() method, if we just need the reference to the ent
07
80
(C) We can use the detach() method to detach an entity from the persistence context
.in
ys
(D) We can use the find() method to retrieve an object from the database.
ra
ar
ith
w
de
A27. B is correct. If we just need the reference to the entity, we can use the getReference() method,
not the findReference().
co
Q28. In order to access entity attributes, we implement access strategies in Hibernate. Which type/(s)
of Access strategy/(ies) is allowed in Hibernate?
A28. D is correct. There are two types of access strategies allowed in Hibernate : field-based access
and property-based access.
Q29. Sophia is using Spring Data JPA for the data layer implementations in her web application. She
wrote a custom method in the repository. She is using ‘update’ query to implement the functionality
of the method. Which one of the following annotation she should use on that method?
(A) @Updating
9 4
21
(B) @Updated
59
07
(C) @Modifying
80
.in
(D) @Modified
ys
ra
ar
ith
A29. C is correct. Only @Modifying annotation is available. All others are incorrect.
w
(D) It provides declarative support for caching, validation, transaction, and forma
A30. C is correct. Apart from Java-based annotations, it also offers XML based configuration
options. Hence option ‘C’ is the right answer.
9 4
21
59
A31. C is correct. The Spring Core Container is the main component of the Spring framework, which
07
is responsible for managing the application’s beans and their dependencies.
80
A32. B is correct. The BeanFactory in Spring is an interface that provides a simple way to retrieve
bean instances.
Q33. What is the main difference between ApplicationContext and BeanFactory in Spring?
9 4
21
(A) To define a bean in the Spring application context
59
07
(B) To inject dependencies into a bean
80
.in
A34. B is correct. The @Autowired annotation in Spring is used to inject dependencies into a bean,
co
(A) A design pattern that allows for greater control over an application's objects
(C) A method for controlling an application's flow of control from the inside out
(D) The term 'Inversion of Control' is not used in Spring
A35. B is correct. Inversion of Control (IoC) is a technique for delegating control of the application to
a central authority, such as the Spring IoC container. The container creates and manages the objects
and their dependencies, allowing the objects to focus on their specific responsibilities.
Q36. Which of the following caching strategies in Hibernate stores data in a cache that can be
accessible across different sessions?
9 4
21
(B) First-level cache
59
07
(C) Second-level cache
80
.in
A36. C is correct. The first-level cache is related to a single session and is used to cache data within
that session only. The second-level cache in Hibernate caches data across different sessions. It stores
w
de
the cached data in a shared cache region. It allows multiple sessions to access the same data without
hitting the database. The query cache caches the results of queries, and the collection cache caches
co
Q37. You are working in a Spring Boot application that needs to connect to a relational database.
You want to configure the data source properties, such as URL, username, and password, in the
application.properties file. Which of the following is the correct way to configure the data source
properties in the application.properties file for an H2 in-memory database?
(A)
jdbc.url=jdbc:h2:mem:testdb
jdbc.username=admin
jdbc.password=admin123
(B)
db.datasource.url=jdbc:h2:mem:testdb
db.datasource.username=admin
db.datasource.password=admin123
(C)
spring.db.datasource.url=jdbc:h2:mem:testdb
spring.db.datasource.username=admin
spring.db.datasource.password=admin123
(D)
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=admin
spring.datasource.password=admin123
9 4
21
59
A37. D is correct . In a Spring Boot application, you typically configure data source properties in the
07
application.properties or application.yml file. Spring Boot uses the spring.datasource prefix to
identify data source-related properties.
80
Q38. In a spring application there are four classes some code segments are defined in the given
.in
options select the one which would contain the business logic.
ys
ra
ar
(A)
ith
@PostMapping("/product")
de
return productService.saveProduct(product);
}
}
(B)
public class ProductService {
public Product saveProduct(Product product) {
if (product.getPrice() > 0) {
return productRepository.save(product);
} else {
throw new IllegalArgumentException("Price must be greater than zero");
}
}
}
(C)
public interface ProductRepository extends JpaRepository<Product, Long> {
// Data access methods
}
(D)
public class ProductValidator {
public boolean isValid(Product product) {
// Validation logic
return product.getPrice() > 0;
}
}
9 4
21
59
A38. B is correct. The class ProductService, as shown in option B, is the most appropriate place for
07
business logic in a Spring application. In this case, ProductService handles the core logic for
validating and saving a product. The ProductController class in option A is responsible for handling
80
incoming HTTP requests, while the ProductRepository interface in option C manages data
.in
persistence. The ProductValidator class in option D might be used for specific validation or utility
functions but does not typically contain business logic.
ys
ra
ar
ith
w
de
co
https://www.youtube.com/@codewitharrays
https://www.instagram.com/codewitharrays/
https://codewitharrays.in/project