Javascript interview questions

Javascript quiz questions

  • 1.

    What does the following code print to the console?

    function Hat() {
      var brand = "shhh can't tell";
      this.getBrand = function () { return brand; };
    }
    var my_hat = new Hat();
    console.log(my_hat.getBrand());

    Answer:

    "shhh can't tell"
    

    The brand variable cannot be directly accessed outside of the Hat() function. However, the brand variable can be accessed anywhere within the Hat() constructor function. The getBrand() method can access the brand variable and make it indirectly accessible.

    View
  • 2.

    What does the following code print to the console?

    function Hat() {
      var brand = "shhh can't tell";
    }
    var my_hat = new Hat();
    console.log(my_hat.brand);

    Answer:

    undefined
    

    brand is a private variable in the Hat() constructor function, so it can only be accessed by functions within the constructor function. Remember that JavaScript functions have function scope meaning that variables defined in the function are only available within the function itself.

    View
  • 3.

    Use object literal notation to create an object that behaves similarly to the wilson object that is created with the following Person() constructor:

    function Person(first_name, last_name) {
      this.first_name = first_name;
      this.last_name = last_name;
      this.full_name = function () { return this.first_name + " " + this.last_name };
    }
    
    wilson = new Person("wilson", "rawls");

    Answer:

    var wilson = {
      first_name: "wilson",
      last_name: "rawls",
      full_name: function () { return this.first_name + " " + this.last_name }
    }
    

    Notice that it would be much easier to make 100 person objects using constructor functions compared to object literal notation. Object literal notation is great if only one object of a specific type is being made, but constructor functions are superior if many objects of a given types will be made.

    View
  • 4.

    Create an object with the Person() constructor and then call the full_name() method.

    function Person(first_name, last_name) {
      this.first_name = first_name;
      this.last_name = last_name;
      this.full_name = function () {
        return this.first_name + " " + this.last_name
      };
    }

    Answer:

    var crazy_pants = new Person("crazy", "pants");
    console.log(crazy_pants.full_name());
    

    The arguments that are passed to the Person() constructor function are used to create properties of the object.

    Notice that the "this" keyword refers to the object that is being created in constructor functions. In the full_name() method of the constructor function, this.first_name and this.last_name refer to properties of the person object that is being created by the constructor function.

    View
  • 5.

    Use object literal notation to create an object that has the same behavior as the objects created by the following constructor function.

    function Calculator() {
      this.square = function (x) {
        return x * x;
      }
    }

    Answer:

    var my_calculator = {
      square: function (x) {
        return x * x;
      }
    }
    

    Constructor functions are just specialized functions that can be used to create the same objects that are created with object literal notation.

    View
  • 6.

    Call the square() method on the my_calculator object.

    function Calculator() {
      this.square = function (x) {
        return x * x;
      }
    }
    var my_calculator = new Calculator();

    Answer:

    console.log(my_calculator.square(5));
    

    Constructor functions can create objects with methods. All objects created by the Calculator constructor in this example have a square() method.

    View
  • 7.

    Use object literal notation to create an object assigned to the spot variable, similar to the following constructor function.

    function Mammal() {
      this.warm_blooded = true;
      this.scaly = false;
    }
    var spot = new Mammal();

    Answer:

    var spot = {
      warm_blooded: true,
      scaly: false  
    }
    

    Constructor functions and object literal notation can both be used to create JavaScript objects. Constructor functions are better when multiple similar objects are created, but both object construction techniques can be used to make JavaScript objects.

    View
  • 8.

    The following code illustrates how to define a constructor function and use the constructor function to create an object.

    function Mammal() {
      this.warm_blooded = true;
      this.scaly = false;
    }
    var fido = new Mammal();
    

    Print the scaly property of the fido object to the console.

    Answer:

    console.log(fido.scaly);
    

    Constructor functions are specialized JavaScript functions for creating objects. Notice that the fido object could also be created with object literal notation.

    View
  • 9.

    Create a sally object with first_name, last_name, and full_name properties with object literal notation, similar to the following example.

    var bob = {
      first_name: "Bob",
      last_name: "Lob",
      full_name: function () {
        return this.first_name + " " + this.last_name
      }
    }

    Answer:

    var sally = {
      first_name: "Sally",
      last_name: "Mae",
      full_name: function () {
        return this.first_name + " " + this.last_name
      }
    }
    

    The sally_object is similar to the bob object and it was tedious retyping all this code to create a separate object with the same behavior. In this quiz, constructor functions are introduced to facilitate the creation of similar objects.

    View
  • 10.

    What does the following code print to the console?

    function a () {};
    console.log(a.length);

    Answer:

    0
    

    Functions that don't have any named parameters have a length of 0.

    View
  • 11.

    Define a goodSortArgs() function that takes a variable number of integer arguments an returns a sorted array. goodSortArgs(1, 44, 3) should return [1, 3, 44].

    Answer:

    function goodSortArgs () {
      var args = Array.prototype.slice.call(arguments, 0);
      var result = args.sort(function (x, y) {
        return x - y;
      });
      return result;
    }
    View
  • 12.

    Why doesn't the following code work?

    function badSortArgs () {
      var result = arguments.sort(function (x, y) {
        return x - y;
      });
      return result;
    }
    badSortArgs(1, 44, 3);

    Answer:

    arguments is an array-like object, but it's not actually an array. The sort() method is not defined for the arguments object.

    View
  • 13.

    Write a function called mergeObjects() that takes a variable number of objects as parameters and returns a single object with all the properties from the individual objects. mergeObjects({some: "thing"}, {cool: "jerk", phat: "cow"}, {holy: "moly"}) should return {some: "thing", cool: "jerk", phat: "cow", holy: "moly"}.

    Answer:

    function mergeObjects () {
      var result = arguments[0];
      for (var i = 1, l = arguments.length; i < l; i ++) {
        var arg = arguments[i];
        for (var key in arg) {
          result[key] = arg[key];
        }
      }
      return result;
    }
    View
  • 14.

    Write a function called biggestArg() that takes a variable number of integer arguments and returns the largest integer. biggestArg(1, 3, 33, 4) should return 33.

    Answer:

    function biggestArg () {
      return Math.max.apply(Math, arguments)
    }
    

    We don't need to use Math as the first argument for the apply() function, but it makes the code more readable. This code would also work:

    Math.max.apply("", arguments)
    View
  • 15.

    Describe what happens when the Helpers.factorial(3) function is run twice.

    var Helpers = {
      cache: {},
      factorial: function (n) {
        if (this.cache[n]) { return this.cache[n]; };
        var counter = 1;
        var result = 1;
        while (counter <= n) {
          result *= counter;
          counter++;
        }
        this.cache[n] = result;
        return result;
      }
    }
    
    Helpers.factorial(3)
    Helpers.factorial(3)

    Answer:

    The factorial is cached in an object, so it does not need to be recomputed if it has already been calculated. When Helpers.factorial(3) is initially run, the factorial is computed and cached. When Helpers.factorial(3) is run again, the cached value is returned without performing the computations again.

    Caching requires extra memory and makes the code more complex, but it is a very useful tactic for expensive calculations. Caching is also referred to as memoization.

    View
  • 16.

    What does the following code print to the console?

    function dinero () {};
    dinero.meaning = "money";
    console.log(dinero.meaning);

    Answer:

    Functions are objects and can be assigned properties, just like any other object.

    View
  • 17.

    What does the following code print to the console?

    var happy = {
      hi: function sing(n, result) {
        result = typeof result !== 'undefined' ? result : [];
        if (n == 0) {
          result.push("No more bottles");
          return result;
        }
        var str = n + " bottles";
        result.push(str);
        return sing(n - 1, result);
      }
    }
    
    console.log(happy.hi(3));

    Answer:

    ["3 bottles", "2 bottles", "1 bottles", "No more bottles"]
    

    The happy object has a hi property that points to the named function sing(). Object values are typically anonymous functions, but they can also be named functions. A named function works well in this example to allow for recursion.

    View
  • 18.

    Write a recursive factorial function and then verify that the factorial of 4 equals 24.

    Answer:

    function factorial(n) {
      if (n == 0) { return  1; }
      return n * factorial(n - 1);
    }
    console.log(factorial(4));
    View
  • 19.

    What does the following code print to the console?

    var boo = function () {};
    console.log(boo.name);

    Answer:

    ""
    

    Anonymous functions also have a name property, but it returns the empty string.

    View
  • 20.

    What does the following code print to the console?

    function dog () {};
    console.log(dog.name);

    Answer:

    "dog"
    

    Functions have a name property that corresponds with the name of the function.

    View

© 2017 QuizBucket.org