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

10.1. Question 1: 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 JS 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.

10.2. Question 2: 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

10.3. Question 3: Implementing setter methods

Which of the following code fragments represents a correct setter method for an ID attribute isbn? Select one or more:

  1. O

    Book.prototype.setIsbn = function (i) {
      const validationResult = this.checkIsbn(i);
      if (validationResult instanceof NoConstraintViolation) {
        this.isbn = i;
      } else {
        throw validationResult;
      }
    };
  2. O

    Book.prototype.setIsbn = function (i) {
      if (Book.checkIsbn(i)) {
        this.isbn = i;
      } else {
        console.log(`${i} is not a valid ISBN!`);
      }
    };
  3. O

    Book.prototype.setIsbn = function (i) {
      const validationResult = this.checkIsbnAsId(i);
      if (validationResult instanceof NoConstraintViolation) {
        this.isbn = i;
      } else {
        throw validationResult;
      }
    };