JavaScript Essentials 2 – JSE2: Module 1: Classless Objects Module 1 Test Exam Answers Full 100%

Navigating the complexities of JavaScript can be daunting, but with the right resources, it becomes manageable and even enjoyable. JavaScript Essentials 2 (JSE2) is a comprehensive course that lays the groundwork for mastering this versatile programming language. In this post, we provide detailed answers for the Module 1 Test Exam on Classless Objects, ensuring you achieve a full 100%. These answers will not only help you excel in your assessments but also deepen your understanding of Classless Objects in JavaScript. Whether you’re a beginner or looking to refresh your skills, this guide is your key to success. Let’s explore the essential concepts and answers that will boost your JavaScript proficiency!

  1. You have declared the following statement:

    console.log(user.name)

    What declaration should precede the statement so that the text Bob appears in the console as a result of code execution?

    • let user = [name: 'Bob'];
    • let user = {name: 'Bob', age: 30};
    • let user = [name: 'Bob', age: 30];
    • var user = {'Bob'};
    • Explanation & Hint:

      let user = {name: ‘Bob’, age: 30};

      It creates an object user with a property name set to ‘Bob’, and when you log user.name, it will print ‘Bob’ to the console.

  2. An object that will no longer be used:

    • will automatically be deleted.
    • should be deleted using the delete command.
    • should be deleted by setting undefined to the variable that holds it.
    • should be deleted by setting null to the variable that holds it.
    • Explanation & Hint:

      In JavaScript, objects no longer referenced or accessible by any part of your code become eligible for automatic garbage collection. This means that at its discretion, the JavaScript engine will remove these objects from memory to free up resources. An object no longer used will automatically be deleted in JavaScript

  3. There is one line missing in the code below:

    let car = {
      make'Dodge',
      model:'Dakota'
    }
     // Insert line of code here.
    console.log(`${car.make} ${car.model}, color: ${car.color}`);

    Select the correct missing line in order for the console to show the following after running the whole code: Dodge Viper, color: red

    • car.model = 'Viper'; car.color = 'red';
    • car.set('model', 'Viper'); car.add('color', 'red');
    • An empty line – it is not possible to add a new color property to a previously created object.
    • car = {model: 'Viper', color: 'red'};
    • Explanation & Hint:

      To achieve the desired console output of “Dodge Viper, color: red” after running the whole code, you should use the following line:

      car.model = ‘Viper’; car.color = ‘red’;

      This line reassigns the car variable with a new object that has both the model and color properties set to the desired values.

  4. You have declared a circle object:

    let circle = {
        centre: {
           x10,
           y20
        },
        radius100
    }

    Which of the following will display the value of the circle’s x-coordinate (i.e. 10) in the console?

    • console.log(circle.centre.x);
    • console.log(circle.centre);
    • console.log(centre.x);
    • console.log(circle.x);
    • Explanation & Hint:

      The correct option to display the value of the circle’s x-coordinate (i.e., 10) in the console is:

      console.log(circle.centre.x);

      It accesses the center property of the circle object and then the x property of the center object to display the x-coordinate

  5. There is one line missing in the code below:

    let key = 'x'
    let point = {
        x100,
        y200
    }
    // Insert line of code here.

    Select the correct missing line so that the executed code results in the following console output: 100

    • console.log(point:key);
    • console.log(point[key]);
    • console.log(point.key);
    • console.log(point[x]);
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “100” is:

      console.log(point[key]);

      This code correctly accesses the value associated with the x property in the point object using the key variable as the property name.

  6. You have declared a point object that has one property: geo position string

    let point = {
        'geo position string': '67.88183984530318, 12.97985704867863'
    }

    Which of the following is the correct way to refer to this property?

    • point['geo position string']
    • point.'geo position string'
    • point[geo position string]
    • You cannot use a property name consisting of several words.
    • Explanation & Hint:

      The correct way to refer to the property ‘geo position string’ in the point object is:

      point[‘geo position string’]

      This is the correct way to access a property with a name that consists of spaces or special characters. Wrapping the property name in square brackets and using single or double quotes (or even without quotes if it doesn’t contain spaces or special characters) is the standard way to access such properties.

  7. You have declared a user object:

    let user = {
        name"Ian",
        age44
    }

    Which line of code will display the values of all object properties? The console should show Ian and 44

    • Object.keys(user).forEach(key => console.log(key));
    • for(key in user) console.log(key);
    • console.log(user[name, age]);
    • Object.keys(user).forEach(key => console.log(user[key]));
    • Explanation & Hint:

      The correct line of code to display the values of all object properties (i.e., “Ian” and 44) is:

      Object.keys(user).forEach(key => console.log(user[key]));

      It uses Object.keys() to get an array of the object’s keys (in this case, ‘name’ and ‘age’) and then iterates over them to access the corresponding values and print them to the console.

  8. You have declared two user objects:

    let user1 = {
        name'Alice'
    }
    let user2 = {
        name'Alice'
    }

    Which of the following comparisons will return true?

    • user1 === user2
    • user1 == user2
    • user1 same user2
    • (user1.name == user2.name) && (user1.name == user2.name)
    • Explanation & Hint:

      The correct comparison that will return true for these two objects is:

      (user1.name == user2.name) && (user1.name == user2.name)

      This comparison checks if the name property of user1 is equal to the name property of user2, and then checks the same condition again. It will return true because both objects have the same name property value, which is ‘Alice’.

  9. Analyze the following code:

    let user1 = {
        name: 'Ian',
        age: 44
    }
    let user2 = user1;
    user2.age = 40;
    console.log(`${user2.name} ${user1.age}`);

    What will appear in the console as a result of code execution?

    • Ian 44
    • Ian 40
    • undefined 44
    • undefined 40
    • Explanation & Hint:

      The code provided will result in the following output in the console:

      Ian 40

      The code first creates an object user1 with two properties, name, and age, and initializes them with the values ‘Ian’ and 44, respectively. Then the next line does not create a new object. Instead, it makes user2 reference the same object as user1. Both user1 and user2 now point to the same object in memory. Since user2 and user1 reference the same object, changing the age property of user2 also affects user1. So, the age property of the shared object is updated to 40. Finally, the last line logs the name property of user2 (‘Ian’) and the age property of user1 (which has been changed to 40 due to the shared reference). Hence, the output is “Ian 40.”

  10. Analyze the following code:

    let car1 = {
        make: 'Dodge',
        model: 'Viper'
    }
    let car2 = Object.assign({}, car1, {model:'RAM', transmission: 'automatic'}, {color: 'red'});
    car1.color = 'green';
    console.log(`${car2.make} ${car2.model} ${car2.transmission} ${car2.color}`);

    What will appear in the console as a result of code execution?

    • Dodge RAM automatic red
    • Dodge RAM automatic
    • Dodge RAM automatic green
    • Dodge Viper undefined green
    • Explanation & Hint:

      The result will be:

      Dodge RAM automatic red

      First, the code creates an object car1 with two properties, make and model, initialized with the values ‘Dodge’ and ‘Viper’, respectively. Then, it will create a new object car2. It uses Object.assign() to copy properties from car1 into car2. It also assigns a new value (‘RAM’) to the model property, adds a transmission property with the value ‘automatic’, and adds a color property with the value ‘red’ to car2. car1.color = ‘green’ line updates the color property of car1 to ‘green’. This change does not affect car2 because they are separate objects. console.log line logs the properties of car2. It displays the make and model properties from car2 (‘Dodge’ and ‘RAM’ after the assignment), the transmission property (‘automatic’ after the assignment), and the color property (‘red’ as assigned during object creation). So, the output is “Dodge RAM automatic red.”

  11. Analyze the following code:

    let car1 = {
        make'Dodge',
        model'Viper'
    }
    let car2 = { ...car1, model:'RAM', color: 'red'};
    car1.color = 'green';
    console.log(`${car2.make} ${car2.model} ${car2.color}`);

    What will appear in the console as a result of code execution?

    • Dodge Viper green
    • undefined RAM red
    • Dodge RAM red
    • Dodge RAM green
    • Explanation & Hint:

      The code will result in the following output in the console:

      Dodge RAM red

      The first line creates an object car1 with two properties, make and model, initialized with the values ‘Dodge’ and ‘Viper’, respectively. Then create a new object car2 using the spread syntax (…). It copies all properties from car1 into car2, assigns a new value (‘RAM’) to the model property, and adds a color property with the value ‘red’ to car2. It updates the color property of car1 to ‘green’. This change does not affect car2 because they are separate objects. Finally, the last line logs the properties of car2. It displays the make and model properties from car2 (‘Dodge’ and ‘RAM’ after the assignment), and the color property (‘red’ as assigned during object creation). So, the output is “Dodge RAM red.”

  12. There is one line missing in the code below:

    let point = {
        x100,
        y100,
        // Insert line of code here.
    }
    point.show();

    Select the correct missing line in order for the console to show the following after running the whole code: 100 100

    • show: function() {console.log(`${this.x} ${this.y}`);}
    • point.show = {console.log(`${this.x} ${this.y}`);}
    • show: function(this) {console.log(`${this.x} ${this.y}`);}
    • show: function() {console.log(`${x} ${y}`);}
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “100 100” after running the whole code is:

      show: function() {console.log(`${this.x} ${this.y}`);}

      It defines a show method within the point object that logs the x and y properties of the object, resulting in the desired console output.

  13. You have declared the following point object:

    let point = {
        x100,
        y100,
        showfunction() {console.log(`${x}:${y}`)}
    }

    The default way to call the show method is: point.show(). Select the correct alternative to call this method:

    • point[show]()
    • point.show
    • point[show]
    • point['show']();
    • Explanation & Hint:

      The correct alternative to call the show method of the point object is:

      point[‘show’]();

      This is an acceptable way to call the method using bracket notation, but it’s not the default way. The dot notation point.show() is more commonly used.

  14. There is one line missing in the code below:

    let point = {
        x100,
        y200,
        // Insert line of code here.
    }
    point.positionX = 0;
    console.log(point.x)

    Select the correct missing line in order for the console to show the following after running the whole code: 0

    • set positionX(x) {this.x = x;}
    • positionX() {this.x = x;}
    • positionX: set() {this.x = x;}
    • positionX: this.x = x
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “0” after running the whole code is:

      set positionX(x) {this.x = x;}

      It defines a setter method positionX that sets the x property of the point object, allowing you to change the x value by assigning a new value to point.positionX.

  15. There is one line missing in the code below:

    let car = {
        make'Dodge',
        model'Viper'
    }
    // Insert line of code here.
    delete car.model;
    car.make = 'Toyota';
    car.color = 'red';
    console.log(`${car.make} ${car.model} ${car.color}`);

    Select the correct missing line in order for the console to show the following after running the whole code: Toyota undefined undefined

    • Object.freeze(car);
    • An empty line
    • Object.seal(car);
    • Object.preventExtensions(car);
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “Toyota undefined undefined” after running the whole code is:

      Object.preventExtensions(car);

      It is used to prevent new properties from being added to an object (car).

  16. There is one line missing in the code below:

    let Car = function(make, model) {
        this.make = make;
        this.model = model;
    }
    // Insert line of code here.
    console.log(`${car.make} ${car.model}`);

    Select the correct missing line in order for the console to show the following after running the whole code: Dodge Viper

    • let car = Car({'Dodge', 'Viper'})
    • let car = new Car('Dodge', 'Viper');
    • let car = Car({}, 'Dodge', 'Viper');
    • let car = Car('Dodge', 'Viper');
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “Dodge Viper” after running the whole code is:

      let car = new Car(‘Dodge’, ‘Viper’);

      It correctly creates a new instance of the Car constructor function, passing ‘Dodge’ as the make parameter and ‘Viper’ as the model parameter, resulting in the desired output.

  17. There is one line missing in the code below:

    let getCar = function(make, model) {
        // Insert line of code here.
    }
    let car = getCar('Dodge', 'Viper');
    console.log(`${car.make} ${car.model}`);

    Select the correct missing line in order for the console to show the following after running the whole code: Dodge Viper

    • return (make, model);
    • this.make = make; this.model = model;
    • return {this.make, this.model};
    • return {make, model};
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “Dodge Viper” after running the whole code is:

      return {make, model};

      It creates an object with properties make and model and returns it. This object is assigned to the car variable when you call getCar(‘Dodge’, ‘Viper’).

  18. There is one line missing in the code below:

    // Insert line of code here.
    let car = getCar('Dodge', 'Viper');
    console.log(`${car.make} ${car.model}`);

    Select the correct missing line in order for the console to show the following after running the whole code: Dodge Viper

    • let getCar = (make, model) => {make, model};
    • let getCar = (make, model) => ({make, model});
    • let getCar = (make, model) => (make, model);
    • let getCar = (make, model) => {this.make = make; this.model = model;};
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “Dodge Viper” after running the whole code is:

      let getCar = (make, model) => ({make, model});

      It defines an arrow function that takes make and model as parameters and returns an object with properties make and model. This is the desired behavior to create the car object.

  19. There is one line missing in the code below:

    let Point = function(x, y) {
        this.x = x;
        this.y = y;
    }
    let point = new Point(0, 0);
    let ColorPoint = function(color) {
        this.color = color;
    }
     // Insert line of code here.
    let cpoint = new ColorPoint('red');
    console.log(cpoint.x);

    Select the correct missing line in order for the console to show the following after running the whole code: 0

    • cpoint.prototype = point;
    • ColorPoint.prototype = point;
    • ColorPoint.prototype = Point;
    • cpoint = point.prototype;
    • Explanation & Hint:

      The correct missing line of code to achieve the desired console output “0” after running the whole code is:

      ColorPoint.prototype = point;

      With this change, ColorPoint inherits properties from point, and cpoint.x will correctly output 0.

Leave a Reply