4. Quiz Questions

4.1. Question 1: Data values and objects

Which of the following statements about data values and objects in JS are true? Select one or more:

true is an object. ☐ A JS array is a JS object. ☐ false is a data value.

☐ A JS function is a JS object. ☐ 1 is a data value. ☐ Infinity is an object.

4.2. Question 2: Evaluating a Boolean expression

What is the value of the Boolean expression null || !0 ? Select one:

O true O false

4.3. Question 3: JavaScript datatypes

Which of the following denote primitive datatypes in JavaScript? Select one or more:

☐ double ☐ string ☐ float ☐ int ☐ boolean ☐ byte ☐ number

4.4. Question 4: Constructor-based class definition

Which of the following JavaScript fragments correctly defines the constructor-based class City shown in the class diagram (either using a constructor function definition or an ES6 class definition)? Hint: notice that setName is an instance-level method while checkName is a class-level ("static") method. Select one or more:

  1. function City( n) {
      this.name = n;
      this.setName = function (n) {this.name = n;};
      checkName = function (n) {...}; // returns true or false
    }
  2. class City {
      constructor (n) {
        setName(n);
      }
      setName(n) {if (City.checkName( n)) this.name = n;}
      static checkName(n) {...}  // returns true or false
    }
  3. function City( n) {
      this.setName( n);
      function checkName( n) {...} // returns true or false
    } 
    City.prototype.setName = function (n) {this.name = n;};
  4. function City( n) {
      this.setName( n);
    }
    City.prototype.setName = function (n) {
      if (City.checkName( n)) this.name = n;
    };
    City.checkName = function (n) {...};  // returns true or false

4.5. Question 5: Type coercion

Consider the following JavaScript code:

var a = 5;
var b = "7";
var c = a + b;

What is the value of the variable c? Select one:

O The string "57" O The number 12 O undefined

O The code will result in an error since you can't use the + operator between two operands of different types.

4.6. Question 6: Variable scope

What is the output of the following program?

function foo() {
  var i=7;
  for (var i=0; i < 10; i++) {
    ...  // do something
  }
  console.log( i);
};
foo();

Answer: _____