JavaScript Essentials 1 | JSE1 – Final Test Exam Answers Full Score 100%

  1. Entering about:blank in the address bar of your browser will:

    • open a tab with information about your browser.
    • clear all inputs on the current page.
    • generate a page with information about the browser’s status.
    • generate and load a minimal blank HTML page into the current tab.
      Explanation & Hint:

      Entering “about:blank” in the address bar of your browser will load a blank page. The “about:blank” URL is a special URL that represents an empty or blank page. When you navigate to “about:blank”, the browser will display a blank page with no content.

  2. Analyze the following code:

    let id = "100";
    {
    let id = 200;
    id = id + 1;
    console.log(id);
    }

    What will appear to the console as a result?

    • 1001
    • 101
    • 201
    • 200
      Explanation & Hint:

      Let’s analyze the code step by step:

      1. The variable id is declared and initialized with the string value “100” using the let keyword in the global scope.
      2. A block of code is defined using curly braces {}. This block creates a new block scope.
      3. Inside the block, a new variable id is declared and initialized with the value 200 using the let keyword. This creates a new block-scoped variable id that shadows the outer variable id.
      4. The value of the block-scoped id is updated by adding 1 to it using the id = id + 1 statement.
      5. The resulting value of id, which is 201, is printed to the console using console.log(id).

      Since the console.log(id) statement is inside the block, it refers to the block-scoped variable id, which has a value of 201 after adding 1 to 200. Therefore, when the code is executed, 201 will appear in the console.

  3. Select a set of data types, containing only complex types:

    • Object, String
    • Boolean, Number, Bigint
    • Array, Object
    • Array, Object, String
      Explanation & Hint:

      In JavaScript, complex data types, also known as reference types, include objects, arrays, and functions. Here is a set of complex data types:

      1. Object: An object is an unordered collection of key-value pairs, where each value can be of any data type, including other complex types. Objects are created using curly braces {} and can be used to represent more complex data structures.
      2. Array: An array is an ordered list of values. It can hold elements of any data type, including other complex types. Arrays are created using square brackets [] and provide methods for manipulating and accessing their elements.
      3. Function: A function is a reusable block of code that can be executed when called. Functions are created using the function keyword or by using arrow function syntax (=>). They can take parameters, perform actions, and return values.
  4. We can replace the declaration let x = 3e-3; with:

    • let x = 3000;
    • let x = 0.003;
    • let x = 0.0003;
    • let x = 0.333;
      Explanation & Hint:

      The correct replacement for the declaration let x = 3e-3; would be: let x = 0.003;

  5. Using the string interpolation technique, we can create the string:

    "I do not like travelling by plane"

    and store it in the msg variable using the command:

    • let means = "plane";
      let msg = 'I do not like travelling by \{ means \}';
    • let means = "plane";
      let msg = "I do not like travelling by ${ means }";
    • let means = "plane";
      let msg = `I do not like travelling by ${means}`;
    • let means = "plane";
      let msg = `I do not like travelling by {means}`;
    • Explanation & Hint:

      To create the string “I do not like travelling by plane” using string interpolation and store it in the msg variable, the correct command would be:
      let means = “plane”;
      let msg = `I do not like travelling by ${means}`;
      String interpolation in JavaScript is achieved by using backticks (“`) as the string delimiter. Within the backticks, you can include expressions or variables by wrapping them in ${}. The expression or variable within ${} will be evaluated and its value will be inserted into the resulting string.
      In this case, the variable means is assigned the value “plane”. The string interpolation syntax ${means} is used within backticks to insert the value of means into the string. Thus, the resulting value of msg will be “I do not like travelling by plane”.

  6. We declare a movie object, with two fields: title and year:

    let movie = {
    title: "Life",
    year1999

    To change the value of the title field to "Matrix" we need to perform:

    • movie[title] = "Matrix";
    • movie.title = "Matrix";
    • movie{title} = "Matrix";
    • title->movie = "Matrix";
      Explanation & Hint:

      In JavaScript, you can access and modify object properties using dot notation (object.property). In this case, movie.title accesses the title property of the movie object, and the assignment = is used to change its value to “Matrix”. After executing the command, the title field in the movie object will be updated to “Matrix”.
      movie.title = “Matrix”;

  7. You declare the following array of animals: let animals = ["canary", "dog", "cat"];. Then, you call the method animals.push("hamster");. What will the animals array look like after calling the method?

    • ["canary", "dog", "cat", "hamster"]
    • ["hamster"]
    • ["hamster", "canary", "dog", "cat"]
    • ["canary", "dog", "cat"]
    • Explanation & Hint:

      After calling the animals.push(“hamster”); method, the animals array will look like this:
      [“canary”, “dog”, “cat”, “hamster”]
      The push() method in JavaScript is used to add one or more elements to the end of an array. In this case, the method animals.push(“hamster”) adds the string value “hamster” to the end of the animals array. As a result, the array is modified, and “hamster” becomes the last element in the array.

  8. A JavaScript code includes the console.log("http://somethingNew.org"); command. Its execution will:

    • display on the console information about the progress of the http://somethingNew.org page loading.
    • cause the page http://somethingNew.org to be loaded into the browser.
    • send a log with information about the currently executed script to the indicated address http://somethingNew.org.
    • display the following message on the console: "http://somethingNew.org".
    • Explanation & Hint:

      When the JavaScript code console.log(“http://somethingNew.org”); is executed, it will print the string “http://somethingNew.org” to the console.
      The console.log() function in JavaScript is used to output messages to the console. In this case, the code includes console.log(“http://somethingNew.org”), which specifies the string “http://somethingNew.org” as the argument to be logged.
      When the code is executed, the specified string will be displayed in the console. It does not perform any other action apart from printing the message to the console output.

  9. Analyze the code snippet:

    let winter = ["December""January""February"]; let index = winter.indexOf("February");

    What value will the index variable have?

    • "February"
    • 2
    • 3
    • 1
    • Explanation & Hint:

      The code snippet let winter = [“December”, “January”, “February”]; let index = winter.indexOf(“February”); will assign the value 2 to the index variable.

      Here’s a breakdown of the code:

      1. An array winter is declared and initialized with three elements: “December”“January”, and “February”.
      2. The indexOf() method is called on the winter array with the argument “February”. This method searches the array for the specified element and returns the index of the first occurrence of that element. If the element is not found, it returns -1.
      3. Since “February” is present in the winter array, the indexOf() method will find it and return the index 2, indicating that “February” is located at the third position in the array.
      4. The value 2 is assigned to the index variable using the let index = statement.

      Therefore, after executing the code, the index variable will have the value 2.

  10. In the daysOfWeek variable, you place an array with the names of the days of the week. Which method will you call to reverse the order of the array elements.

    • daysOfWeek.invert();
    • daysOfWeek = reverse(daysOfWeek);
    • daysOfWeek.reverse();
    • daysOfWeek.order(-1);
    • Explanation & Hint:

      To reverse the order of the elements in the daysOfWeek array, you can call the reverse() method.

  11. Review the following code:

    let msg1 = 'hello';
    let msg2 = msg1.slice(-1);
    console.log(msg2 ? msg2 : msg2 + msg1);

    What will appear on the console as a result?

    • o
    • ohello
    • hello
    • h
    • Explanation & Hint:

      The code snippet will display the letter “o” on the console as a result.

      Let’s break down the code:

      1. The variable msg1 is declared and assigned the string value ‘hello’.
      2. The variable msg2 is assigned the result of msg1.slice(-1). The slice() method is used to extract a portion of a string. In this case, -1 as the parameter means to extract the last character of msg1, which is ‘o’.
      3. The console.log() statement is called, which outputs a value to the console.
      4. The expression msg2 ? msg2 : msg2 + msg1 is evaluated and passed as the argument to console.log().
      5. In this expression, msg2 is truthy since it contains the letter ‘o’. Therefore, the result of the expression will be the value of msg2, which is ‘o’.
      6. As a result, the letter ‘o’ will be displayed on the console.

       

      So, when the code is executed, the console will display the letter ‘o’.

  12. Analyze the following code:

    let test = prompt("Run""code");

    What value will the test variable have if, after running the code, we immediately press the OK button on the newly created dialog?

    • true
    • OK
    • code
    • Run
    • Explanation & Hint:

      The value of the test variable will depend on the user’s input when the prompt dialog appears. If the user clicks the OK button, the test variable will be assigned the string (“code”). Since it is defined in the code. Therefore, the test variable will have the value “code”.

  13. Analyze the following code:

    let x = false || true;
    let y = "true" && "false";
    let z = false && true;
    console.log(`${x} ${y} ${z}`);

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

    • false true true
    • false false false
    • false false true
    • true false false
    • Explanation & Hint:

      Let’s break down the code:

      • The variable x is assigned the value of the logical OR operation between false and true. In a logical OR operation, if any of the operands is true, the result will be true. In this case, since the second operand (true) is true, the result of false || true is true, which is assigned to x.
      • The variable y is assigned the value of the logical AND operation between the string “true” and the string “false”. In a logical AND operation, if all operands are true, the result will be the last operand. In this case, since both “true” and “false” are considered truthy values, the result of “true” && “false” is “false”, which is assigned to y.
      • The variable z is assigned the value of the logical AND operation between false and true. In a logical AND operation, if any of the operands is false, the result will be false. In this case, since the first operand (false) is false, the result of false && true is false, which is assigned to z.
      • The console.log() statement is called to output a value to the console.
      • The expression ${x} ${y} ${z} is evaluated and passed as the argument to console.log(). It uses string interpolation to concatenate the values of xy, and z into a single string with spaces in between.
      • As a result, the console will display true false false.
  14. Analyze the following code:

    let a = true && 20;
    let b = 0 || 20
    let c = 0 && 20;
    console.log(`${a} ${b} ${c}`);

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

    • true 20 0
    • 1 1 0
    • true true false
    • 20 20 0
    • Explanation & Hint:

      Let’s break down the code:

      1. The variable a is assigned the value of the logical AND operation between true and 20. In a logical AND operation, if all operands are true, the result will be the last operand. In this case, both true and 20 are considered truthy values, so the result of true && 20 is 20, which is assigned to a.
      2. The variable b is assigned the value of the logical OR operation between 0 and 20. In a logical OR operation, if any of the operands is true, the result will be the first truthy operand encountered. In this case, 0 is considered a falsy value, so the result of 0 || 20 is 20, which is assigned to b.
      3. The variable c is assigned the value of the logical AND operation between 0 and 20. In a logical AND operation, if any of the operands is false, the result will be false. In this case, 0 is considered a falsy value, so the result of 0 && 20 is 0, which is assigned to c.
      4. The console.log() statement is called to output a value to the console.
      5. The expression ${a} ${b} ${c} is evaluated and passed as the argument to console.log(). It uses string interpolation to concatenate the values of ab, and c into a single string with spaces in between.
      6. As a result, the console will display 20 20 0.
  15. Examine the following code:

    let a = 20 + "10";
    let b = 20 + +"10";
    let c = 20 + -"10" + "10";
    let d = "10" - "10" + "100";
    let e = "A" - "B" + 0xA;
    console.log(`${a}, ${b}, ${c}, ${d}, ${e}`);

    What will appear on the console as a result?

    • 30, 30, 20, 100, 2
    • 30, 31, 39, 100, NaN
    • 2010, 2010, 20-1010, 0100, NaN
    • 2010, 30, 1010, 0100, NaN
    • Explanation & Hint:

      Let’s break down the code:

      1. The variable a is assigned the result of the concatenation between the string “20” and the string “10”. When the + operator is used with a string, it performs string concatenation. So, “20” + “10” results in the string “2010”, which is assigned to a.
      2. The variable b is assigned the result of the addition between the number 20 and the numeric conversion of the string “10”. The unary + operator before “10” converts it into a number, resulting in 20 + 10, which is 30, assigned to b.
      3. The variable c is assigned the result of the addition between the number 20 and the negated numeric conversion of the string “10”, followed by the concatenation with the string “10”. The unary – operator before “10” converts it into a number and negates it, resulting in -10. Then, 20 + (-10) is 10, which is concatenated with the string “10”, resulting in “1010”, assigned
        to c.
      4. The variable d is assigned the result of the subtraction between the numeric conversions of the strings “10” and “10”, followed by the concatenation with the string “100”. Both “10” and “10” are converted into numbers, resulting in 10 – 10, which is 0. Then, “0” + “100” is “0100”, assigned to d.
      5. The variable e is assigned the result of the subtraction between the numeric conversions of the strings “A” and “B”, followed by the addition of 0xA (which is 10 in hexadecimal). Both “A” and “B” cannot be converted into valid numbers, so the result is NaN. Then, NaN + 10 is still NaN, assigned to e.
      6. The console.log() statement is called to output the values to the console.
      7. The expression ${a}, ${b}, ${c}, ${d}, ${e} is evaluated and passed as the argument to console.log(). It uses string interpolation to concatenate the values of abcd, and e into a single string separated by commas.
      8. As a result, the console will display “2010, 30, 1010, 0100, NaN”.
  16. Which of the following loop instructions checks the loop continuation condition only after the iteration has been completed?

    • for ... in
    • while
    • for
    • do ... while
    • Explanation & Hint:

      The “do … while” loop instruction checks the loop continuation condition only after the iteration has been completed. In the “do … while” loop, the code block is executed first, and then the condition is checked. If the condition evaluates to true, the loop continues executing. This guarantees that the code block is executed at least once, regardless of the condition.

  17. The following function using a function expression has been defined:

    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;
    • Explanation & Hint:

      In arrow function syntax, the parameters are specified within parentheses (a, b), followed by the arrow =>, and then the expression
      a + b representing the function body. If the function body is a single expression, you can omit the curly braces {} and the return keyword.

  18. The temp array contains air temperature data measured over a period of time. You want to display the minimal temperature using the following code:

    temp.forEach(e => min = min > e ? e : min);
    console.log(min);

    What should the declaration of the min variable look like?

    • let min;
    • It’s not necessary, as it will be declared automatically on first use.
    • let min = temp[0];
    • let min = 0;
    • Explanation & Hint:

      The code snippet temp.forEach(e => min = min > e ? e : min); uses the forEach method to iterate over the elements of the temp array and update the value of min based on each element. In order to store the minimum temperature, you need to declare the min variable like this let min = 0; before using it in the code.

  19. Examine the following code:

    let x = [10, 20, 30, 40];
    let y = [50, 60];
    x.reverse().push(y);
    console.log(x.length);

    What will appear on the console as a result?

    • 6
    • 4
    • 5
    • 2
    • Explanation & Hint:

      The code will output 5 in the console.

  20. Analyze the following code:

    for(let a=1; a<10; a+=2) {
        console.log(a);
    };

    Which statement can replace the for from the example?

    • let counter = 1;
      while (counter++ < 10) console.log(counter++);
    • let counter = 0;
      while (counter < 9) console.log(counter++);
    • let counter = 0;
      while (counter++ < 10) console.log(counter++);
    • let counter = 0;
      while (counter < 10) console.log(counter++);
    • Explanation & Hint:

      The correct statement that can replace the for loop from the example is:
      let counter = 0;
      while (counter < 10) console.log(counter++);

  21. Analyze the following code:

    let colors = ['red', 'green', 'blue'];
    for (let of colors) console.log(c);

    What will appear on the console as a result?

    • ['red', 'green', 'blue']
    • red
      green
      blue
    • 0
      1
      2
    • 3
    • Explanation & Hint:

      The code is using a for…of loop to iterate over the elements of the colors array. In each iteration, the loop assigns the current element to the variable c, and then console.log(c) prints the value of c to the console. Since the colors array contains three elements (‘red’‘green’, and ‘blue’), the loop will iterate three times, printing each element on a separate line in the console.

  22. Analyze the following code:

    let route = {distance: 131, elevation: 1.4};
    for (let in route) console.log(k);

    What will appear on the console as a result?

    • 131
      1.4
    • distance
    • distance
      elevation
    • 2
    • Explanation & Hint:

      The code is using a for…in loop to iterate over the properties of the route object. In each iteration, the loop assigns the current property name to the variable k, and then console.log(k) prints the value of k to the console. The route object has two properties: distance and elevation. Therefore, the loop will iterate twice, printing each property name on a separate line in the console.

  23. Examine the following code:

    let a = (n) => {
        return n > 2 ? n * a(n - 1) : 2
    }
    console.log(a(6));

    What will appear on the console as a result?

    • 120
    • 720
    • 6
    • 4
    • Explanation & Hint:

      The code defines an arrow function a that takes a parameter n. Inside the function, there is a ternary operator that checks if n is greater than 2. If it is, the function recursively calls itself with the argument (n – 1) and multiplies the result by n. This recursion continues until n reaches 2, at which point the function returns 2. In the console.log(a(6)) statement, the function a is called with the argument 6. The function recursively calculates the factorial of 6 (6 * 5 * 4 * 3 * 2), which results in 720. This value is then printed to the console using console.log.

  24. Examine the following code:

    let x = mult(2)(10);
    console.log(x); // -> 20

    What should the mult function declaration look like if the execution of this code results in a value of 20 in the console?

    • let mult = function (a) {
          return function (b) {
              return a * b;
         }
      }
    • There is an error in the code and it is not possible to declare such a function correctly.
    • let mult = function (a, b) {
          return a * b;
      }
    • let mult = function (a, b) {
           return b ? mult(b) : mult(a);
      }
    • Explanation & Hint:

      The correct declaration for the mult function that would result in the desired output of 20 in the console is:

      let mult = function (a) {
      return function (b) {
      return a * b;
      }
      }

  25. Analyze the code snippet:

    let counter = 0;
    let userName = "John";

    After declaring the counter variable, we want to add a short comment with information about what the variable is used for. To do this, we modify the line with the declaration to the form:

    • // let counter = 0; user visit counter
    • let counter = 0; // user visit counter
    • let counter = 0; ;;user visit counter
    • let counter = 0; /* user visit counter
      Explanation & Hint:

      In the modified line, the comment // user visit counter provides information about the purpose or usage of the counter variable.

  26. Examine the following code:

    x = [40, 10, 30, 20, 50];
    x.sort(cmp);

    How should the function cmp be declared if, after the code execution, the elements of the array x are sorted in ascending order?

    • let cmp = (a, b) => b < a;
    • let cmp = (a, b) => a - b;
    • let cmp = (a, b) => b > a;
    • let cmp = (a, b) => b - a;
      Explanation & Hint:

      This comparison function subtracts b from a, which will result in a negative value if a should be placed before b in the sorted order, a positive value if b should be placed before a, and zero if they are considered equal. Here is the code: let cmp = (a, b) => a – b;

  27. Analyze the following code, which is missing one line:

    let counter = 2;
    let interval = setInterval(() => {
        console.log(counter);
    // Insert missing line here.
    }, 1000);

    What should the missing line look like if the execution of this code results in the console displaying the values 21, and 0 in sequence?

    • if (counter-- >= 0) clearInterval(interval);
    • if (counter-- <= 0) clearInterval(interval);
    • while (counter-- >= 0) clearInterval(interval);
    • clearInterval(interval);
      Explanation & Hint:

      This line checks if the value of counter is less than or equal to 0 after decrementing it. If counter becomes 0 or negative, it means the desired sequence of numbers (2, 1, 0) has been displayed. At that point, clearInterval(interval) is called to stop the interval from continuing.
      The correct line is: if (counter– <= 0) clearInterval(interval);

  28. Analyze the following code:

    function execute(todo, a, b) {
    return todo(a, b);
    }
    console.log(execute(power, 32));

    Before declaring the function, we should add one more line of code. Which one, if the execution of the completed code will result in the console displaying the value 9?

    • let power = () => a ** b;
    • let power = 9;
    • let power = (x,y) => x * y;
    • let power = (x, y) => x ** y;
      Explanation & Hint:

      To ensure that the execution of the completed code results in the console displaying the value 9, the line defines the power function as an arrow function that calculates the power of x raised to the y exponent using the ** operator.

  29. Analyze the following code:

    const a = "hello";
        console.log(a.toUpperCase());
    }
    } catch (error) {
        console.log(a)
    } finally {
        console.log(a);
    }

    What will happen as a result of its execution?

    • The following words will appear in the console on subsequent lines: HELLOhello, and hello.
    • The following words will appear in the console on subsequent lines: HELLO and hello.
    • The following words will appear in the console on subsequent lines: hello and hello.
    • The following word will appear in the console: HELLO.
      Explanation & Hint:

      The line console.log(a.toUpperCase()); will print “HELLO” to the console. It uses the toUpperCase() method to convert the string a to uppercase. The try block does not have any code, so it won’t execute any specific statements. The catch block will not be triggered because there is no error thrown in the preceding code. The finally block will execute regardless of whether there was an error or not. In this case, it will print “hello” to the console, as it references the a variable outside the try-catch block, which is not affected by the toUpperCase() call.

  30. Placing a debugger; statement in the program code will:

    • stop the program without the ability to continue, as long as the execution environment supports “debugging functionality”.
    • pause the program with the ability to continue, as long as the execution environment supports “debugging functionality”.
    • put the interpreter into report mode, which will cause the console to print out all sequentially executed commands.
    • cause the console to display the completion status of the statement preceding the debugger.
      Explanation & Hint:

      Placing a debugger; statement in the program code will pause the execution of the code at that point and activate the debugger. It allows you to inspect the current state of the program, including variable values, call stack, and execute step-by-step or run in debug mode.

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments