JSE1: Module 5 Test Answers (JavaScript Essentials 1)

JavaScript Essentials 1 – JSE1: Module 5 Test Answers

1. Analyze the following code:

let show = function () {
    console.log(test);
}
setTimeout(show, 2000);

Its execution will cause:

  • the console to display "test" after a 2 second delay.
  • the console to display "test" after a 2000 second delay.
  • the console to display "test" 2000 times.
  • the console to display "show" after a 2 second delay.

2. Review the following code:

let x = 10;
let y = 20;

function test(y) {
    console.log(y);
}

test(x);

What will be displayed in the console as a result of its execution?

  • "y"
  • 20
  • 10
  • Nothing, because the function expects the y variable to be passed and receives x instead.

3. We define a function using the following function expression:

let sum = function (a, b) {
    return (a + b);
}

What could the definition of the corresponding arrow function look like?

  • let sum = function (a, b) => {
        return (a + b);
    }
    ;
  • let sum = (a, b) => a + b;
  • let sum = (a, b) > a + b;
  • let sum = (a, b) => {
        a + b;
    };

4. If the function is to return some calculated value on completion, we use the following keyword to do so:

  • yield;
  • ret;
  • function;
  • return;

5. Analyze the code below:

function test(counter) {
    console.log("test");
    if (counter > 0)
        test(--counter);
}

test(3);

How many times will the word "test" be displayed in the console?

  • 2
  • 4
  • 3
  • 0

6. A callback function is a function that:

  • contains a reference to itself in its code.
  • is always executed at fixed intervals.
  • is always called with a certain pre-defined delay.
  • is passed to another function as an argument and only called in its code.

7. Review the following code:

let x = 10;

function test(x) {
    console.log(x);
}

test(20);

What will be displayed in the console as a result of its execution?

  • Nothing will show up.
  • "x"
  • 10
  • 20

8. We can use the forEach method to pass through the elements of an array. Which of the following code snippets will display all consecutive elements of the animals array in the console?

  • animals.forEach(a => {
        console.log(a);
    })
  • forEach(animals, a => {
        console.log(a);
    })
  • animals.forEach(console.log(animal));
  • animals.forEach(a => a);

9. We have defined an arrow function:

let multiply = (m, n) => m * n;

We will try to write it in a slightly modified form, but without changing what it is supposed to do. Point out the correct definition:

  • let multiply = (m, n) => {
    	m * n;
    }
  • let multiply = (m, n) => {
        console.log(m * n);
    }
  • let multiply = (m, n) =>
    return (m * n);
  •  {
        return (m * n);
    }

10. The code snippet:

function test() {
}

is:

  • the declaration of the test variable in which the values returned by the completed function will be stored.
  • the call of the test function.
  • incorrect, as the code is wrong and does not mean anything.
  • the declaration of an empty test function.

11. Analyze the following code:

let x = 10;

function test() {
    let x = 20;
    console.log(x);
}

What will be displayed in the console as a result of its execution?

  • "x"
  • 20
  • 10
  • Nothing will show up.

Leave a Reply

Your email address will not be published. Required fields are marked *