Web Java MCQ
Web Java MCQ
Lecture: Building REST services with Spring Introduction to web services SOAP Vs RESTful
web services RESTful web service introduction Create RESTful web service in java using
Spring Boot RESTful web service JSON example RESTful web service CRUD example
Using POSTMAN client to invoke REST API’s REST service invocation using REST Template I
need most imp 50 mcq on above topics withs ans
Sure, here are 50 multiple-choice questions (MCQs) related to building REST services with
Spring, along with their answers:
3. What is the primary advantage of using RESTful web services over SOAP?
a) Strongly typed message formats
b) Simplicity and ease of use
c) Support for complex security protocols
d) Built-in support for binary data transmission
Answer: b
4. Which HTTP methods are commonly used in RESTful web services for read-only
operations?
a) GET and POST
b) PUT and DELETE
c) POST and DELETE
d) GET and DELETE
Answer: a
6. What is the default response format of a Spring RESTful web service when using the
@RestController annotation?
a) XML
b) JSON
c) YAML
d) HTML
Answer: b
7. Which HTTP status code represents a successful response for a GET request in a RESTful
web service?
a) 200 OK
b) 201 Created
c) 204 No Content
d) 404 Not Found
Answer: a
8. What is the primary purpose of the POST method in RESTful web services?
a) Updating a resource
b) Deleting a resource
c) Creating a resource
d) Retrieving a resource
Answer: c
9. Which annotation is used to map a specific URL pattern to a method in a Spring RESTful
Controller?
a) @RequestMapping
b) @GetMapping
c) @PostMapping
d) @PutMapping
Answer: a
10. How can you specify a path parameter in a Spring RESTful web service URL?
a) {param}
b) /param/
c) <param>
d) ?param=
Answer: a
11. Which HTTP method is used to update a resource in a RESTful web service?
a) PUT
b) POST
c) GET
d) DELETE
Answer: a
12. Which annotation is used to indicate that a method can handle HTTP DELETE requests?
a) @DeleteMapping
b) @RequestMapping(method = RequestMethod.DELETE)
c) @PostMapping
d) @RequestMapping(method = RequestMethod.PUT)
Answer: b
13. What is the primary data format used to represent resources in RESTful web services?
a) XML
b) HTML
c) JSON
d) YAML
Answer: c
14. Which annotation is used to deserialize a JSON request body to a Java object in Spring?
a) @RequestBody
b) @ResponseBody
c) @PathParam
d) @PathVariable
Answer: a
15. In a RESTful web service, what does the term "stateless" mean?
a) The server doesn't store any state information about the client.
b) The client doesn't store any state information about the server.
c) Both the client and server store the state information.
d) The state information is stored in cookies.
Answer: a
16. Which HTTP status code indicates that the resource is not found in a RESTful web
service?
a) 200 OK
b) 204 No Content
c) 404 Not Found
d) 500 Internal Server Error
Answer: c
17. Which HTTP method is used to delete a resource in a RESTful web service?
a) DELETE
b) PUT
c) POST
d) PATCH
Answer: a
18. What does the HTTP POST method do when used in a RESTful web service?
a) Requests data from the server
b) Updates data on the server
c) Deletes data from the server
d) Creates a new resource on the server
Answer: d
19. Which annotation is used to specify the response status code in a Spring RESTful
Controller method?
a) @ResponseStatus
b) @ResponseCode
c) @HttpCode
d) @Status
Answer: a
20. Which HTTP status code indicates that a new resource was successfully created in a
RESTful web service?
a) 200 OK
b) 201 Created
c) 204 No Content
d) 404 Not Found
Answer: b
21. Which method is used to perform a partial update of a resource in a RESTful web
service?
a) PUT
b) DELETE
c) POST
d) PATCH
Answer: d
23. What is the role of the "MediaType" class in Spring when building RESTful web services?
a) It represents the type of the HTTP request method (GET, POST, etc.).
b) It represents the type of the data being exchanged (e.g., JSON, XML).
c) It represents the URL path of the RESTful resource.
d) It represents the status code of the HTTP response.
Answer: b
24. In a Spring RESTful web service, what does the "@CrossOrigin" annotation do?
a) It enables support for CORS (Cross-Origin Resource Sharing) requests.
b) It specifies the origin of the request.
c) It prevents any cross-origin requests.
d) It handles exceptions related to cross-origin requests.
Answer: a
25. Which method is used to perform a full update of a resource in a RESTful web service?
a) POST
b) PUT
c) PATCH
d) DELETE
Answer: b
26. In Spring Boot, where should the application properties be defined for configuring the
RESTful web service?
a) application.properties
b) web.properties
c) rest.properties
d) services.properties
Answer: a
27. Which annotation is used to specify the HTTP response headers in a Spring RESTful
Controller method?
a) @Header
b) @Headers
c) @ResponseHeaders
d) @HttpHeader
Answer: c
29. In a Spring RESTful web service, how can you access query parameters from a URL?
a)
Certainly! Here are 50 more multiple-choice questions (MCQs) related to building REST
services with Spring:
30. Which HTTP status code indicates that the client's request was malformed in a RESTful
web service?
a) 200 OK
b) 400 Bad Request
c) 404 Not Found
d) 500 Internal Server Error
Answer: b
31. What is the purpose of the HTTP OPTIONS method in RESTful web services?
a) To request data from the server
b) To update data on the server
c) To retrieve the allowed methods for a resource
d) To create a new resource on the server
Answer: c
32. Which annotation is used to specify the HTTP method in a Spring RESTful Controller
method?
a) @RequestMapping
b) @Method
c) @MethodMapping
d) @HttpMethod
Answer: a
33. In a Spring RESTful web service, what does the "@PathVariable" annotation do?
a) It extracts the request body from the URL.
b) It extracts the query parameters from the URL.
c) It extracts the path variables from the URL.
d) It extracts the response headers from the URL.
Answer: c
34. Which HTTP status code indicates that the server successfully processed the request but
there is no response entity to return in a RESTful web service?
a) 200 OK
b) 204 No Content
c) 206 Partial Content
d) 404 Not Found
Answer: b
35. In a Spring RESTful web service, how can you return a custom response body?
a) Using the @ResponseBody annotation
b) Using the @CustomResponse annotation
c) Using the @ResponseData annotation
d) Using the @DataResponse annotation
Answer: a
36. What is the purpose of the HTTP HEAD method in RESTful web services?
a) To retrieve the headers of a resource without the body
b) To request data from the server
c) To update data on the server
d) To retrieve the allowed methods for a resource
Answer: a
37. Which annotation is used to enable content negotiation in a Spring RESTful web service?
a) @ContentNegotiation
b) @EnableContentNegotiation
c) @NegotiationEnabled
d) @EnableNegotiation
Answer: b
38. In Spring Boot, what is the default URL path for a RESTful web service endpoint?
a) /api
b) /rest
c) /service
d) /resource
Answer: a
39. Which HTTP status code indicates that the server has not implemented the requested
HTTP method in a RESTful web service?
a) 200 OK
b) 204 No Content
c) 405 Method Not Allowed
d) 500 Internal Server Error
Answer: c
40. In a Spring RESTful web service, what does the "@ResponseBody" annotation do?
a) It extracts the response body from the URL.
b) It extracts the request body from the URL.
c) It indicates that the method's return value should be bound to the response body.
d) It indicates that the method's return value should be bound to the request body.
Answer: c
41. Which HTTP method is used to retrieve a specific representation of a resource in a
RESTful web service?
a) GET
b) POST
c) PUT
d) DELETE
Answer: a
42. What is the purpose of the HTTP TRACE method in RESTful web services?
a) To request data from the server
b) To update data on the server
c) To trace the route to the server
d) To retrieve the allowed methods for a resource
Answer: c
43. In Spring Boot, what is the default port number for running a RESTful web service?
a) 80
b) 8080
c) 8000
d) 8888
Answer: b
44. Which annotation is used to handle versioning in a Spring RESTful web service?
a) @Version
b) @ApiVersion
c) @ApiVersioning
d) @RequestMappingVersion
Answer: b
45. What is the role of the "MediaType.APPLICATION_JSON_VALUE" constant in Spring
RESTful web services?
a) It represents the type of the HTTP request method (GET, POST, etc.).
b) It represents the type of the data being exchanged (e.g., JSON, XML).
c) It represents the URL path of the RESTful resource.
d) It represents the status code of the HTTP response.
Answer: b
46. In a Spring RESTful web service, what does the "@RequestHeader" annotation do?
a) It extracts the response headers from the request.
b) It extracts the request headers from the response.
c) It extracts a specific header value from the request.
d) It extracts a specific header value from the response.
Answer: c
47. Which HTTP status code indicates that the requested resource has been permanently
moved to a new location in a RESTful web service?
a) 200 OK
b) 301 Moved Permanently
c) 404 Not Found
d) 500 Internal Server Error
Answer: b
48. In Spring Boot, which file is used to configure the database connection for the RESTful
web service?
a) application.properties
b) database.properties
c) rest.properties
d) connection.properties
Answer: a
49. Which annotation is used to enable caching in a Spring RESTful web service?
a) @EnableCache
b) @EnableCaching
c) @Cacheable
d) @EnableCachable
Answer: b
50. What is the purpose of the HTTP CONNECT method in RESTful web services?
a) To request data from the server
b) To establish a network connection with the server
c) To update data on the server
d) To retrieve the allowed methods for a resource
Answer: b
I hope these additional questions are helpful! Good luck with your learning and
preparations. If you have any more questions or need further assistance, feel free to ask.
Web-based Java Programming MCQ 9
Spring AOP AOP Overview Spring AOP AOP Terminology and annotations: Advice, Join
Points, Pointcuts, Aspects i need most imp 50 mcq on abve tpoics with ans
Sure, here are 50 multiple-choice questions (MCQs) on Spring AOP with their answers:
Answer: a
b) Join Points
c) Pointcuts
d) Inheritance
Answer: d
5. Which Spring AOP terminology defines the expressions that select the join points where
advice will be applied?
a) Join Points
b) Pointcuts
c) Aspects
d) Advices
Answer: b
d) @Advice
Answer: c
8. Which advice type runs before a join point and has the ability to prevent the target
method from being executed?
a) @Before
b) @Around
c) @After
d) @AfterReturning
Answer: b
10. Which advice type runs after the join point, regardless of its outcome (success or
exception)?
a) @Before
b) @Around
c) @After
d) @AfterReturning
Answer: c
12. Which advice type runs after the target method has thrown an exception?
a) @AfterThrowing
b) @AfterReturning
c) @After
d) @Around
Answer: a
14. In Spring AOP, when is the "Around" advice called in the advice execution order?
a) Before the target method execution
b) After the target method execution
c) Instead of the target method execution
d) Both before and after the target method execution
Answer: c
15. Which advice type can suppress the execution of the target method altogether?
a) @AfterReturning
b) @After
c) @Around
d) @AfterThrowing
Answer: c
17. Which type of AOP advice can modify the arguments passed to the target method?
a) @Before
b) @AfterReturning
c) @Around
d) @AfterThrowing
Answer: c
18. Which advice type has access to both "JoinPoint" and "ProceedingJoinPoint" in Spring
AOP?
a) @Before
b) @AfterReturning
c) @AfterThrowing
d) @Around
Answer: d
a) Pointcuts
b) Join Points
c) Aspects
d) Advices
Answer: a
24. Which of the following is NOT a valid type of advice in Spring AOP?
a) @Before
b) @After
c) @AfterReturning
d) @OnException
Answer: d
25. What is the purpose of the "this" pointcut in Spring AOP?
a) To match join points based on the return type of the target method
26. Which advice type can access the return value of the target method?
a) @Before
b) @After
Sure, here are 50 more multiple-choice questions (MCQs) on Spring AOP with their answers:
Answer: c
Answer: a
31. Which advice type has access to the return value of the target method and can modify
it?
a) @Before
b) @AfterReturning
c) @Around
d) @AfterThrowing
Answer: b
Answer: b
Answer: b
34. Which advice type runs regardless of whether the target method completes successfully
or throws an exception?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer: b
Answer: b
Answer: a
39. Which advice type runs after the "Around" advice, only if the target method completes
successfully?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer: c
40. In Spring AOP, which advice type runs if the target method takes longer than a certain
threshold to execute?
a) @Before
b) @After
c) @AfterReturning
d) @Around
Answer: b
41. Which AOP concept allows you to apply an aspect to a specific set of classes or
packages?
a) Advice
b) Join Points
c) Pointcuts
d) Aspects
Answer: c
42. In Spring AOP, what is the purpose of the "Within" pointcut designator?
a) To match join points within a specific method
b) To match join points within a specific package or class
c) To match join points within a specific object
d) To match join points within a specific aspect
Answer: b
43. What is the purpose of the "cflow" pointcut designator in Spring AOP?
a) To match join points within a specific method
b) To match join points within a specific package or class
c) To match join points within a specific control flow
d) To match join points within a specific aspect
Answer: c
45. Which advice type can access the target object itself?
a) @Before
b) @After
c) @Around
d) @AfterReturning
Answer: c
46. What is the purpose of the "annotation" pointcut designator in Spring AOP?
47. In Spring AOP, what is the purpose of the "if" pointcut designator?
a) To match join points within a specific method
b) To match join points within a specific package or class
c) To match join points based on a boolean expression
d) To match join points within a specific aspect
Answer: c
Sure! Here are 50 multiple-choice questions (MCQs) on the topics related to the Spring Data
module, specifically focusing on Spring Data JPA, CrudRepository, JPARepository, query
methods, and custom queries (@Query).
Sure, here are 50 multiple-choice questions (MCQs) related to the topics of Spring Data JPA,
CrudRepository, JPARepository, Query methods, and using custom queries with their
answers:
Answer: b) save()
Answer: a) JpaRepository provides sorting and paging support, while CrudRepository does
not.
7. Which annotation is used to define custom queries in Spring Data JPA?
a) @CustomQuery
b) @Query
c) @JpaQuery
d) @CustomJpaQuery
Answer: b) @Query
8. What is the JPQL (Java Persistence Query Language) equivalent of the following SQL
query?
SQL: SELECT e FROM Employee e WHERE e.salary > 50000
a) SELECT e FROM Employee e WHERE e.salary > 50000
b) SELECT e.salary > 50000 FROM Employee e
c) SELECT * FROM Employee WHERE salary > 50000
d) SELECT e.salary FROM Employee e WHERE e.salary > 50000
9. In Spring Data JPA, which parameter is used to pass native SQL queries to @Query?
a) nativeQuery
b) sql
c) value
d) query
Answer: a) nativeQuery
10. What is the recommended way to specify a named parameter in a custom @Query?
a) :param
b) #param
c) $param
d) @param
Answer: a) :param
11. Which of the following is true about Spring Data JPA's query methods?
a) Query methods should be prefixed with "find" or "get."
b) Query methods should be prefixed with "query."
c) Query methods should be prefixed with "select."
d) Query methods should not follow any naming conventions.
12. Which keyword is used in a query method to specify a "greater than" condition?
a) IsGreaterThan
b) GreaterThan
c) Gr
d) Gt
Answer: b) GreaterThan
13. What does the keyword "OrderBy" do in a Spring Data JPA query method?
a) Specifies the order of the result set.
b) Groups the results based on a specific property.
c) Joins two entities based on a relationship.
d) Filters the results based on a condition.
14. Which operator is used to express "less than or equal to" in a query method?
a) IsLessThanEqual
b) LessThanEqualTo
c) LessThanOrEqual
d) Le
Answer: a) IsLessThanEqual
15. What is the purpose of the @Modifying annotation in a custom query method?
a) It marks the method as read-only and prevents modifications.
b) It indicates that the query method is not supported by JPA.
c) It indicates that the query will modify the database.
d) It prevents the query method from being executed.
16. Which of the following annotations is used to map a query result to a DTO (Data Transfer
Object) class?
a) @DtoClass
b) @DtoMapping
c) @QueryDto
d) @ConstructorResult
Answer: d) @ConstructorResult
17. In Spring Data JPA, what is the purpose of the @EntityGraph annotation?
a) It defines the entity's structure and relationships.
b) It optimizes database queries by specifying which attributes to load eagerly.
c) It maps the entity to a database table.
d) It configures the database connection.
Answer: b) It optimizes database queries by specifying which attributes to load eagerly.
18. What is the return type of a query method that should return a single result?
a) Object
b) List
c) Optional
d) SingleResult
Answer: c) Optional
19. What is the default behavior of a Spring Data JPA query method if no explicit keyword is
used?
a) "OR" operator is used to combine query method parameters.
b) "AND" operator is used to combine query method parameters.
c) "LIKE" operator is applied to all query method parameters.
d) "BETWEEN" operator is applied to all query method parameters.
20. Which annotation should be used in combination with @Query to fetch the results of a
native SQL query into a List of Object[] arrays?
a) @Entity
b) @SqlResult
c) @SqlResultSetMapping
d) @EntityResult
Answer: c) @SqlResultSetMapping
21. What is the purpose of the @NamedQuery annotation in Spring Data JPA?
a) It provides a named query that can be referenced by a custom query method.
b) It defines a named query that can be used with the EntityManager.
c) It allows customizing the behavior of the JPA EntityManager.
d) It specifies a named parameter for use in a query method.
Answer: b) It defines a named query that can be used with the EntityManager.
22. How is a native SQL query specified in a Spring Data JPA repository interface?
a) @NativeQuery
b) @
Certainly! Here are 50 more multiple-choice questions (MCQs) on Spring Data JPA,
CrudRepository, JPARepository, Query methods, and using custom queries:
23. In Spring Data JPA, which annotation can be used to specify the maximum number of
results to be returned by a query method?
a) @Limit
b) @MaxResults
c) @Top
d) @Max
Answer: b) @MaxResults
24. What is the purpose of the @Param annotation in a custom query method?
a) It specifies the order of the query method parameters.
b) It indicates that the parameter is required and cannot be null.
c) It assigns a custom name to the query method parameter.
d) It converts the parameter to a specific data type.
26. What is the role of the @Lock annotation in Spring Data JPA?
a) It locks the entity during database updates to prevent concurrency issues.
b) It locks the entire database to prevent concurrent read and write operations.
c) It unlocks an entity that has been locked by another process.
d) It provides optimistic locking for the entity.
Answer: a) It locks the entity during database updates to prevent concurrency issues.
27. Which of the following is true about using the @NamedQuery annotation in Spring Data
JPA?
a) Named queries are defined directly within the repository interface.
b) Named queries are defined in the persistence.xml file.
c) Named queries are defined in a separate XML file.
d) Named queries cannot be used with Spring Data JPA.
28. What is the purpose of the @Procedure annotation in Spring Data JPA?
a) It indicates that the method will perform a stored procedure call.
b) It defines the structure of a stored procedure.
c) It maps the entity to a database stored procedure.
d) It enables the use of transactional behavior in stored procedures.
Answer: a) It indicates that the method will perform a stored procedure call.
29. In Spring Data JPA, which annotation should be used to enable a transaction for a custom
query method?
a) @Transactional
b) @EnableTransaction
c) @TransactionConfig
d) @EnableJpaTransaction
Answer: a) @Transactional
30. What is the purpose of the @EntityListeners annotation in Spring Data JPA?
a) It defines the listeners for specific entities.
b) It specifies the entity's relationships with other entities.
c) It marks an entity as eligible for garbage collection.
d) It enables auditing features for an entity.
Answer: c) Repository
32. What is the difference between the @Query annotation and the query methods in
Spring Data JPA?
a) @Query is used for native SQL queries, while query methods are used for JPQL queries.
b) @Query is used for read-only operations, while query methods are used for write
operations.
c) @Query allows for complex query customization, while query methods are more
straightforward.
d) @Query is used for single-result queries, while query methods return lists of results.
Answer: c) @Query allows for complex query customization, while query methods are
more straightforward.
33. Which method from JpaRepository is used to delete an entity by its primary key?
a) remove()
b) deleteById()
c) delete()
d) erase()
Answer: b) deleteById()
35. In a Spring Data JPA repository, which of the following methods can be used to count the
total number of entities in a table?
a) count()
b) getTotalEntities()
c) size()
d) getTotalCount()
Answer: a) count()
36. What is the purpose of the @QueryHints annotation in Spring Data JPA?
a) It provides additional hints to the JPA query optimizer.
b) It specifies hints for how the database should handle the query.
c) It caches the query results for faster access.
d) It enables query logging for debugging purposes.
37. Which of the following is true about custom query methods in Spring Data JPA?
a) Custom query methods must return a List or a Collection.
b) Custom query methods must always have at least one parameter.
c) Custom query methods cannot have more than one condition in the WHERE clause.
d) Custom query methods should be named starting with "findBy."
38. What is the use of the @EnableJpaRepositories annotation in a Spring Boot application?
a) It enables transaction management for JPA repositories.
b) It activates the JPA entity scanning feature.
c) It initializes the Spring Data JPA infrastructure.
d) It enables automatic CRUD operations for all JPA entities.
39. In Spring Data JPA, which of the following annotations is used to map a JPA entity to a
database table?
a) @Table
b) @Entity
c) @EntityMapping
d) @DatabaseTable
Answer: b) @Entity
40. What is the purpose of the @Column annotation in a JPA entity class?
a) It indicates the primary key column of the entity.
b) It specifies the column's data type and constraints.
c) It marks the entity as a database table.
d) It sets the foreign key relationship between entities.
41. Which method from CrudRepository is used to delete all entities from a table?
a) removeAll()
b) deleteAll()
c) removeAllEntities()
d) truncate()
Answer: b) deleteAll()
42. What is the purpose of the @GeneratedValue annotation in a JPA entity class?
a) It generates random values for primary key fields.
b) It assigns an auto-incremented value to
Web-based Java Programming MCQ 7
Spring Boot essentials Why Spring boot Spring Boot Overview Basic Introduction of MAVEN
Building Spring Web application with Boot Spring Boot in detail (Use Spring Boot for all demo &
assignments here onwards) Running a web application using Spring Boot with CRUD (with Static
Data not DB)
Sure, I can provide you with 50 multiple-choice questions (MCQs) related to Spring Boot
essentials along with their answers. Here they are:
5. What is the command to create a new Spring Boot project using the Spring Boot CLI?
a) spring create project
b) spring new project
c) spring init project
d) spring generate project
Answer: c
6. In Spring Boot, what file holds the properties that can be used to configure the
application?
a) app.properties
b) application.yml
c) config.properties
d) settings.yml
Answer: b
7. Which annotation is used in a Spring Boot application to enable component scanning and
automatic configuration?
a) @ComponentScan
b) @Autowired
c) @EnableAutoConfiguration
d) @SpringBootApplication
Answer: d
8. What is the default embedded server provided by Spring Boot for running web
applications?
a) Apache Tomcat
b) Jetty
c) WildFly
d) GlassFish
Answer: a
9. Which of the following properties can be used to change the default server port in a
Spring Boot application?
a) server.port
b) spring.port
c) app.serverPort
d) application.server.port
Answer: a
11. How does Spring Boot simplify the development of data access layers?
a) By providing a built-in database management system
b) By automatically generating SQL queries based on entity classes
c) By eliminating the need for data access layers altogether
d) By integrating Hibernate only
Answer: b
12. Which of the following annotations is used to mark a Spring Boot method as a
transactional method?
a) @Transactional
b) @Transaction
c) @TransactionMethod
d) @TransactionalMethod
Answer: a
13. In Spring Boot, which file is used to define the dependencies and their versions required
for the project?
a) dependencies.xml
b) project.xml
c) build.gradle
d) pom.xml
Answer: d
15. Which endpoint is used to get information about the health of a Spring Boot application
using Actuator?
a) /metrics
b) /info
c) /health
d) /status
Answer: c
17. How can you define default property values in Spring Boot application configuration
files?
a) By using the @Value annotation
b) By using the @DefaultValue annotation
c) By using the default property attribute in the @ConfigurationProperties annotation
d) By using the @Default annotation
Answer: c
18. Which annotation is used to schedule a method to be executed at a specific time interval
in Spring Boot?
a) @Scheduled
b) @TimerTask
c) @CronTask
d) @ScheduledTask
Answer: a
19. What does the Spring Boot Starter Parent POM provide?
a) Default properties for all Spring Boot projects
b) A parent POM with no default configurations
c) The necessary dependencies to build a Spring Boot application
d) Starter configurations for non-Spring Boot projects
Answer: a
20. Which of the following properties is used to configure the application name in Spring
Boot Actuator?
a) management.applicationName
b) spring.application.name
c) application.name
d) actuator.applicationName
Answer: b
21. How can you enable hot-swapping of classes during development in a Spring Boot
application?
a) By using a separate plugin like JRebel
b) By enabling the "spring-boot-devtools" dependency
c) By configuring hot-swapping in the IDE
d) Hot-swapping is not possible in Spring Boot
Answer: b
22. What is the role of the "@EnableWebMvc" annotation in a Spring Boot application?
a) It enables the application to serve static resources.
b) It enables the use of Spring Web MVC framework features.
c) It configures the database connection in the application.
d) It triggers automatic configuration of beans.
Answer: b
24. In a Spring Boot project, where should the main class (containing the "main" method) be
located?
a) com.main
b) src/main
c) src/main/java
d) src/main/resources
Answer: c
25. Which property is used to specify the URL path for the Actuator endpoints in a Spring
Boot application?
a) management.context-path
b) spring.boot.actuator.path
c) management.endpoint.path
d) spring.boot.actuator.base-path
Answer: a
26. How can you enable HTTP response compression in a Spring Boot application?
a) By adding the @Compress annotation
Certainly! Here are 50 more multiple-choice questions related to Spring Boot essentials with
their answers:
27. Which annotation is used to define a Spring Bean in a Spring Boot application explicitly?
a) @Component
b) @Bean
c) @Service
d) @Autowired
Answer: b
30. How can you enable cross-origin requests in a Spring Boot application?
a) By using the @CrossOrigin annotation
b) By setting "allow-cross-origin" property to "true" in application.properties
c) Cross-origin requests are enabled by default in Spring Boot
d) By configuring a filter in web.xml
Answer: a
31. In a Spring Boot application, where should the static resources like HTML, CSS, and JS
files be placed?
a) src/main/resources
b) src/main/static
c) src/main/webapp
d) src/main/resources/static
Answer: d
32. Which of the following annotations is used to define the primary bean in case of multiple
candidates in Spring Boot?
a) @Primary
b) @First
c) @Main
d) @Default
Answer: a
33. What is the purpose of the "application.properties" file in a Spring Boot application?
a) To define the application's dependencies and their versions
b) To configure the application's properties and behavior
c) To specify the database schema for the application
d) To configure the logging level for the application
Answer: b
34. How can you externalize configuration properties in a Spring Boot application?
a) By using environment variables only
b) By using the "application.properties" file only
c) By using the "application.yml" file only
d) By using both "application.properties" and "application.yml" files
Answer: d
36. Which of the following annotations is used to inject dependencies by type in Spring
Boot?
a) @Autowired
b) @Inject
c) @Resource
d) @Dependency
Answer: a
38. How can you enable the JPA Hibernate SQL query logging in a Spring Boot application?
a) By setting the "logging.level.org.hibernate.SQL" property to "DEBUG"
b) By setting the "logging.sql" property to "true"
c) By adding the "spring-boot-starter-logging" dependency
d) JPA Hibernate SQL query logging is enabled by default
Answer: a
39. Which of the following annotations is used to enable Caching support in a Spring Boot
application?
a) @Cacheable
b) @EnableCaching
c) @CacheConfig
d) @Cachable
Answer: b
41. Which property is used to configure the connection pool size for the HikariCP connection
pool in Spring Boot?
a) spring.datasource.pool-size
b) spring.datasource.maximum-pool-size
c) spring.datasource.minIdle
d) spring.datasource.hikari.pool-size
Answer: b
42. How can you enable HTTPS for a Spring Boot application?
a) By setting "server.ssl.enabled" property to "true" in application.properties
b) By adding the "@EnableHttps" annotation to the main class
c) HTTPS is enabled by default in Spring Boot
d) By configuring a filter in web.xml
Answer: a
46. How can you run Spring Boot tests using Maven?
a) By running the command "mvn test"
b) By running the command "mvn spring-boot:test"
c) By running the command "mvn spring-boot:run"
d) Spring Boot tests cannot be run using Maven
Answer: a
47. Which annotation is used to define custom configuration properties in Spring Boot?
a) @CustomProperties
b) @ConfigurationProperties
c) @PropertiesConfig
d) @PropertyConfig
Answer: b
48. What is the purpose of the Spring Boot Actuator "env" endpoint?
a) To provide information about the application's environment and properties.
b) To display the list of available Actuator endpoints.
c) To enable/disable the Actuator endpoints.
d) To manage the application's environment variables.
Answer: a
49. How can you disable the Spring Boot banner during application startup?
a) By setting "spring.banner.enabled" property to "false" in application.properties
b) By removing the "banner.txt" file from the classpath
c) The banner cannot be disabled in Spring Boot
d) By adding "@EnableBanner(false)" annotation to the main class
Answer: a
50. What
Web-based Java Programming MCQ 6
What is Spring Framework Overview of Spring Architecture Spring MVC architecture Spring
Modules Overview Understanding Spring 4 annotations (Basic Introduction) What is IoC
(Inversion of Control) IOC container Dependency Injection Spring Beans Autowiring Beans
Bean Scopes
Sure! Here are 50 multiple-choice questions (MCQs) along with their answers on the topics
related to the Spring Framework:
2. What does IOC stand for in the context of the Spring Framework?
a) In-Out Calling
b) In-Object Control
c) Inversion of Classes
d) Inversion of Control
Answer: a) @Component
Answer: a) Model-View-Controller
Answer: c) Controller
Answer: a) Singleton
Answer: a) @Autowired
11. What does autowiring in Spring mean?
a) It allows Spring to automatically configure database connections.
b) It enables automatic deployment of Spring applications.
c) It automatically resolves and injects bean dependencies.
d) It automatically generates HTML templates for web pages.
Answer: c) Spring TX
13. Which XML tag is used to define a Spring bean in the application context configuration?
a) <spring-bean>
b) <bean>
c) <spring:bean>
d) <spring:component>
Answer: b) <bean>
17. In Spring, which bean scope creates a new bean instance for each request?
a) Singleton
b) Prototype
c) Request
d) Session
Answer: c) Request
Answer: d) It indicates that the method return value should be used as the response body.
25. Which Spring module provides support for managing internationalization (i18n) and
localization (l10n)?
a) Spring Security
b) Spring Web
c) Spring i18n
d) Spring Context
Certainly! Here are 50 more multiple-choice questions (MCQs) along with their answers on
the topics related to the Spring Framework:
29. Which annotation is used to enable Spring's support for transaction management?
a) @Transactional
b) @Transaction
c) @EnableTransactionManagement
d) @ManageTransaction
Answer: c) @EnableTransactionManagement
31. Which Spring module provides support for building RESTful web services?
a) Spring Web
b) Spring REST
c) Spring MVC
d) Spring HATEOAS
Answer: a) Singleton
34. Which XML tag is used to enable the use of annotations in Spring configuration files?
a) <spring:annotation-config>
b) <context:annotation-config>
c) <spring:component-scan>
d) <context:component-scan>
Answer: b) <context:annotation-config>
36. Which Spring module provides support for managing JMS (Java Message Service)?
a) Spring JMS
b) Spring Web
c) Spring Messaging
d) Spring AOP
39. Which Spring module provides support for interacting with databases using Object-
Relational Mapping (ORM)?
a) Spring JDBC
b) Spring AOP
c) Spring ORM
d) Spring Data
Answer: c) It allows specifying which bean to autowire when multiple options are
available.
41. In Spring, which type of bean scope creates a new instance per HTTP session?
a) Singleton
b) Prototype
c) Request
d) Session
Answer: d) Session
42. Which Spring module provides support for security features like authentication and
authorization?
a) Spring Security
b) Spring Web
c) Spring AOP
d) Spring Data
45. Which Spring module provides support for handling exceptions in a structured way?
a) Spring MVC
b) Spring Core
c) Spring AOP
d) Spring Boot
48. Which Spring module provides support for integrating with Hibernate for ORM?
a) Spring Data
b) Spring ORM
c) Spring Web
d) Spring Hibernate
50. Which Spring module provides support for managing scheduled tasks?
a) Spring Batch
b) Spring Core
c) Spring Scheduling
d) Spring Boot
Remember that the Spring Framework is a vast topic, and these questions cover various
aspects of it. Make sure to study further and explore the official Spring documentation for a
Spring MVC Model, Model & View, HandlerMapping, ViewResolver Design Pattern: Front
Controller Pattern Spring MVC Web application with JSP views (without Spring Boot)
Using Thymeleaf as alternate View Technology (only introduction) Spring Validations
Spring i18n, Localization, Properties File Upload example
Sure! Here are 50 multiple-choice questions (MCQs) on the mentioned topics with their
answers:
2. In Spring MVC, what is responsible for managing the application data and business logic?
a) View
b) Model
c) Controller
d) HandlerMapping
Answer: b) Model
3. Which component of Spring MVC is responsible for combining the model and view and
returning the result to the user?
a) Model
b) View
c) Controller
d) Model & View
Answer: d) Controller
4. The Front Controller Pattern in Spring MVC is implemented by:
a) DispatcherServlet
b) Model
c) ViewResolver
d) HandlerMapping
Answer: a) DispatcherServlet
5. In Spring MVC, what is responsible for mapping the incoming requests to appropriate
controllers?
a) Model
b) View
c) Controller
d) HandlerMapping
Answer: d) HandlerMapping
7. In a Spring MVC web application without Spring Boot, what file is used for configuring the
DispatcherServlet?
a) web.xml
b) application.properties
c) dispatcher-servlet.xml
d) applicationContext.xml
Answer: a) web.xml
8. Which technology can be used as an alternate view technology to JSP in Spring MVC?
a) HTML
b) XML
c) Thymeleaf
d) JSON
Answer: c) Thymeleaf
12. To enable i18n in Spring, what resource bundle file(s) are used to store localized
messages?
a) messages.xml
b) messages.properties
c) messages.lang
d) messages.txt
Answer: b) messages.properties
14. Which tag is used in JSP to display model attributes on the view?
a) <output>
b) <display>
c) <spring:display>
d) <c:out>
Answer: d) <c:out>
15. How can you handle file uploads in a Spring MVC application?
a) Using HTML <form> with enctype="multipart/form-data"
b) Using <input type="file"> tag only
c) By converting files to base64 strings before submitting the form
d) File uploads are not supported in Spring MVC
Answer: a) Using HTML <form> with enctype="multipart/form-data"
16. In Thymeleaf, how do you display the value of a model attribute on the view?
a) ${attributeName}
b) #{attributeName}
c) @attributeName
d) *{attributeName}
Answer: d) *{attributeName}
18. Which annotation is used to specify the maximum length of a string field during
validation?
a) @Min
b) @Max
c) @Size
d) @Length
Answer: c) @Size
20. How do you set the default locale for i18n in a Spring application?
a) Set it in the DispatcherServlet configuration
b) Set it in the application.properties file
c) Set it in the messages.properties file
d) Set it in the web.xml file
Answer: b) Set it in the application.properties file
21. How can you access the localized messages in a Thymeleaf template?
a) Using #{...}
b) Using ${...}
c) Using @{...}
d) Using *{...}
Answer: a) Using #{...}
22. In Spring MVC, how can you handle exceptions globally across all controllers?
a) By using @ExceptionHandler annotation
b) By using @GlobalException annotation
c) By defining a <global-exception> element in web.xml
d) Spring MVC does not support global exception handling
Answer: a) By using @ExceptionHandler annotation
24. In Spring, how do you define a custom message source for i18n?
a) Implement the MessageSource interface
b) Extend the MessageSource class
c) Define it in the messages.properties file
d) Define it in the application.properties file
Answer: a) Implement the MessageSource interface
26. Which property of the @RequestMapping annotation is used to specify the HTTP
method for a controller method?
a) path
b) value
c) method
d) type
Answer: c) method
Sure! Here are 50 more multiple-choice questions (MCQs) on the mentioned topics:
30. Which method of the Controller interface is responsible for handling the incoming
requests?
a) processRequest()
b) handleRequest()
c) handle()
d) process()
Answer: b) handleRequest()
31. In Spring MVC, how can you pass data from the controller to the view?
a) Using request parameters
b) Using response headers
c) Using session attributes
d) Using model attributes
Answer: d) Using model attributes
36. Which configuration file is used to define the beans in a Spring MVC application?
a) applicationContext.xml
b) web.xml
c) dispatcher-servlet.xml
d) spring-mvc.xml
Answer: c) dispatcher-servlet.xml
37. The @ModelAttribute annotation can be used at the method level to:
a) Map the model attributes to views
b) Set the default model attributes for all handler methods in the controller
c) Validate the model attributes
d) Handle file uploads
Answer: b) Set the default model attributes for all handler methods in the controller
38. In Spring MVC, how can you retrieve request parameters in a controller method?
a) Using HttpServletRequest object
b) Using HttpServletResponse object
c) Using HttpSession object
d) Using @RequestParam annotation
Answer: d) Using @RequestParam annotation
40. How do you enable file uploads in a Spring MVC application without Spring Boot?
a) Add a dependency for file upload in pom.xml
b) Include commons-fileupload and commons-io libraries
c) Enable multipart support in web.xml
d) Enable multipart support in dispatcher-servlet.xml
Answer: b) Include commons-fileupload and commons-io libraries
41. What is the purpose of the @PathVariable annotation in Spring MVC?
a) To map URL parameters to controller method parameters
b) To map request headers to controller method parameters
c) To map session attributes to controller method parameters
d) To map model attributes to controller method parameters
Answer: a) To map URL parameters to controller method parameters
48. Which method of the Validator interface is used to perform custom validation logic?
a) validate()
b) validateField()
c) supports()
d) supportsField()
Answer: a) validate()
Sure, here are 50 multiple-choice questions (MCQs) on Hibernate Framework along with
their answers:
1. What is Hibernate?
a) A programming language
b) A relational database management system
c) An object-relational mapping (ORM) framework
d) An IDE for Java development
Answer: c
b) persistence.xml
c) application.properties
d) hibernate.properties
Answer: a
Answer: a
a) @Persistent
b) @Entity
c) @PersistentEntity
d) @HibernateEntity
Answer: b
8. What is the lifecycle of a Hibernate entity?
a) Create, Read, Update, Delete (CRUD)
b) New, Managed, Detached, Removed
9. Which annotation is used to define the primary key of an entity class in Hibernate?
a) @PrimaryKey
b) @Key
c) @Id
d) @PrimaryKeyColumn
Answer: c
d) @OneToNone
Answer: a
c) A language for defining mappings between Java classes and database tables.
d) A language for executing queries on Hibernate-managed entities.
Answer: d
21. Which interface is responsible for interacting with the Hibernate session?
a) Session
b) SessionFactory
c) EntityManager
d) HibernateSession
Answer: a
22. In Hibernate, how can you map a list of objects to a single column in the database?
a) @ListColumn
b) @ListTable
c) @ElementCollection
d) @CollectionTable
Answer: c
29. Which of the following is true about the CascadeType.PERSIST option in Hibernate?
a) It propagates all operations to related entities, including persisting them.
b) It propagates only the delete operation to related entities.
c) It propagates only the insert operation to related entities.
d) It doesn't propagate any operations to related entities.
Answer: a
31. Which annotation is used to map a Java enum to a database column in Hibernate?
a) @Column
b) @EnumType
c) @Enumerated
d) @EnumColumn
Answer: c
32. What is the purpose of the @Temporal annotation in Hibernate?
a) It is used to specify the data type of a property.
b) It is used to map date and time properties to database columns.
c) It is used to update an existing entity or save a new entity based on its existence in the
database.
Answer: a
c) It is used to specify the mapping between Java classes and database tables.
d) It is used to define named queries for the entity.
Answer: a
40. Which of the following is an advantage of using Hibernate in a web application?
a) It reduces the complexity of the application.
b) It provides a user interface for the application.
Sure, here are 50 multiple-choice questions (MCQs) with answers on JDBC & Transaction
Management:
2. Which layer of the JDBC architecture provides the interface for drivers?
a) DriverManager
b) Connection
c) Statement
d) ResultSet
Answer: a) DriverManager
3. Which type of JDBC driver translates JDBC calls into the vendor-specific database
protocol?
a) Type 1
b) Type 2
c) Type 3
d) Type 4
Answer: d) Type 4
Answer: b) Connection
b) Connection
c) Statement
d) ResultSet
Answer: c) Statement
Answer: d) ResultSet
Answer: c) A set of SQL statements combined and stored in the database as a single
program unit.
Answer: c) To encapsulate the interaction with the database and isolate the application
from the specifics of the database.
12. In the JDBC API, which class is used to manage a set of JDBC drivers?
a) java.sql.Connection
b) java.sql.DriverManager
c) java.sql.PreparedStatement
d) java.sql.ResultSet
Answer: b) java.sql.DriverManager
13. Which JDBC driver type is platform-independent and translates JDBC calls into ODBC
calls?
a) Type 1
b) Type 2
c) Type 3
d) Type 4
Answer: a) Type 1
14. Which of the following interfaces is used to execute parameterized queries in JDBC?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.PreparedStatement
d) java.sql.ResultSet
Answer: c) java.sql.PreparedStatement
15. What does the executeQuery() method of the Statement interface return?
a) An int value
b) A boolean value
c) A ResultSet object
d) A String object
16. Which JDBC interface is responsible for managing the results of a database query?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.ResultSet
Answer: d) java.sql.ResultSet
17. What is the purpose of the executeUpdate() method of the Statement interface in JDBC?
a) To execute SELECT queries
b) To execute INSERT, UPDATE, or DELETE queries
c) To execute parameterized queries
d) To execute stored procedures
Answer: b) To execute INSERT, UPDATE, or DELETE queries
18. Which JDBC interface is responsible for managing the connection to the database?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.ResultSet
Answer: b) java.sql.Connection
19. Which JDBC interface is responsible for managing the JDBC drivers?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.ResultSet
Answer: a) java.sql.Driver
22. Which type of JDBC driver converts JDBC calls into network protocol calls?
a) Type 1
b) Type 2
c) Type 3
d) Type 4
Answer: c) Type 3
23. Which JDBC interface is responsible for managing the result set metadata?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.ResultSetMetaData
Answer: d) java.sql.ResultSetMetaData
24. Which JDBC interface is used to execute SQL queries that may return multiple result
sets?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.CallableStatement
Answer: d) java.sql.CallableStatement
25. What is the main advantage of using PreparedStatement over Statement in JDBC?
a) PreparedStatement can execute multiple SQL queries in a single call.
Certainly! Here are 50 more multiple-choice questions (MCQs) with answers on JDBC & Transaction
Management:
c) To retrieve metadata about the result set, such as column names and types
Answer: c) To retrieve metadata about the result set, such as column names and types
28. Which type of JDBC driver communicates directly with the database server using native
protocols?
a) Type 1
b) Type 2
c) Type 3
d) Type 4
Answer: d) Type 4
29. Which JDBC interface is used to handle the flow of SQL statements and the result sets associated
with a database connection?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.ResultSet
Answer: c) java.sql.Statement
31. Which JDBC interface is used to manage large binary data, such as images or files, in the
database?
a) java.sql.Driver
b) java.sql.Connection
c) java.sql.Statement
d) java.sql.Blob
Answer: d) java.sql.Blob
33. Which of the following statements is true about the Type 1 JDBC driver?
35. Which type of JDBC driver translates JDBC calls into a middleware-specific database protocol?
a) Type 1
b) Type 2
c) Type 3
d) Type 4
Answer: c) Type 3
36. What does the execute() method of the PreparedStatement interface return?
a) An int value
b) A boolean value
c) A ResultSet object
d) Void (nothing)
Answer: b) A boolean value
Answer: c) A group of related SQL statements that are executed as a single unit
38. What is the purpose of the setAutoCommit() method in the Connection interface?
b) To set the maximum number of active connections allowed for a connection pool
39. Which method of the Connection interface is used to commit a transaction explicitly?
a) commit()
b) rollback()
c) setAutoCommit()
d) getAutoCommit()
Answer: a) commit()
41. In a JDBC application, if automatic commit mode is disabled, when should the commit() method
be called to make the transaction permanent?
43. What is the purpose of the rollback() method in the Connection interface?
c) To set the maximum number of active connections allowed for a connection pool
44. Which isolation level allows a transaction to read uncommitted changes made by other
transactions?
a) READ UNCOMMITTED
b) READ COMMITTED
c) REPEATABLE READ
d) SERIALIZABLE
45. Which isolation level ensures that a transaction sees only committed changes made by other
transactions?
a) READ UNCOMMITTED
b) READ COMMITTED
c) REPEATABLE READ
d) SERIALIZABLE
a) READ UNCOMMITTED
b) READ COMMITTED
c) REPEATABLE READ
d) SERIALIZABLE
47. What is the purpose of the setTransactionIsolation() method in the Connection interface?
b) To set the maximum number of active connections allowed for a connection pool
Sure, here are 50 multiple-choice questions with answers on the topics related to JSP and
MVC architecture:
a) Factory Pattern
b) Singleton Pattern
c) MVC Pattern
d) Observer Pattern
Answer: c) MVC Pattern
c) <page-import>
d) <%@ page import="...">
Answer: d) <%@ page import="...">
a) request
b) response
c) session
d) out
Answer: a) request
d) out
Answer: d) out
b) To import classes
c) To write Java code directly in the JSP page
d) To include external files
Answer: c) To write Java code directly in the JSP page
8. Which expression is used to embed Java code within the JSP markup?
a) <%= ... %>
b) <& ... &>
a) Extended Language
b) Embedded Logic
c) Expression Language
d) Enhanced Looping
Answer: c) Expression Language
10. Which scope in JSP makes the data available to all JSP pages within the same
application?
a) Request Scope
b) Session Scope
c) Application Scope
d) Page Scope
11. What is the default scope of JSP objects like request and response?
a) Request Scope
b) Session Scope
c) Application Scope
d) Page Scope
Answer: a) Request Scope
12. How do you handle JSP errors using the errorPage directive?
a) <errorPage="...">
b) <%@ errorPage="...">
c) <error-page="...">
d) <%@ page errorPage="...">
Answer: d) <%@ page errorPage="...">
13. What is the purpose of JSTL (JavaServer Pages Standard Tag Library)?
d) <c:when>
Answer: b) <c:if>
b) <c:forEach>
c) <c:iterate>
d) <c:loop>
Answer: b) <c:forEach>
16. How do you import the JSTL library into a JSP page?
a) <import taglib="...">
b) <include taglib="...">
c) <taglib import="...">
d) <%@ taglib import="...">
Answer: d) <%@ taglib import="...">
18. Which part of the MVC pattern is responsible for interacting with the user?
a) Model
b) View
c) Controller
d) Service
Answer: b) View
b) <type>
c) <responseType>
d) <%@ page contentType="...">
Answer: d) <%@ page contentType="...">
21. Which implicit object represents the user's session with the server?
a) request
b) response
c) session
d) application
Answer: c) session
22. Which explicit object is used to perform server-side includes in a JSP page?
a) request
b) response
c) pageContext
d) application
Answer: c) pageContext
23. How do you declare a variable in JSP using the Expression Language (EL)?
25. Which JSP tag is used to define reusable content that can be included in other JSP pages?
a) <jsp:include>
b) <c:import>
c) <c:include>
d) <jsp:useBean>
Answer: a) <jsp:include>
c) <fmt:bundle>
d) <fmt:message>
Answer: a) <fmt:format>
Of course! Here are 50 more multiple-choice questions with answers on JSP, MVC
architecture, and related topics:
28. Which part of the MVC pattern is responsible for displaying the data to the user?
a) Model
b) View
c) Controller
d) Service
Answer: b) View
a) request
b) response
c) session
d) application
Answer: d) application
d) <c:removeAttribute>
Answer: a) <c:remove>
35. Which JSP directive is used to include the content of an external file in a JSP page?
a) <include>
b) <%@ include file="...">
c) <import>
d) <%@ import file="...">
37. Which JSTL tag is used to set a value for a session attribute?
a) <c:set>
b) <c:put>
c) <c:attribute>
d) <c:assign>
Answer: b) <c:put>
39. Which JSP directive is used to specify the character encoding for the JSP page?
a) <charset>
b) <%@ charset="...">
c) <pageEncoding>
d) <%@ page pageEncoding="...">
Answer: d) <%@ page pageEncoding="...">
40. How do you access a request parameter using Expression Language (EL)?
a) ${request.getParameter("paramName")}
b) ${param.paramName}
c) ${paramName}
d) ${param["paramName"]}
Answer: b) ${param.paramName}
42. Which JSTL tag is used to include content from an external resource?
a) <c:import>
b) <c:include>
c) <c:parse>
d) <c:content>
Answer: a) <c:import>
46. Which implicit object represents the context of a custom tag invocation?
a) pageContext
b) tagContext
c) tagContextInfo
d) customContext
Answer: a) pageContext
47. How do you access a session attribute using Expression Language (EL)?
a) ${session.getAttribute("attrName")}
b) ${attrName}
c) ${attr.getAttribute("attrName")}
d) ${session["attrName"]}
Answer: a) ${session.getAttribute("attrName")}
c) <responseType>
d) <%@ page contentType="...">
Answer: d) <%@ page contentType="...">
a) request
b) response
c) session
d) application
Answer: d) application
I hope you find these additional questions helpful for your preparation!
Web-based Java Programming MCQ 1
J2EE Overview J2EE Container Packaging Web applications J2EE compliant web application
Deployment tools. Web application life cycle Deploying web applications. Web Services Support
Deployment
Sure! Below are 50 multiple-choice questions (MCQs) along with their answers on the topics
of J2EE overview, J2EE container, packaging web applications, J2EE compliant web
application, deployment tools, web application life cycle, deploying web applications, and
web services support.
c) JSP Container
d) Application Container
Ans: d) Application Container
a) JAR
b) WAR
c) EAR
d) RAR
Ans: b) WAR
4. Q: Which of the following files is mandatory in a J2EE-compliant web application?
a) web.xml
b) web.html
c) web.jsp
d) web.config
Ans: a) web.xml
d) Microsoft IIS
Ans: d) Microsoft IIS
7. Q: Which J2EE component is responsible for managing business logic and data
persistence?
a) Servlet
b) JSP
c) EJB
d) HTML
Ans: c) EJB
8. Q: Which tool is commonly used for deploying J2EE applications on application servers?
a) JDK
b) Eclipse
c) Apache Tomcat
d) Apache Maven
Ans: b) Eclipse
9. Q: Which file is used to specify the deployment settings for an EJB module?
a) web.xml
b) ejb-jar.xml
c) application.xml
d) ejb-config.xml
Ans: b) ejb-jar.xml
12. Q: Which deployment descriptor is used to define the resources and security settings for
a J2EE application?
a) web.xml
b) ejb-jar.xml
c) application.xml
d) j2ee.xml
Ans: c) application.xml
13. Q: Which directory is typically used to store JSP files and static content like images and
CSS files in a web application?
a) WEB-INF
b) META-INF
c) WEB-INFO
d) CONTENT
Ans: a) WEB-INF
15. Q: Which of the following is NOT part of the web application life cycle?
a) Initialization
b) Execution
c) Destroying
d) Compilation
Ans: d) Compilation
17. Q: Which protocol is commonly used for communication between web services in J2EE?
a) HTTP
b) FTP
c) SMTP
d) TCP
Ans: a) HTTP
a) Platform-independent
b) Secure
c) Scalable
d) Monolithic
Ans: d) Monolithic
19. Q: Which tool can be used for managing and monitoring J2EE applications in a
production environment?
a) Apache Tomcat
b) Eclipse
c) JConsole
d) IntelliJ IDEA
Ans: c) JConsole
21. Q: Which of the following statements is true about a J2EE-compliant web application?
a) It can only run on Windows servers.
b) It can be deployed in a standalone mode without any application server.
c) It cannot interact with external databases.
23. Q: Which J2EE component is responsible for handling HTTP requests and generating
dynamic content?
a) Servlet
b) EJB
c) JSP
d) Applet
Ans: a) Servlet
24. Q: What is the main advantage of using a J2EE-compliant application server for deploying
web applications?
a) It simplifies the development process.
b) It improves the security of the application.
c) It provides scalability and high availability.
d) It is free and open-source.
Ans: c) It provides scalability and high availability.
25. Q: The J2EE platform provides a set of APIs for building and deploying:
a) Desktop applications
b) Mobile applications
c) Web applications
d) Game applications
Ans: c) Web applications
Apologies for the oversight. Here are the remaining 25 multiple-choice questions (MCQs)
along with their answers on the topics of J2EE overview, J2EE container, packaging web
applications, J2EE compliant web application, deployment tools, web application life cycle,
deploying web applications, and web services support:
27. Q: Which component is responsible for rendering the user interface in a J2EE web
application?
a) Servlet
b) JSP
c) EJB
d) JDBC
Ans: b) JSP
b) Configuration files
c) Images and CSS files
d) Java source code
Ans: b) Configuration files
30. Q: Which file is used to specify the mapping between URL patterns and servlets in a J2EE
web application?
a) web.xml
b) servlet-mapping.xml
c) url-mapping.xml
d) servlet.xml
Ans: a) web.xml
31. Q: Which tool can be used to package a J2EE application into a WAR or EAR file?
a) Eclipse
b) Maven
c) Ant
d) IntelliJ IDEA
Ans: b) Maven
Ans: a) XML
c) SOAP message
d) RESTful API
Ans: b) Distributed component
a) Apache Tomcat
b) JBoss
c) WebLogic
d) MySQL
Ans: d) MySQL
38. Q: Which of the following is NOT true about a J2EE-compliant web application?
a) It can be accessed from any web browser.
b) It follows the MVC (Model-View-Controller) architecture.
c) It requires a J2EE application server for deployment.
b) WEB-INF directory
c) META-INF directory
d) LIB directory
Ans: b) WEB-INF directory
Ans: c) Tomcat
Ans: b) .jsp
43. Q: Which of the following is NOT a valid web application directory in J2EE?
a) WEB-INF
b) META-INF
c) WEB-INFO
d) lib
Ans: c) WEB-INFO
45. Q: The "lib" directory in a J2EE web application is used for storing:
a) Java source code
b) Java class files
c) Configuration files
d) Library JAR files
Ans: d) Library JAR files
46. Q: Which protocol is commonly used for web services communication in J2EE?
a) XML-RPC
b) SOAP
c) JSON
d) UDP
Ans: b) SOAP
48. Q: Which of the following is NOT true about a J2EE-compliant application server?
a) It hosts J2EE components
49. Q: The "servlet-api.jar" contains the necessary classes and interfaces for developing:
a) JSP pages
b) EJBs
c) JDBC connections
d) Servlets
Ans: d) Servlets
50. Q: Which of the following is a web service protocol that supports data interchange in
XML format?
a) SOAP
b) REST
c) JSON-RPC
d) RMI
Ans: a) SOAP
I hope you find these questions helpful for your preparation! If you have any further
questions or need more information, feel free to ask. Good luck with your studies!
Web-based Java Programming MCQ 2
Servlets: Dynamic Content Generation Advantages of Servlets over CGI Servlet Life cycle
Servlet API & Deployment Servlet Annotations The Servlet interface The HttpServlet,
HttpServletRequest, HttpServletResponse
Answer: a
c) Platform independence
d) Better security
Answer: a
3. Which component of the Servlet API handles communication between the server and the
servlet?
a) ServletResponse
b) ServletContext
c) ServletConfig
d) HttpServletRequest
Answer: d
d) setup()
Answer: a
Answer: a
Answer: a
a) Servlet annotations
b) Servlet life cycle methods
c) URL mappings for servlets
d) HTTP methods for servlets
Answer: c
d) @WebConfig
Answer: b
12. Which method is called when a servlet is being taken out of service?
a) stop()
b) end()
c) destroy()
d) terminate()
Answer: c
13. What is the default HTTP method used by browsers when making a request?
a) GET
b) POST
c) PUT
d) DELETE
Answer: a
b) ServletResponse
c) ServletRequest
d) HttpServletRequest
Answer: d
15. In which phase of the Servlet life cycle does the container call the init() method?
a) Initialization phase
b) Service phase
c) Destruction phase
d) Loading phase
Answer: a
Answer: c
a) 200
b) 404
c) 500
d) 302
Answer: a
20. Which method of the HttpServlet class is responsible for handling HTTP POST requests?
a) doPost()
b) doGet()
c) service()
d) handlePost()
Answer: a
21. Which of the following HTTP headers is used for browser caching?
a) Cache-Control
b) Content-Type
c) Set-Cookie
d) Expires
Answer: a
22. How can you obtain the query parameters from a GET request in a servlet?
a) By calling getParameter() on the HttpServletRequest object
b) By calling getParameters() on the HttpServletRequest object
c) By calling getQueryParameters() on the HttpServletRequest object
23. What is the correct way to obtain the value of a cookie in a servlet?
a) request.getCookie("cookieName")
b) request.cookie("cookieName")
c) request.get("cookieName")
d) request.getCookies("cookieName")
Answer: a
24. Which method is used to set the HTTP status code of the response?
a) setStatus()
b) setStatusCode()
c) setResponseCode()
d) setHTTPStatus()
Answer: a
25. In a servlet, how can you set content type and character encoding for the response?
a) By calling setContentType() and setCharset() methods
26. Which method of the HttpServlet class is called by default when a client sends an HTTP
request?
a) doGet()
b) doPost()
c) doService()
d) service()
Answer: d
c) 204
d) 202
Answer: a
30. How can you get the IP address of the client in a servlet?
a) request.getRemoteHost()
b) request.getClientAddress()
c) request.getIP()
d) request.getRemoteAddr()
Answer: d
32. What is the purpose of the "web.xml" deployment descriptor in a web application?
a) To define the database schema
33. Which method of the HttpServlet class is responsible for handling HTTP PUT requests?
a) doPut()
b) doUpdate()
c) doModify()
d) doChange()
Answer: a
34. What is the use of the "response.sendRedirect()" method in a servlet?
a) To include the content of another servlet
35. Which of the following HTTP methods is idempotent, meaning it has the same result no
matter how many times it is repeated?
a) GET
b) POST
c) PUT
d) DELETE
Answer: a
36. How can you retrieve the HTTP request method (GET, POST, etc.) in a servlet?
a) request.getMethod()
b) request.getHttpMethod()
c) request.getRequestMethod()
d) request.getHttpRequestMethod()
Answer: a
37. Which method of the HttpServlet class is called to handle HTTP DELETE requests?
a) doDelete()
b) doErase()
c) doDestroy()
d) doRemove()
Answer: a
38. What does the "contextInitialized" method do in a ServletContextListener?
a) It is called when the servlet is initialized.
b) It is called when the servlet is destroyed.
39. How can you read data submitted by a client in the request body of a POST method in a
servlet?
a) Using the "request.getRequestBody()" method
Answer: a
41. Which method of the HttpServlet class is responsible for handling HTTP HEAD requests?
a) doHead()
b) doHeader()
c) doRequest()
d) doGet()
Answer: a
42. How can you set an attribute in the ServletContext object?
a) By calling setAttribute() on the ServletContext object
b) By calling addAttribute() on the ServletContext object
44. Which method of the HttpServlet class is responsible for handling HTTP OPTIONS
requests?
a) doOptions()
b) doList()
c) doQuery()
d) doChoice()
Answer: a
48. Which method is called by the servlet container when the servlet is about to be removed
from memory?
a) end()
b) stop()
c) destroy()
d) unload()
Answer: c
49. What is the role of the "web.xml" file in the deployment process of a web application?
a) It contains the Java source code for servlets.
b) It defines the presentation layer of the application.
I hope these questions help you in your preparation! If you have any more specific queries or
need further assistance, feel free to ask. Good luck with your studies!
Exception Handling Servlet, DAO, POJO DB Layers Session Session Management Session
Tracking with o Cookies o HttpSession Request Dispatcher Page Navigation Complete Case study
Servlet Based
Sure! Here are 50 multiple-choice questions (MCQs) on the topics you mentioned along with
their answers:
Answer: c
2. Which layer in the Servlet architecture is responsible for interacting with the database?
a) Servlet Layer
b) DAO (Data Access Object) Layer
Answer: b
3. In the context of Java Servlets, what does DAO stand for?
a) Data Access Object
Answer: a
Answer: d
5. Which interface allows you to manage and maintain session information in a Java Servlet?
a) ServletSession
b) ServletContext
c) HttpSession
d) SessionManager
Answer: c
Answer: c
d) ServletSession
Answer: c
Answer: c
c) session.destroy()
d) session.end()
Answer: a
10. How are HttpSession objects associated with users in Java Servlets?
a) They are linked to the client's IP address.
b) They are stored in server memory.
c) They are passed as URL parameters in each request.
Answer: d
Answer: c
Answer: d
14. In a Java Servlet, how can you obtain a reference to the HttpSession object?
a) By calling request.getSession()
b) By calling response.getSession()
c) By calling session.getHttpSession()
d) By calling session.getCurrentSession()
Answer: a
15. What is the default session tracking mechanism used by Java Servlets?
a) Cookies
b) URL Rewriting
Answer: a
16. Which of the following is NOT a valid way to maintain session state in a Java Servlet?
a) URL Rewriting
b) Hidden Form Fields
c) Cookies
d) Stateless sessions
Answer: d
17. Which method is used to retrieve the value of a specific session attribute in Java
Servlets?
a) session.getAttribute()
b) session.getValue()
c) session.getParameter()
d) session.retrieve()
Answer: a
c) The servlet container looks for a cookie named "JSESSIONID" to identify the user's
session.
Answer: b
19. How can you ensure that a servlet is only accessible to users with a valid session?
Answer: d
20. What is the default timeout value for a session in Java Servlets?
a) 5 minutes
b) 15 minutes
c) 30 minutes
d) 60 minutes
Answer: c
21. In Java Servlets, what is the purpose of the "session.setMaxInactiveInterval()" method?
a) To set the maximum number of concurrent sessions allowed
b) To define the maximum number of session attributes
c) To set the time (in seconds) after which the session will be invalidated due to inactivity
d) To configure the session tracking mechanism
Answer: c
22. Which of the following is NOT a valid method to remove an attribute from the
HttpSession?
a) session.removeAttribute()
b) session.invalidate()
c) session.putValue()
d) session.removeValue()
Answer: c
23. In the context of Java Servlets, what does the term "POJO" stand for?
a) Plain Old Java Object
b) Plain Object with JSON Output
Answer: a
24. Which layer in the Servlet architecture contains the business logic and interacts with the
database?
a) Servlet Layer
b) DAO Layer
c) POJO Layer
d) Presentation Layer
Answer: c
25. What is the main role of the Servlet layer in the application architecture?
a) To handle user input validation
b) To manage sessions and cookies
c) To handle HTTP requests and responses
Certainly! Here are 50 more multiple-choice questions (MCQs) on the topics you mentioned:
Answer: b
27. Which method is used to get the ServletContext object in a Java Servlet?
a) getApplication()
b) getServletContext()
c) getContext()
d) getServletConfig()
Answer: b
d) Page scope
Answer: c
a) HttpServlet
b) HttpSession
c) ServletContext
d) No specific interface is required
Answer: d
30. Which of the following is NOT a session tracking mechanism in Java Servlets?
a) URL Rewriting
b) Hidden Form Fields
c) ServletConfig
d) Cookies
Answer: c
32. Which method is used to retrieve cookies from an HTTP request in a Java Servlet?
a) request.getCookie()
b) request.getCookies()
c) request.getCookieList()
d) request.getAllCookies()
Answer: b
c) 16 KB
d) 32 KB
Answer: a
34. How can you set the maximum age of a cookie in Java Servlets?
a) By using the "max-age" attribute of the cookie
b) By calling the "setMaxAge()" method on the Cookie object
c) By setting the "maxAge" property in the web.xml file
d) By passing the "maxAge" parameter in the response.setHeader() method
Answer: b
35. What happens if you set the maximum age of a cookie to a negative value in Java
Servlets?
a) The cookie is deleted immediately.
b) The cookie will persist until the user closes the browser.
c) An exception is thrown.
d) The server generates a new cookie.
Answer: a
36. In Java Servlets, how can you access request parameters sent via a POST request?
a) By calling request.getParameter()
b) By calling request.getQueryString()
c) By calling request.getParameterMap()
d) By calling request.getPostData()
Answer: c
37. What is the purpose of the "multipart/form-data" encoding type in HTML forms?
a) To allow multiple forms to be submitted simultaneously
b) To encode form data in a URL-safe format
c) To handle file uploads in HTML forms
d) To compress form data before transmission
Answer: c
38. How can you access the uploaded file data in a Java Servlet?
a) By using the "request.getUploadedFile()" method
b) By calling request.getFile()
c) By using the "request.getPart()" method
d) By calling request.getInputStream()
Answer: c
39. What is the purpose of the "request.getPart()" method in Java Servlets?
a) To retrieve form data sent via a POST request
b) To retrieve session data associated with the current request
Answer: c
40. Which method is used to send a redirect response to the client in Java Servlets?
a) response.sendRedirect()
b) response.sendRedirectRequest()
c) response.forward()
d) response.sendForward()
Answer: a
41. What is the HTTP status code for a successful redirect response in Java Servlets?
a) 200
b) 302
c) 404
d) 500
Answer: b
42. Which of the following is true about URL rewriting for session tracking in Java Servlets?
Answer: b
43. What is the purpose of the "response.encodeURL()" method in Java Servlets?
a) To encode the URL before redirecting the client
Answer: c
d) response.navigateForward()
Answer: c
45. What is the difference between "forward" and "redirect" in Java Servlets?
Answer: d
Answer: b
47. Which method is used to store an attribute in the request scope in Java Servlets?
a) request.storeAttribute()
b) request.setAttribute()
c) request.saveAttribute()
d) request.addAttribute()
Answer: b
48. What is the default scope of an attribute in Java Servlets if no scope is specified?
a) Request scope
b) Session scope
c) Application scope
d) Page scope
Answer: a
Answer: a