Spring MVC MCQ Banner

Spring MVC Multiple Choice Questions (MCQs) and Answers

Master Spring MVC with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of Spring MVC concepts. Begin your placement preparation journey now!

Q91

Q91 A global exception handler defined with @ControllerAdvice is not handling exceptions. What could be the possible reason?

A

The class is not annotated with @RestController

B

The method is missing @ExceptionHandler

C

The class is not detected by component scanning

D

Both B and C

Q92

Q92 What is the primary purpose of Spring Security in a Spring MVC application?

A

To manage database connections

B

To handle dependency injection

C

To provide authentication and authorization

D

To optimize performance

Q93

Q93 Which component in Spring Security is responsible for authenticating users?

A

AuthenticationManager

B

AuthorizationFilter

C

SecurityContextHolder

D

WebSecurityConfigurer

Q94

Q94 What does the SecurityContextHolder in Spring Security do?

A

Stores security-related information for a request

B

Manages database transactions

C

Handles session timeouts

D

Acts as a view resolver

Q95

Q95 Which of the following best describes method-level security in Spring Security?

A

It restricts access at the service layer

B

It applies security at the controller level

C

It ensures only GET requests are allowed

D

It manages CSRF protection

Q96

Q96 Which annotation enables Spring Security in a Spring Boot application?

A

@EnableWebSecurity

B

@EnableSecurity

C

@EnableAuthentication

D

@EnableSpringSecurity

Q97

Q97 Which method is used to configure HTTP security in Spring Security?

A

configure(HttpSecurity http)

B

setSecurity(HttpSecurity http)

C

defineSecurity(HttpSecurity http)

D

enableSecurity(HttpSecurity http)

Q98

Q98 How do you define a custom authentication provider in Spring Security?

A

Implement AuthenticationProvider

B

Extend WebSecurityConfigurerAdapter

C

Use @EnableWebSecurity

D

Define a custom filter

Q99

Q99 How can you disable CSRF protection in Spring Security?

A

csrf().disable()

B

disableCsrf()

C

setCsrf(false)

D

ignoreCsrf()

Q100

Q100 A user is unable to log in to a secured Spring MVC application. What is the most likely cause?

A

The user does not exist in the database

B

The SecurityContextHolder is null

C

The session has expired

D

The database connection is slow

Q101

Q101 A developer is getting a 403 Forbidden error when accessing a secured endpoint. What could be the reason?

A

The user is not authenticated

B

The user does not have the required role

C

Spring Security is disabled

D

The endpoint URL is incorrect

Q102

Q102 A Spring MVC application is redirecting users to the login page repeatedly. What could be the cause?

A

The session is expiring too quickly

B

The authentication filter is misconfigured

C

CSRF protection is disabled

D

The database is down

Q103

Q103 What does REST stand for in RESTful web services?

A

Representational State Transfer

B

Remote Execution of Services and Transactions

C

Reliable Event Synchronized Transactions

D

Resource Exchange and State Transfer

Q104

Q104 Which annotation is used to create a RESTful controller in Spring MVC?

A

@RestController

B

@Controller

C

@RequestMapping

D

@ResponseBody

Q105

Q105 What is the main difference between @Controller and @RestController in Spring MVC?

A

@RestController returns JSON/XML responses, while @Controller returns views

B

@Controller is for REST APIs, while @RestController is for JSPs

C

@RestController does not require @RequestMapping

D

@Controller automatically enables security

Q106

Q106 Which HTTP method is typically used to update an existing resource in a RESTful API?

A

GET

B

POST

C

PUT

D

DELETE

Q107

Q107 Which annotation maps a method to an HTTP GET request in Spring MVC?

A

@PostMapping

B

@GetMapping

C

@PutMapping

D

@DeleteMapping

Q108

Q108 How can you return JSON responses from a Spring MVC RESTful API?

A

Use @RestController and return Java objects

B

Manually serialize Java objects to JSON

C

Use @Controller with @ResponseBody

D

Use @RequestMapping

Q109

Q109 Which annotation is used to extract path variables in a Spring MVC RESTful API?

A

@RequestParam

B

@PathVariable

C

@ModelAttribute

D

@RequestBody

Q110

Q110 A RESTful API in Spring MVC is returning a 415 Unsupported Media Type error. What could be the reason?

A

The request does not specify the correct Content-Type header

B

The URL mapping is incorrect

C

The service is down

D

The database is not connected

Q111

Q111 A client is getting a 404 Not Found error when calling a REST API endpoint. What could be the issue?

A

The method is private

B

The URL does not match any controller mapping

C

The request body is missing

D

The response type is incorrect

Q112

Q112 Which interface in Spring JDBC provides methods for executing SQL queries?

A

JdbcTemplate

B

EntityManager

C

SessionFactory

D

DataSource

Q113

Q113 What is the primary advantage of using JPA over JDBC?

A

JPA allows object-relational mapping

B

JPA does not require SQL knowledge

C

JPA is faster than JDBC

D

JPA does not need a database connection

Q114

Q114 Which of the following is NOT a key feature of Spring JDBC?

A

Simplified error handling

B

Automatic transaction management

C

Object-relational mapping

D

PreparedStatement support

Q115

Q115 Which annotation is used to mark an entity class in JPA?

A

@Table

B

@Entity

C

@Repository

D

@PersistenceContext

Q116

Q116 Which annotation is used to define a primary key in a JPA entity?

A

@Column

B

@PrimaryKey

C

@Id

D

@Key

Q117

Q117 How do you configure a DataSource bean in a Spring application using Java-based configuration?

A

Use @DataSourceConfig annotation

B

Manually create a DataSource object and annotate it with @Bean

C

Use @Autowired on a DataSource field

D

Define it in application.properties

Q118

Q118 Which method is used in JdbcTemplate to execute an INSERT query?

A

queryForObject()

B

update()

C

executeQuery()

D

persist()

Q119

Q119 A developer is facing a 'No qualifying bean of type DataSource' error. What could be the cause?

A

The DataSource bean is not defined

B

The database is down

C

The table does not exist

D

The SQL syntax is incorrect

Q120

Q120 A JPA query is returning an empty result set despite data being present in the database. What could be the issue?

A

The entity class is not properly annotated

B

The database connection is closed

C

The application is using JDBC instead of JPA

D

The entity is not in the persistence context

ad verticalad vertical
ad