Search Tutorials


Spring Data JPA Quiz - MCQ - Multiple Choice Questions | JavaInUse

Spring Data JPA Quiz - MCQ - Multiple Choice Questions

Q. What is Spring Data?

A. A database management system
B. A cloud computing platform
C. An ORM framework for Java applications
D. A dependency injection container

Q. What is the main goal of Spring Data?

A. To provide a simple and consistent API for data access
B. To optimize database performance
C. To replace the need for SQL
D. To integrate with cloud storage systems

Q. Which of the following is NOT a Spring Data module?

A. Spring Data JPA
B. Spring Data MongoDB
C. Spring Data Hibernate
D. Spring Data Redis

Q. Which annotation is used to declare a Spring Data repository?

A. @Repository
B. @Controller
C. @Service
D. @Component

Q. Which query method keyword is used to specify a custom query in Spring Data?

A. @Query
B. @FindBy
C. @CustomQuery
D. @CustomMethod

Q. Which of the following is NOT a valid Spring Data repository method prefix?

A. findById
B. findByFirstName
C. countByLastName
D. removeByAge

Q. Which of the following is NOT a valid repository method return type in Spring Data?

A. void
B. Iterable
C. List
D. Optional





Q. Which of the following is NOT a valid way to enable Spring Data integration in a project?

A. Adding dependency to pom.xml (Maven)
B. Adding <context:component-scan> in the Spring configuration file
C. Using Spring Boot's auto-configuration
D. Manually configuring the data source bean

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. Which of the following is NOT true about Spring Data repositories?

A. They can be defined for both entities and value objects
B. They provide basic CRUD operations out-of-the-box
C. Custom query methods can be defined using method names
D. They can only be used with relational databases

Q. Which of the following code snippets demonstrates the correct configuration of a Spring Data JPA Repository?

A.
public interface UserRepository extends JpaRepository<User, Long> {

}
B.
public interface UserRepository extends CrudRepository<User, Long> {

}
C.
public interface UserRepository extends JpaDao<User, Long> {

}
D.
public interface UserRepository extends JpaRepository<User, Long> {

}

Q. Which of the following code snippets demonstrates the correct usage of a derived query method in Spring Data JPA?

A.
public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}
B.
public interface UserRepository extends JpaRepository<User, Long> {
    User getUserByUsername(String username);
}
C.
public interface UserRepository extends JpaRepository<User, Long> {
    User findByEmail(String email);
}
D.
public interface UserRepository extends JpaRepository<User, Long> {
    User findUserByEmail(String email);
}

Q. Which annotation is used to define a named query in Spring Data JPA?

A.
@Query(name = "findUserByUsername")
B.
@NamedQuery(name = "findUserByUsername")
C.
@NamedQuery("findUserByUsername")
D.
@NamedQuery(value = "findUserByUsername")

Q. Which of the following code snippets demonstrates the correct usage of the @Transactional annotation in Spring Data JPA?

A.
@Transactional
public void saveUser(User user) {
    userRepository.save(user);
}
B.
@Transactional(propagation = Propagation.REQUIRED)
public void saveUser(User user) {
    userRepository.save(user);
}
C.
@Transactional(rollbackFor = Exception.class)
public void saveUser(User user) {
    userRepository.save(user);
}
D.
@Transactional(readOnly = true)
public void saveUser(User user) {
    userRepository.save(user);
}

Q. Which of the following code snippets demonstrates the correct usage of the @Query annotation in Spring Data JPA?

A.
@Query("SELECT u FROM User u WHERE u.username = :username")
public User findByUsername(@Param("username") String username);
B.
@Query(value = "SELECT * FROM users WHERE username = :username", nativeQuery = true)
public User findByUsername(@Param("username") String username);
C.
@Query("SELECT * FROM User u WHERE u.username = :username")
public User findByUsername(@Param("username") String username);
D.
@Query(value = "SELECT u FROM users u WHERE u.username = :username", nativeQuery = true)
public User findByUsername(@Param("username") String username);

Q. Which of the following code snippets demonstrates the correct usage of the @JoinColumn annotation in Spring Data JPA?

A.
@OneToOne
@JoinColumn(name = "address_id")
private Address address;
B.
@OneToOne(mappedBy = "user")
private Address address;
C.
@ManyToOne
@JoinColumn(name = "address_id")
private Address address;
D.
@ManyToOne(mappedBy = "user")
private Address address;

Q. Which of the following code snippets demonstrates the correct usage of the @OneToMany annotation in Spring Data JPA?

A.
@OneToMany(mappedBy = "user")
private List<Order> orders;
B.
@OneToMany
@JoinColumn(name = "user_id")
private List<Order> orders;
C.
@ManyToOne
@JoinColumn(name = "user_id")
private List<Order> orders;
D.
@OneToMany(mappedBy = "orders")
private List<User> users;

Q. Which of the following code snippets demonstrates the correct usage of the @ManyToMany annotation in Spring Data JPA?

A.
@ManyToMany
@JoinTable(
    name = "user_role",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id")
)
private List<Role> roles;
B.
@ManyToMany(mappedBy = "users")
private List<Role> roles;
C.
@OneToMany(mappedBy = "users")
private List<Role> roles;
D.
@ManyToMany
@JoinColumn(name = "role_id")
private List<Role> roles;

Q. What is the purpose of the @Transactional annotation in Spring Data JPA?

A.
@Transactional is used to define a named query.
B.
@Transactional is used to mark a method as transactional.
C.
@Transactional is used to define a foreign key column.
D.
@Transactional is used to define a custom JPQL query.

Q. What is the purpose of the @GeneratedValue annotation in Spring Data JPA?

A.
@GeneratedValue is used to define a named query.
B.
@GeneratedValue is used to mark a method as transactional.
C.
@GeneratedValue is used to define a foreign key column.
D.
@GeneratedValue is used to automatically generate a primary key value.