









































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A practice exam for spring web application developers. It includes multiple-choice questions covering topics such as inversion of control (ioc), applicationcontext, bean configuration, annotations, spring expression language (spel), aspect-oriented programming (aop), jdbctemplate, and transaction management. Each question is followed by the correct answer and a brief explanation. This practice exam is designed to help developers assess their knowledge and prepare for certification or job interviews. It covers key concepts and best practices in spring web application development, offering valuable insights and practical knowledge. The questions are structured to test understanding of core spring principles and their application in real-world scenarios. The document serves as a useful resource for both beginners and experienced developers looking to enhance their skills in spring web development.
Typology: Exams
1 / 81
This page cannot be seen from the preview
Don't miss anything!










































































Question 1. What is the main advantage of using Inversion of Control (IoC) in Spring applications? A) Reduces memory usage B) Allows for static configuration C) Decouples object creation and management from business logic D) Increases application speed Answer: C Explanation: IoC decouples object creation and management from business logic, making applications easier to maintain and test. Question 2. Which Spring container implementation is capable of supporting advanced features like internationalization and event propagation? A) BeanFactory B) ApplicationContext C) ServletContext D) WebApplicationContext Answer: B Explanation: ApplicationContext provides advanced features beyond BeanFactory, such as event propagation and internationalization. Question 3. In XML-based Spring configuration, how do you define a bean with a specific class? A)
Question 4. What annotation is used to mark a Java class as a configuration class in Spring? A) @Bean B) @Config C) @Configuration D) @SpringConfig Answer: C Explanation: @Configuration marks a class as a source of bean definitions for the Spring IoC container. Question 5. Which method allows you to retrieve a bean from the ApplicationContext by its name? A) getBeanByName() B) retrieveBean() C) getBean(String name) D) findBean() Answer: C Explanation: The getBean(String name) method retrieves a bean by its name from ApplicationContext. Question 6. What is the default scope of a Spring bean? A) Prototype B) Request C) Session D) Singleton Answer: D Explanation: By default, Spring beans are singleton scoped, meaning one instance per container. Question 7. Which annotation injects a property value from an external configuration file into a Spring bean?
B) To define REST endpoints C) To handle database queries D) To create custom exceptions Answer: A Explanation: SpEL is used for evaluating expressions dynamically in Spring configuration and annotations. Question 11. Which annotation marks a class as a Spring-managed component? A) @Service B) @Component C) @Repository D) @Controller Answer: B Explanation: @Component is the generic stereotype annotation for Spring-managed beans. Question 12. What does @ComponentScan do in a Spring application? A) Scans for configuration files B) Automatically discovers and registers beans annotated with stereotype annotations C) Starts the application D) Manages transactions Answer: B Explanation: @ComponentScan instructs Spring to scan a package for beans annotated with @Component, @Service, etc. Question 13. What is the main difference between field injection and constructor injection using @Autowired? A) Field injection is safer
B) Constructor injection is preferred for mandatory dependencies C) Constructor injection does not allow optional dependencies D) Field injection supports circular dependencies Answer: B Explanation: Constructor injection is preferred because it ensures all required dependencies are provided at object creation. Question 14. How does @Qualifier help when multiple beans of the same type are present? A) It disables autowiring B) It specifies which bean to inject C) It marks beans as optional D) It creates a new bean Answer: B Explanation: @Qualifier specifies which bean to inject when multiple candidates are available. Question 15. What does the @PostConstruct annotation indicate? A) Method should be called before bean is destroyed B) Method should be called after bean initialization C) Bean should be injected D) Method should be ignored Answer: B Explanation: @PostConstruct marks a method to be called after the bean’s properties are set. Question 16. Which interface allows custom code to run after all bean properties are set? A) InitializingBean B) DisposableBean
D) An exception thrown Answer: A Explanation: A join point is a point during execution, such as a method invocation, where advice can be applied. Question 20. How is an aspect defined in Spring? A) As a bean annotated with @Aspect B) Using @Service C) As a configuration class D) With @ComponentScan Answer: A Explanation: Aspects are beans annotated with @Aspect to define cross-cutting concerns. Question 21. Which advice type runs before a join point method executes? A) AfterReturning B) Before C) Around D) AfterThrowing Answer: B Explanation: Before advice runs before the target method execution. Question 22. Which advice type executes after a method returns normally? A) Before B) AfterReturning C) AfterThrowing D) Around
Answer: B Explanation: AfterReturning advice executes after successful completion of the method. Question 23. In AspectJ pointcut expressions, what does “execution(* com.example.service..(..))” match? A) All classes in com.example.service B) All methods in com.example.service package C) Only public methods D) Only constructors Answer: B Explanation: This matches all methods in any class within com.example.service package. Question 24. What is the difference between JDK dynamic proxies and CGLIB proxies in Spring AOP? A) JDK proxies work with interfaces, CGLIB with classes B) CGLIB proxies are faster C) JDK proxies support private methods D) CGLIB proxies do not support inheritance Answer: A Explanation: JDK dynamic proxies require interfaces, CGLIB proxies work by subclassing concrete classes. Question 25. What is the primary purpose of the JdbcTemplate class? A) To manage HTTP requests B) To simplify JDBC database operations C) To parse XML files D) To manage transactions Answer: B
Question 29. Which annotation enables transaction management in Spring? A) @EnableTransactions B) @TransactionManager C) @EnableTransactionManagement D) @Transact Answer: C Explanation: @EnableTransactionManagement activates annotation-driven transaction management. Question 30. What does the @Transactional annotation do? A) Creates a new bean B) Marks a method or class as transactional C) Disables transactions D) Specifies bean scope Answer: B Explanation: @Transactional marks methods or classes as transactional. Question 31. What does transaction propagation behavior ‘REQUIRES_NEW’ mean? A) Joins existing transaction if present B) Always starts a new transaction, suspending any existing one C) Does not support transactions D) Commits after each operation Answer: B Explanation: REQUIRES_NEW starts a new transaction and suspends any current one.
Question 32. What is the default transaction isolation level in Spring when using a relational database? A) READ_UNCOMMITTED B) READ_COMMITTED C) SERIALIZABLE D) REPEATABLE_READ Answer: B Explanation: READ_COMMITTED is the default isolation level for most relational databases. Question 33. How do you specify rollback rules for a transaction in Spring? A) Using @Rollback B) By configuring rollbackFor in @Transactional C) By setting throwOnRollback D) By using @TransactionRule Answer: B Explanation: rollbackFor in @Transactional specifies exceptions that trigger rollback. Question 34. Which class allows for programmatic transaction management? A) TransactionTemplate B) TransactionManager C) TransactionalBean D) TransactionProcessor Answer: A Explanation: TransactionTemplate enables programmatic control of transactions. Question 35. What is the primary use of Spring Data JPA?
B) Automatically configures JPA entities and repositories C) Deletes the bean D) Throws an exception Answer: B Explanation: Spring Boot configures JPA entities and repositories if a DataSource bean is found. Question 39. Which annotation is used to create RESTful web controllers in Spring MVC? A) @RestController B) @WebController C) @APIController D) @Controller Answer: A Explanation: @RestController is used for RESTful web controllers in Spring MVC. Question 40. What does @RequestMapping specify in a Spring MVC controller? A) Security rules B) URL pattern for handler methods C) Bean scope D) Transaction boundaries Answer: B Explanation: @RequestMapping maps HTTP requests to handler methods. Question 41. Which annotation is typically used to map HTTP GET requests in Spring MVC? A) @Get B) @GetMapping
C) @RequestMapping(GET) D) @HTTPGet Answer: B Explanation: @GetMapping is a convenient annotation to map GET requests. Question 42. How do you bind URL path variables to method parameters in Spring MVC? A) @PathParam B) @PathVariable C) @RequestParam D) @BindVariable Answer: B Explanation: @PathVariable binds URL segments to method parameters. Question 43. Which annotation is used to bind HTTP request parameters to method parameters? A) @RequestParam B) @Param C) @BindRequest D) @Parameter Answer: A Explanation: @RequestParam binds query parameters to method parameters. Question 44. What does @RequestBody do in a Spring MVC controller method? A) Maps HTTP headers B) Binds the request body to a method parameter C) Maps cookies
Answer: B Explanation: @ControllerAdvice with @ExceptionHandler methods manages global exception handling. Question 48. What does @ModelAttribute do in Spring MVC? A) Maps request parameters to a model object B) Specifies HTTP methods C) Handles exceptions D) Maps headers Answer: A Explanation: @ModelAttribute binds request parameters to a model object. Question 49. What is the purpose of the Model in Spring MVC? A) To store view templates B) To pass data between controller and view C) To manage transactions D) To store configuration Answer: B Explanation: The Model holds data to be rendered by the view. Question 50. Which annotation would you use to return a view name from a controller method? A) @ViewName B) @ReturnView C) None; just return a string D) @View Answer: C
Explanation: In Spring MVC, returning a String from a controller method specifies the view name. Question 51. What does @SessionAttributes do in Spring MVC? A) Stores beans in the session scope B) Specifies which model attributes should be stored in the session C) Maps session variables D) Sets bean scope Answer: B Explanation: @SessionAttributes specifies model attributes to be stored in the session. Question 52. How do you enable CORS support in Spring MVC? A) By using @CrossOrigin on controller methods B) By configuring CORS in web.xml C) By using @CorsSupport D) By annotating with @EnableCORS Answer: A Explanation: @CrossOrigin enables CORS for controller methods. Question 53. What is the role of DispatcherServlet in Spring MVC? A) Handles database transactions B) Dispatches requests to appropriate controllers C) Manages bean lifecycle D) Handles view rendering only Answer: B Explanation: DispatcherServlet routes requests to controllers and handles the overall workflow.
Question 57. How do you run a Spring Boot application? A) Using a main method with SpringApplication.run() B) Using runApp() C) Through web.xml D) Using @RunApp Answer: A Explanation: SpringApplication.run() starts the Spring Boot application. Question 58. What is actuator in Spring Boot? A) A tool for monitoring and managing your application B) A database connector C) A view resolver D) A bean manager Answer: A Explanation: Actuator provides endpoints for monitoring and managing the application. Question 59. How do you expose health check endpoints in Spring Boot? A) By enabling Actuator and accessing /actuator/health B) By using @HealthCheck C) By configuring in web.xml D) Through @EnableHealth Answer: A Explanation: Actuator exposes endpoints like /actuator/health. Question 60. Which annotation injects the value of a property into a Spring bean?
A) @Value B) @Inject C) @Autowired D) @ConfigValue Answer: A Explanation: @Value is used to inject property values into beans. Question 61. What is the role of @Repository in Spring? A) To mark beans as controllers B) To define service classes C) To indicate data access objects and enable exception translation D) To manage transactions Answer: C Explanation: @Repository marks DAO classes and enables exception translation. Question 62. How does Spring handle circular dependencies for beans injected via constructor? A) Throws a BeanCurrentlyInCreationException B) Resolves automatically C) Ignores the dependency D) Delays injection Answer: A Explanation: Constructor injection cannot resolve circular dependencies and throws an exception. Question 63. What is the difference between eager and lazy bean initialization? A) Eager beans are initialized at startup, lazy beans upon first use