JavaScript Essentials 2 : Module 2 Test Answer 2024

You are currently viewing JavaScript Essentials 2 : Module 2 Test Answer 2024
  1. A class may have a constructor, i.e. a method:
  • named constructor
  • of any name that will be defined first in the class.
  • of any name preceded by the word constructor
  • named this

2. You have defined a class Point whose constructor takes two arguments: x and y. Which of the following is the correct way to create a point object of this class?

  • let point = new Point(100, 200);
  • let point = create Point(100, 200);
  • let point = Point(100, 200);
  • Point(let point, 100, 200);

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

class User {
    // Insert line of code here.
    showName() {
        console.log(this.name);
    }
}
let user = new User('Alice');
user.showName();

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

  • constructor(n) { this.name = n;}
  • constructor(n) { name = n;}
  • constructor function(n) { name = n;}
  • User(n) { this.name = n;}

4. Analyze the following code:

let Point = class {};

It is:

  • correct, because it is a declaration of an anonymous class, and saves it to the variable Point (a class as a first-class citizen).
  • incorrect, because it should be: class Point {};
  • incorrect, because a class definition (the content of the brackets) can’t be empty.
  • correct, because it is the creation of an object based on an empty class, and saves it to the Point variable.

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

class A {
    // Insert line of code here.
}

Select the correct missing line in order to declare a property named test and initialize it with the value 10:

  • test = 10;
  • this.test = 10;
  • test: 10;
  • test: 10

6. Analyze the following code:

class User {
    constructor (x, y) {
        this.x = x;
        this.y = y;
    }

    setColor(color) {
        this.color = color;
    }
}
let point = new Point(100, 200);
point.setColor('red');

The point object:

  • has three properties: xy and color.
  • has two properties: x and y (only these are defined in the constructor)
  • will not be created – setColor references a color property that was not defined in the constructor.
  • will not be created – setColor references a color property that was not defined in the constructor.

7. Analyze the following code:

class Point {
    name = 'Point';
    constructor (x, y) {
        this.x = x;
        this.y = y;
    }

    setColor(color) {
        this.color = color;
    }
}
let point = new Point(100, 200);
point.setColor('red');

Which of the following properties will the point object have?

  • name x y color
  • name x y
  • x y
  • name

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

class User {
    // Insert line of code here.
    get name() {return this.#name;}
}
let point = new User();
point.x = 10;

Select the correct missing line in order to insert a private property declaration in the code:

  • #name = 'Bob';
  • name = 'Bob';
  • this.#name = 'Bob';
  • this.#name = 'Bob';

9. Analyze the following code:

class Point {
    #x = 0;
    #y = 0;
    color = 'red';
}
let point = new Point();
console.log(Object.keys(point));

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

  • ['color']
  • []
  • ['x', 'y', 'color']
  • ['#x', '#y', 'color']

10. Analyze the following code:

class User {};
let user = new User(); 
console.log(`${user instanceof User} ${typeof(user)}`);

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

  • true object
  • User User
  • User object
  • true User

11. Analyze the following code:

class User {
    constructor(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    get fullName()  {return `${this.firstName} ${this.lastName}`}
}
let user = new User('Bob Marley');

Select the correct command in order for the console to show the following string after running the whole code: Bob Marley

  • console.log(user.fullName);
  • console.log(user['get fullName']);
  • console.log(user['get fullName']);
  • console.log(user.fullName());

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

class User {
    _name = 'Alice';
    // Insert line of code here.
}
let user = new User();
user.name = 'Bob';
console.log(user._name);

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

  • set name(val) { this._name = val;}
  • _name set(name) { this._name = name;}
  • set _name(name) { this._name = name;}
  • set (name) { this._name = name;}

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

class A {
    test1() { return 'A';}
}

// Insert line of code here.
    test2() { return 'B';}

}
let b = new B();
console.log(`${b.test1()} ${b.test2()}`);

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

  • class B extends A {
  • class A extends B {
  • A inherits B {
  • B inherits A {

14. Analyze the following code:

class User {};
class EUser extends User {};
class EEUser extends EUser {};
let eeuser = new EEUser();
console.log(`${eeuser instanceof User} ${eeuser instanceof EUser} ${eeuser instanceof EEUser}`);

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

  • true true true
  • true false false
  • false false true
  • false false false

15. Analyze the following code:

class A {
    getName() {
        return 'A';
    }
}
class B extends A {
    getName() {
        return 'B';
    }
    test(x) {
        return x ? this.getName() : super.getName();
    }
}
 let b = new B();
console.log(`${b.test(true)} ${b.test(false)}`);

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

  • B A
  • A A
  • A B
  • B B

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

class A {
    constructor(val) {
        console.log(`A: ${val}`);
    }
}

class B extends A {
    constructor(val) {
        // Insert line of code here.
        console.log(`B: ${val}`);
    }
}
let b = new B(10);

Select the correct missing line so that the executed code results in the following console output:
A: 10
B: 10

  • super(val);
  • this = new A(val);
  • A(val);
  • An empty line

17. A static method defined in a class:

  • is bound to the class only and will not be available in the object created from it.
  • is visible both in the class and in the object created from it (it can be called in both).
  • is visible only in the object created from the class and can be called arbitrarily in that object.
  • is visible only in the object created from the class, but can only be called by other methods of the object.

18. You have declared the class Test and create a test object from it:

class Test {
    static info() { return'Test';}
}
test = new Test();

Select the correct call so that the executed code results in the following console output: Test

  • Test.info()
  • test.info()
  • test['static info']()
  • Test.info

19. Analyze the following code:

function A() {};
class B extends A {};
let b = new B();
console.log(`${b instanceof A} ${b instanceof B}`);

As a result of its execution:

  • true true will be displayed, because A will be treated as a constructor during inheritance.
  • false true will be displayed, because A is not a class.
  • false false will be displayed, because B inheriting from A will also be a function.
  • an error will appear because you are trying to inherit in class B from function A and not from another class.

Leave a Reply