11. 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.

11.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 {
    ... ;
  }
};

11.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 {
    __________________________;
  }
};

11.3. Question 3: Methods to add in JS class model

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 class model? Select one or many:

checkName( n: String): ConstraintViolation

getName(): String

setName( n: String)

checkNameAsId( n: String): ConstraintViolation

checkNameAsId( n: String): ConstraintViolation

checkName( n: String): ConstraintViolation

setName( n: String)

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

11.4. Question 4: Implementing 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