Close
« Tutorial

Spring MVC - Auto Generating Model Attribute Names MCQ

0/13
0 ✓
0 ✗

1. What happens when you add an object to a Model using addAttribute(Object) without giving a name?

2. What is the name generation rule for an object that is not a collection or array?

3. What attribute name is generated for the following code?

model.addAttribute(10);

4. A non-empty list of String values is added without a name. What attribute name is generated?

List<String> names =
    Arrays.asList("Ann", "Bob");
model.addAttribute(names);

5. How does the name generation find the element type of a collection?

6. An array of strings is added without a name. What attribute name is generated?

model.addAttribute(new String[]{"a", "b"});

7. What attribute name is generated for an object of this custom class?

class ProductDetail { }

model.addAttribute(new ProductDetail());

8. What are the generated names for a, b and c, in this order?

Object a = new TreeMap<>();
Object b = new HashSet<>(List.of("x", "y"));
Object c = 'z';

9. What will the page show for the three expressions?

@RequestMapping("/")
public String show(Model model) {
    model.addAttribute("hi");
    model.addAttribute(7);
    model.addAttribute(
        Arrays.asList("a", "b"));
    return "page";
}
<p>${string}</p>
<p>${integer}</p>
<p>${stringList}</p>

10. A Model needs to hold two different String values. What is the best approach?

11. What names are generated when this code runs?

List<Object> items = List.of("a", 5);
model.addAllAttributes(items);

12. Which Spring utility does the Model use to generate attribute names?

Join