JavaScript Essentials 2 : Module 1 Test Answer 2024

You are currently viewing JavaScript Essentials 2 : Module 1 Test Answer 2024

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', age: 30};
  • var user = {'Bob'};
  • let user = [name: 'Bob'];
  • let user = [name: 'Bob', age: 30];

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 null to the variable that holds it.
  • should be deleted by setting undefined to the variable that holds it.

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 = {model: 'Viper', 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.

4. You have declared a circle object:

let circle = {
    centre: {
       x: 10,
       y: 20
    },
    radius: 100
}

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);

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

let key = 'x'
let point = {
    x: 100,
    y: 200
}
// 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[x]);
  • console.log(point.key);
  • console.log(point:key);

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']
  • You cannot use a property name consisting of several words.
  • point.'geo position string'
  • point[geo position string]

7. You have declared a user object:

let user = {
    name: "Ian",
    age: 44
}

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(user[key]));
  • console.log(user[name, age]);
  • for(key in user) console.log(key);
  • Object.keys(user).forEach(key => console.log(key));

8. You have declared two user objects:

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

Which of the following comparisons will return true?

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

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 40
  • undefined 40
  • Ian 44
  • undefined 44

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

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 RAM red
  • Dodge RAM green
  • undefined RAM red
  • Dodge Viper green

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

let point = {
    x: 100,
    y: 100,
    // 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}`);}
  • show: function() {console.log(`${x} ${y}`);}
  • show: function(this) {console.log(`${this.x} ${this.y}`);}
  • point.show = {console.log(`${this.x} ${this.y}`);}

13. You have declared the following point object:

let point = {
    x: 100,
    y: 100,
    show: function() {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]()

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

let point = {
    x: 100,
    y: 200,
    // 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() {this.x = x;}
  • positionX: set() {this.x = x;}

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.preventExtensions(car);
  • Object.seal(car);
  • Object.freeze(car);
  • An empty line

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 = new Car('Dodge', 'Viper');
  • let car = Car({'Dodge', 'Viper'})
  • let car = Car('Dodge', 'Viper');
  • let car = Car({}, 'Dodge', 'Viper');

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};
  • return (make, model);
  • return {this.make, this.model}
  • ;this.make = make; this.model = model;

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;};

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

  • ColorPoint.prototype = point;
  • cpoint = point.prototype;
  • cpoint.prototype = point;
  • ColorPoint.prototype = Point;

Leave a Reply