12. Quiz Questions

If you would like to look up the answers for the following quiz questions, you can check our discussion forum. If you don't find an answer in the forum, you may create a post asking for an answer to a particular question.

If you would like to do interactive quizzes with online feedback and with the option to ask a coach, then you should enrol in the introductory online course (book parts 1-3) offered by our partner developer-competence.com.

12.1. Question 1: Validation in Setter (1)

Complete the following setter code fragment:

Book.prototype.setIsbn = function (isbn) {
  var validationResult = ______________________________;
  if (validationResult instanceof NoConstraintViolation) {
    this.isbn = isbn;
  } else {
    ... ;
  }
};

12.2. Question 2: Validation in Setter (2)

Complete the following setter code fragment:

Book.prototype.setIsbn = function (isbn) {
  var validationResult = ...;
  if (validationResult instanceof NoConstraintViolation) {
    this.isbn = isbn;
  } else {
    __________________________;
  }
};

12.3. Question 3: Methods to Add in JS Data Model

Figure 6.4. Which methods to add?

Which methods to add?

Consider the simple information design model shown in the class diagram.

Which of the following methods have to be added to the Publisher class in a corresponding JavaScript data model? Select one or many:

  1. checkName( n: String): ConstraintViolation

  2. getName(): String

  3. setName( n: String)

  4. checkNameAsId( n: String): ConstraintViolation

  5. checkNameAsId( n: String): ConstraintViolation

  6. checkName( n: String): ConstraintViolation

  7. setName( n: String)

Notice that an underlined method name denotes a class-level ("static") method.

12.4. Question 4: Implementing Constraints

Figure 6.5. How to implement these constraints?

How to implement these constraints?

Consider the constraints specified for the object type Person in the class diagram.

Which of the following JavaScript code fragments implements these constraints? Select one:

  1. O

    if (name === undefined) {
      return "A name is required!";
    } else if (typeof( name) !== "string" || name.trim() === "") {
      return "Name must be a non-empty string!";
    } else if (age !== undefined) {
      if (!Number.isInteger( age) || age < 0) {
        return "Age must be a non-negative integer!"
      }
    } else return "";  // no error
  2. O

    if (name === undefined) {
      return "A name is required!";
    } else if (typeof( name) !== "string") {
      return "Name must be a string!";
    } else if (age !== undefined) {
      if (!Number.isInteger( age) || age < 0) {
        return "Age must be a non-negative integer!"
      }
    } else return "";  // no error
  3. O

    if (name === undefined) {
      return "A name is required!";
    } else if (typeof( name) !== "string" || name.trim() === "") {
      return "Name must be a non-empty string!";
    } else if (age < 0) {
        return "Age must be a non-negative integer!"
    } else return "";  // no error
  4. O

    if (name === undefined) {
      return "A name is required!";
    } else if (typeof( name) !== "string" || name.trim() === "") {
      return "Name must be a non-empty string!";
    } else if (age === undefined || !Number.isInteger( age) || age < 0) {
        return "Age must be a non-negative integer!"
    } else return "";  // no error