Ads by Google
Get This Adsense Pop-up Window


Friday, 18 March 2016

Spring MVC Test Answer

1. Which of the following code samples will get the ServletContext inside an Interceptor?
Answers:
  1. @Autowired ServletContext context;
  2. request.getSession().getServletContext();/li>
  3. setServletContext(ServletContext context) { this.context = context; }
  4. request.getSession().getServletContext();
2. How can an HTTP 404 status code be returned from a Spring MVC Controller?
Answers:
  1. Throwing a ResourceNotFoundException declared with the @ResponseStatus annotation.<>
  2. Throwing an HttpRequestMethodNotSupportedException.
  3. Configuring <context:annotation-config> in the Spring configuration XML document to send a 404 status for a controller via its “returnCode” argument.
  4. Having the method accept HttpServletResponse as a parameter, so that setStatus(404) can be called on it.
3. Which of the following interfaces can be implemented to interact with a container’s management of the bean lifecycle?
Answers:
  1. InitializingBean
  2. IntializeableBean
  3. DisposableBean
  4. DisposingBean
4. Which of the following can be used to serve static resources while still using DispatchServlet at the site’s root?
Answers:
  1. <mvc:default-resources/>
  2. <mvc:resources/>
  3. <mvc:default-servlet-handler/>
  4. <mvc:view-controller/>
5. Which of the following code samples will correctly return an image in @ResponseBody from a byte[] of image data?
Answers:
  1. @RequestMapping(“/photo”) public ResponseEntity<byte[]> testphoto() throws IOException { InputStream in = servletContext.getResourceAsStream(“/images/no_image.jpg”); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED); }
  2. @ResponseBody @RequestMapping(“/photo”, method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public byte[] testphoto() throws IOException { InputStream in = servletContext.getResourceAsStream(“/images/no_image.jpg”); return IOUtils.toByteArray(in); }
  3. @RequestMapping(“/photo”) public void photo(HttpServletResponse response) throws IOException { response.setContentType(“image/jpeg”); InputStream in = servletContext.getResourceAsStream(“/images/no_image.jpg”); IOUtils.copy(in, response.getOutputStream()); }
  4. @ResponseBody @RequestMapping(“/photo2) public byte[] testphoto() throws IOException { InputStream in = servletContext.getResourceAsStream(“/images/no_image.jpg”); return IOUtils.toByteArray(in); }
6. Which of the following annotations are supported by classes with the @Bean annotation?
Answers:
  1. @PostConstruct
  2. @PreDestroy
  3. @PreConstruct
  4. @PostDestroy
7. Which of the following statements is false?
Answers:
  1. Spring MVC provides both declarative and programmatic transaction management.
  2. The transaction-manager attribute in the transactional advice (<tx:advice/>) is required if the bean name of the
  3. PlatformTransactionManager that is being wired is transactionManager.
  4. By default, a transaction is only marked for rollback in the case of runtime unchecked exceptions.
  5. None of these.
8. Which of the following statements is true about the HandlerExceptionResolver class?
Answers:
  1. Any Spring bean that implements HandlerExceptionResolver will be used to intercept and process any exception raised that was handled by a Controller.
  2. @ExceptionHandler methods can be injected with the model.
  3. DefaultHandlerExceptionResolver converts standard Spring exceptions and converts them to HTTP Status Codes.
  4. None of these.
9. Which of the following statements is true about method arguments that have an @ModelAttribute annotation?
Answers:
  1. It indicates that the argument should be retrieved from the model.
  2. If the argument is not present in the model, it should be added to the model first, and then instantiated.
  3. If the argument is not present in the model, it should be instantiated first, and then added to the model.
  4. Model attributes have to be explicitly added when using @ModelAttribute.
10. Given the following method:
@RequestMapping(method=RequestMethod.GET, value=”/fooBar”)
public ResponseEntity<String> fooBar2() {
String json = “jsonResponse”;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<String>(json, responseHeaders, HttpStatus.CREATED);
}
Which of the following statements is correct?
Answers:
  1. It allows access to request and response headers.
  2. It doesn’t allow access to request and response headers.
  3. It returns a JSON String and sets the mimetype to text/plain.
  4. It returns a JSON String and sets the mimetype to text/javascript.
11. Which of the following classes provides built-in pagination functionality in SpringMVC?
Answers:
  1. PageListSorter
  2. PageListContext
  3. PagedListHolder
  4. Spring MVC doesn’t include a built-in class for handling pagination.
12. Given the method below:
@RequestMapping(value = “/foo”, method = RequestMethod.GET)
public final String foo(HttpServletRequest request, BindingResult bindResult, ModelMap model) {
model.addAttribute(“abc”, 123);
return “foo”;
}
When the view is displayed in the browser, it’s URL is “http://mydomain/foo?abc=123”.
Which of the following statements is true?
Answers:
  1. The attribute appears as string name-value pairs in the URL because the @ModelAttribute annotation is used in the controller.
  2. Adding “model.asMap().clear();” will prevent the attribute name-value pair from appearing in the URL.
  3. The attribute name-value pair will be included in the URL, regardless of the use of @ModelAttribute in the controller.
  4. The attribute name-value pair cannot be excluded from being displayed in the URL.
13. Which of the following is true about the use of <context:annotation-config /> in a servlet?
Answers:
  1. <context:annotation-config> activates many different annotations in beans, whether they are defined in XML or through component scanning.
  2. <context:annotation-config> declares explicit support for annotation-driven MVC controllers.
  3. <context:annotation-config> adds support for declarative validation via @Valid.
  4. All statements are false.
14. Which of the following are valid sets of constructor arguments for the ModelAndView class? (Select all correct answers.)
Answers:
  1. String viewName
  2. String viewName, Map<String,?> model
  3. String modelName, Map<String,?> model
  4. View view, Map<String,?> model, Object modelObject
15. Which of the following are possible validation methods for user input in Spring MVC?
Answers:
  1. XPath validation
  2. Annotation validation
  3. Programmatic validation
  4. Mixed annotation and programmatic validation
16. Fill in the blank:
The _______ enables the use of the bean element’s attributes, instead of nested <property/> elements, to describe property values and/or collaborating beans.
Answers:
  1. default namespace
  2. c-namespace
  3. p-namespace
  4. namespace
17. Which of the following statements is true about the @RequestMapping annotation?
Answers:
  1. It has a single String parameter.
  2. It has a String[] paramater.
  3. It supports ant-style paths.
  4. It doesn’t support wildcard input.
18. Which of the following statements is true for the configuration of the Spring handler adapter(s) in a Spring MVC application context?
Answers:
  1. Spring MVC defines 2 different request handler adapters by default: HttpRequestHandlerAdapter and SimpleControllerHandlerAdapter.
  2. Request handler adapters need to be defined in the context files.
  3. If at least one request handler adapter is defined the context files, Spring will not create the default adapters.
  4. Using <mvc:annotation-driven /> causes the context to define both AnnotationMethodHandlerAdapter and SimpleControllerHandlerAdapter.
19. What does the following code do?
@RequestMapping(“/{id}/**”)
public void foo(@PathVariable(“id”) int id, HttpServletRequest request) {
String restOfTheUrl = (String) request.getAttribute(
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);

}
Answers:
  1. It retrieves the complete path value after the @RequestMapping and @PathVariable values have been parsed.
  2. It retrieves the complete path value before the @RequestMapping and @PathVariable values have been parsed.
  3. It retrieves the partial path value (after “**”) after the @RequestMapping and @PathVariable values have been parsed.
  4. It retrieves the partial path value (after “**”) before the @RequestMapping and @PathVariable values have been parsed.
20. Which of the following statements is true about the HandlerInterceptor interface?
Answers:
  1. It gets called after the appropriate HandlerAdapter triggers the execution of the handler itself.
  2. It allows exchanging the request and response objects that are handed down the execution chain.
  3. It gets configured in the application context.
  4. It is well-suited for request content and view content handling, such as multipart forms and GZIP compression.
21. Which of the following dependency injection (DI) methodologies are available in Spring MVC?
Answers:
  1. Constructor-based dependency injection
  2. Setter-based dependency injection
  3. Prototype-based dependency injection
  4. Manual dependency injection
22. Which of the following is the default bean scope in Spring MVC?
Answers:
  1. global session
  2. local session
  3. prototype
  4. singleton
23. True or false: a factory class can hold more than one factory method.
Answers:
  1. True
  2. False
24. Which of the following enables custom qualifier annotation types to be registered even if they are not annotated with Spring’s @Qualifier annotation?
Answers:
  1. CustomQualifier
  2. CustomAutowireConfigurer
  3. CustomQualifierType
  4. CustomAutowire
25. Which of the following statements is correct?
Answers:
  1. Explicitly declared handler mappings or declaring <mvc:annotation-driven> are optional when using <mvc:resources>.
  2. <mvc:resources> declares BeanNameUrlHandlerMapping by default.
  3. <mvc:resources> doesn’t declare its own handler mapping.
  4. <mvc:resources> declares only DefaultAnnotationHandlerMapping by default.
26. Which of the following statements is true about the @RequestParam annotation?
Answers:
  1. Providing a “defaultValue” optional element sets the “required” optional element to true.
  2. It indicates that a method parameter should be bound to a web request parameter.
  3. It is unsupported for annotated handler methods in Servlet and Portlet environments.
  4. None of these.
27. Select all authentication methods that are supported by Spring Security by default:
Answers:
  1. Basic Authentication
  2. Digest Access Authentication
  3. Remember-me Authentication
  4. Multi-factor authentication
28. In an annotation-based Spring MVC controller, which of the following are valid ways to set cache headers for a specific path?
Answers:
  1. Ensuring the instance of “AnnotationMethodHandlerAdapter” does not have the “cacheSeconds” property set, and adding an instance of “WebContentInterceptor”.
  2. Adding “final HttpServletResponse response” as a parameter, then setting the header “Cache-Control” to “all-cache”.
  3. Using a Handler Interceptor and using the “postHandle” method provided by it.
  4. Cache headers cannot be set for a specific path.
29. What is the difference between the @Repository and the @Controller annotations in Spring?
Answers:
  1. “@Repository” is used as a stereotype for the persistence layer, while “@Controller” is used as a stereotype for the presentation layer.
  2. “@Repository” is used as a stereotype for the presentation layer, while “@Controller” is used as a stereotype for the persistence layer.
  3. “@Repository” is used as a stereotype for the service layer, while “@Controller” is used as a generic stereotype for any Spring-managed component.
  4. “@Controller” is used as a stereotype for the service layer, while “@Repository” is used as a generic stereotype for any Spring-managed component.
30. Which of the following statements are correct, with respect to using the @PreAuthorize annotation in Spring controller methods?
Answers:
  1. @PreAuthorize does not work with Spring controller methods.
  2. @PreAuthorize works with Spring controller methods.
  3. The “pre-post-annotations” expression should be set to “disabled” in the servlet.xml file.
  4. Using CGLIB proxies are optional when using @PreAuthorize in Spring controller methods.
31. Which of the following is not a built-in Spring MVC bean scope?
Answers:
  1. singleton
  2. request
  3. global session
  4. local session
32. Fill in the blank: _______ is a class-level annotation indicating that an object is a source of bean definitions.
Answers:
  1. @Configuration
  2. @Definition
  3. @Bean
  4. @Scope
33. Regarding the @Resource annotation, which of the following statements is false?
Answers:
  1. It is part of the JSR-250 specification.
  2. By default, Spring interprets its name attribute as the bean name to be injected.
  3. If no name attribute is specified, the default name is derived from the field name or setter method.
  4. It supports injection on bean property setter methods only.
34. Fill in the blank:
When defining a bean that is created with a static factory method, the ______ attribute is used to specify the class containing the static factory method.
Answers:
  1. class
  2. class-name
  3. factory-class
  4. factory-method
35. Which of the following statements is true about the @ModelAttribute annotation?
Answers:
  1. It binds a method parameter or method return value to an anonymous model attribute.
  2. It can be used to expose reference data to a web view.
  3. It is not supported for controller classes with @RequestMapping methods.
  4. It cannot be used to expose command objects to a web view.
36. Fill in the blank:
In Spring’s XML-based configuration, the _______ attribute of the <property/> element specifies a property or constructor argument as a string representation.
Answers:
  1. namespace
  2. name
  3. class
  4. value
37. Which of the following statements is/are true about autowiring in Spring?
Answers:
  1. Multiple constructors a given class may carry the @AutoWired annotation.
  2. Only one constructor of any given class may carry the @AutoWired annotation.
  3. Fields are injected before the construction of a bean.
  4. Fields are injected after the construction of a bean.
38. Regarding dependency resolution, which of the following statements is false?
Answers:
  1. The ApplicationContext is created and initialized with configuration metadata that describes all the beans.
  2. Configuration metadata can only be specified via XML or annotations.
  3. Each property or constructor argument is an actual definition of the value to set, or a reference to another bean in the container.
  4. For each bean, its dependencies are expressed in the form of properties, constructor arguments, or arguments to the static-factory method.

No comments:

Post a Comment

Blog Archive