JavaScript Essentials 1 (JSE) | JSE1 – Module 2 Test Exam Answers Full 100% 2023 2024

This is the JavaScript Essentials 1 (JSE) | JSE1 – Module 2 Test Exam Answers consisting of all questions and answers with a full score of 100% plus explanations. Our experts have received many questions from students day by day and update the answers here in JSE1 – Module 2 Test Exam Answers Cisco NetAcad SkillsForAll. You can practice and review all the questions and answers before you prepare for the exam. If you are unclear about something, you can comment below the page. We are happy to assist you.

2.4 Module 2 Completion – Module Test Exam Answers Full 100% 2023 2024

  1. We declare an object called dog, with two fields: age and name:

    let dog = {
          age: 5.
          name: "Axel
    };

    To change the value of the age field to 6 , we need to perform:

    • dog{age} = 6;
    • age of dog = 6;
    • dog.age = 6;
    • dog[age] = 6;
      Answers Explanation & Hints:

      To change the value of the age field to 6 in the given object, you would use the syntax:

      dog.age = 6;

      This will update the age field of the dog object to 6.

  2. In order to check the number of elements of the array stored in the names variable, we call:

    • names.length
    • names.count
    • length of names;
    • names.length();
      Answers Explanation & Hints:

      To check the number of elements in the array stored in the names variable, you would use the syntax:

      names.length

      This will return the length or number of elements in the names array.

  3. We need to come up with a name for a variable where we will store the age of a user. All of the following variable names are formally correct, but one of them is the most readable, indicate which one:

    • userAge
    • ua
    • user
    • age
      Answers Explanation & Hints:

      The most readable variable name among the options provided is userAge.

      This name clearly indicates that it stores the age of a user, making it more understandable and self-explanatory compared to the other options.

  4. We can replace the declaration let x = 0x21; with:

    • let x = "0x21"
    • let x = 33;
    • let x = 17;
    • let x = 21;
      Answers Explanation & Hints:

      The replacement for the declaration let x = 0x21; can be:

      let x = 33;
      The hexadecimal value 0x21 represents the decimal value 33, so assigning x to 33 achieves the same result as the original declaration.
  5. In the daysOfWeek variable, we place an array with the names of the days of the week. To reverse the order of the array elements, we should call:

    • reverse daysOfWeek;
    • daysOfWeek.invert();
    • invert(daysOfWeek);
    • daysOfWeek.reverse();
      Answers Explanation & Hints:

      To reverse the order of the array elements stored in the daysOfWeek variable, you should call:

      daysOfWeek.reverse();

      The reverse() method is used to reverse the order of elements in an array in JavaScript. By calling daysOfWeek.reverse(), the elements in the daysOfWeek array will be reversed.

  6. By default, JavaScript allows us to write to an undeclared variable (it declares it implicitly for us). If we want the interpreter to treat such a situation as an error, we have to:

    • perform all writes to variables in a block of code delimited by braces.
    • place the "use strict"; directive before each write we want to protect.
    • place the "use strict"; directive at the beginning of the script.
    • place the "prevent undeclared variables"; directive at the beginning of the script.
      Answers Explanation & Hints:

      To treat writing to an undeclared variable as an error in JavaScript, you would need to place the "use strict"; directive at the beginning of the script.

      So the correct option is:

      place the "use strict"; directive at the beginning of the script.

      By using the "use strict"; directive, you enable strict mode in JavaScript, which helps catch common mistakes and enforces stricter rules for variable declaration and usage. It will treat writing to an undeclared variable as an error instead of implicitly declaring it.

  7. If a variable stores the value false, then the variable:

    • is of the Logical type.
    • is of the Math type.
    • is of the Boolean type.
    • will no longer be used in the program.
      Answers Explanation & Hints:

      If a variable stores the value false, then the variable is of the Boolean type.

      The Boolean type in JavaScript represents a logical value that can have two possible states: true or false. Therefore, when a variable stores the value false, it is considered a Boolean variable. The variable can still be used in the program and can hold other Boolean values or be reassigned to different values as needed.

  8. The msg variable contains a String type value. Information about the number of characters of this string can be obtained using:

    • msg.chars
    • msg.charsAt()
    • msg.length
    • msg.length()
      Answers Explanation & Hints:

      To obtain information about the number of characters in a string stored in the msg variable, you can use the following syntax:

      msg.length

      The length property is used to retrieve the number of characters in a string in JavaScript. By calling msg.length, you will get the length or number of characters in the msg string.

      Note that msg.chars, msg.charsAt(), and msg.length() are not valid ways to get the length of a string in JavaScript.

  9. Analyze the code snippet:

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

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

    • // let counter = 0; user visit counter
    • let counter = 0; // user visit counter
    • let counter = 0; /* user visite counter
    • let counter = 0; # user visite counter
      Answers Explanation & Hints:

      To put a short comment with information about what the counter variable is used for, you can modify the line declaration in the following ways:

      let counter = 0; // user visit counter

      or

      // let counter = 0; user visit counter
      let counter = 0;

      Both of these modifications achieve the desired result by adding a comment next to the declaration of the counter variable. It provides information that the variable is used for counting user visits.

      Note that in JavaScript, single-line comments start with //, and multi-line comments are enclosed between /* and */. The # symbol is not used for comments in JavaScript.

  10. Analyze the code snippet. Identify which variables are local and which are global;

    let name;
    let age;
    {
          let profession;
          {
                 let height;
                 let weight;
           }
    • name ==> Global 
    • profession ==> Local 
    • age ==> Global 
    • height ==> Local 
    • weight ==> Local 
      Answers Explanation & Hints:

      In the given code snippet, the variables can be classified as follows:

      Global variables:

      • name
      • age

      Local variables within the innermost block:

      • pofession
      • height
      • weight

      Based on the scope of the variables, name and age are global variables as they are declared outside of any specific block or function. They can be accessed and modified throughout the entire code.

      On the other hand, height and weight are local variables because they are declared within the innermost block, which is nested inside the outer block. These variables have a limited scope and can only be accessed within the block in which they are declared.

  11. We want to convert the string "1024" to type Number and store the result in variable n. Point out the correct statement

    • let n = Number("1024");
    • let n = String("1024");
    • let n = StringToNumber("1024");
    • let n = "1024" + 0;
    • Answers Explanation & Hints:

      The correct answer is:

      let n = Number("1024");

      This code converts the string “1024” to a number using the Number() function and stores the result in the variable n.

  12. Analyze the code snippet:

    let summer = ["June", "July", "August"];
    let index = summer.indexOf("June");

    The index variable will have the value:

    • 0
    • 1
    • True
    • "June"
    • Answers Explanation & Hints:

      The indexOf() method is used to find the index of the first occurrence of a specified element within an array. In this case, it is used to find the index of the string “June” within the summer array.

      Since arrays in JavaScript are zero-based, meaning the first element has an index of 0, the indexOf() method will return the index of “June” as 0. Therefore, the variable index will have the value 0.

  13. We have declared an array of animals let animals = ["dog", "cat", "hamster"];. Then we call the method animals.push("canary");. As a result, the animals array will look like this:

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

      After calling the method animals.push("canary");, the animals array will look like this:

      [“dog”, “cat”, “hamster”, “canary”]

      The push() method is used to add elements to the end of an array. In this case, it adds the string “canary” to the end of the animals array. Therefore, the resulting array will have the elements “dog”, “cat”, “hamster”, and “canary” in that order.

  14. We perform the operation: let x = "abcdefg".slice(2, 4). As a result, the value:

    • "ab" will be written to the variable x
    • "cd" will be written to  the variable x
    • "cdef" will be written to the variable x
    • "cdefg" will be written to the variable x
    • Answers Explanation & Hints:

      The slice() method is used to extract a portion of a string based on the specified starting and ending indices. In this case, "abcdefg".slice(2,4) will extract characters starting from index 2 (inclusive) up to index 4 (exclusive).

      The resulting substring will be “cd”, and it will be assigned to the variable x.

  15. We want to declare a distance constant and initialize it with the value 120. What should such a declaration look like?

    • const distance = 120;
    • const distance; distance = 120;
    • let distance; const distance = 120;
    • let constant distance = 120;
    • Answers Explanation & Hints:

      In JavaScript, when declaring a constant using the const keyword, you need to provide both the constant name and its initial value in a single statement. Therefore, the correct way to declare and initialize the constant distance with the value of 120 is by using the code snippet above.

  16. Analyze the following code:

    let x = 10 /100;
    console.log(typeof (x));

    As a result of its execution:

    • an error will appear because JavaScript does not allow operations on fractional numbers.
    • it will display 0.1 in the console
    • it will display "number" in the console.
    • it will display "fraction" in the console.
    • Answers Explanation & Hints:

      performs the division operation 10 / 100 and assigns the result to the variable x. In JavaScript, the division operation between two numbers results in a floating-point number.

      The console.log(typeof(x)) statement outputs the type of the variable x to the console. Since the result of the division is a floating-point number, the typeof operator will return "number". Therefore, executing the code will display "number" in the console.

  17. We have declared an array of animals let animals = ["dog", "cat", "hamster"];. Then we call the method animals.pop();. As a result, the animals array will look like this:

    • ["dog", "cat"]
    • ["cat", "hamster"]
    • ["hamster"]
    • ["dog", "cat", "hamster"]
    • Answers Explanation & Hints:

      After calling the method animals.pop();, the animals array will look like this:

      ["dog", "cat"]

      The pop() method is used to remove the last element from an array. In this case, it removes the element “hamster” from the end of the animals array. Therefore, the resulting array will have the elements “dog” and “cat” in that order.

  18. Analyze the code snippet:

    let name;
    let age;
    {
    let height;
    // 2
     {  // 2
     { let weight;  // 1  // 2
     console.log(name);  // 1  // 2
      // 2
       console.log(name);  // 2
    }

    We have access to the weight variable:

    • in the part marked 1.
    • throughout the program.
    • nowhere, as we have no access at all (the variable has not been initialized).
    • in the part marked 2.
    • Answers Explanation & Hints:

       

  19. Performing the operation: let x = 20n + 10; will:

    • result in the string "20n10" being stored in the variable x.
    • cause the program to abort due to an error.
    • result in a value of 30n being stored in the variable x.
    • result in a value of 30 being stored in the variable x.
    • Answers Explanation & Hints:

  20. Performing the operation: let x = 100 / 0; will result in

    • an Infinity value being stored in the variable x.
    • the value NaN being stored in the variable x.
    • the value 0 being stored in the variable x.
    • the value 0 being stored in the variable x.
    • Answers Explanation & Hints:

      In JavaScript, dividing a non-zero number by zero results in a special value called Infinity. Therefore, when performing the operation let x = 100/0;, the value Infinity will be stored in the variable x.

      It’s important to note that dividing zero by zero (0/0) results in NaN (Not a Number), but dividing a non-zero number by zero results in Infinity.

  21. Review the following code (note the variable name):

    let age = 32;
    age = age + 1;
    console.log(Age);

    As a result of its execution, the following should appear in the console:

    • undefined
    • 32
    • error message: "uncaught ReferenceError: Age is not defined" .
    • 33
    • Answers Explanation & Hints:

      In the code snippet provided, there is a mismatch in the variable names. The variable age is declared and assigned the value 32. Then, it is incremented by 1 using age = age + 1;. However, when attempting to console.log(Age);, it refers to a variable Age with a capital “A”, which has not been declared or assigned any value.

      JavaScript is case-sensitive, so Age and age are considered as separate variables. Since Age is not defined, it will result in an “uncaught ReferenceError” when trying to access it.

  22. Performing the operation: let x = "Alice" + 10; will result in:

    • the program execution to abort due to an error.
    • the value NaN of Number type to be stored in the variable x .
    • the value 15 of Number type to be stored in the variable x .
    • the value "Alice10" of String type to be stored in the variable x .
    • Answers Explanation & Hints:

      In JavaScript, when you use the + operator with a string and a number, the number is implicitly converted to a string, and the concatenation of the two values is performed.

      In the code snippet let x = "Alice" + 10;, the string “Alice” is concatenated with the number 10. The result of this operation is the string “Alice10”, which is then stored in the variable x.

  23. Complex (or composite) data types:

    • is an alternative name for primitive types.
    • may consist of multiple elements, of which is of a primitive type.
    • may consist of multiple elements, each of which may be of a primitive or composite type.
    • are not used in JavaScript.
    • Answers Explanation & Hints:

      Complex (or composite) data types in JavaScript can consist of multiple elements, and each element can be of a primitive type or another complex data type. Examples of complex data types in JavaScript include arrays, objects, and functions.

      Primitive types, on the other hand, are not complex data types. They are basic data types that represent simple values, such as numbers, strings, booleans, null, and undefined.

      Therefore, complex data types can have multiple elements, each of which can be either a primitive type or another complex data type, making them more versatile and flexible in storing and representing data.

  24. What does shadowing mean?
    • Declaring a global variable with the same name as a previously declared global variable.
    • Changing the value of a variable.
    • Deleting and rewriting a selected piece of program code.
    • Declaring a local variable with the same name as the previously declared global variable.
    • Answers Explanation & Hints:

      Shadowing refers to the situation where a variable declared within a specific scope (such as a function or block) has the same name as a variable declared in an outer scope. As a result, the outer variable is temporarily “shadowed” or inaccessible within the inner scope, as the local variable takes precedence.

      This can occur when a variable is declared in a nested block, function, or another scope that has its own variable with the same name as a variable in an outer scope. The local variable “shadows” the outer variable, meaning that any references to that variable within the inner scope will refer to the local variable, not the outer one.

      This concept is important to understand to avoid confusion and unintended consequences when dealing with variable scoping in programming languages.

  25. We have declared an array of selected month names let summer = ["June", "July", "August"];. We want to change the value "July" stored in the array to the number 7:

    • summer[1] = 7;
    • We cannot do this (an array can only contain elements of the same type).
    • summer.July = 7;
    • summer[0] = 7;
    • Answers Explanation & Hints:

      To change the value “July” stored in the array summer to the number 7, you can use the index notation ([]) and assign the new value to the corresponding index.

      In JavaScript arrays, elements can be of different types, so you can replace an element in the array with a value of a different type. By using summer[1] = 7;, you are accessing the element at index 1 (which is “July”) and assigning the value 7 to it, effectively changing the element to the number 7.

      Therefore, after executing summer[1] = 7;, the summer array will become ["June", 7, "August"], with the element at index 1 being replaced by the number 7.

  26. Analyze the following code:

    let height = 180;
    {
          let height = 200;
          height = height +10;
    }
    console.log(height);

    As a result of its execution:

    • a value 180 will be displayed in the console.
    • a value 210 will be displayed in the console.
    • a value 200 will be displayed in the console.
    • the program will be terminated due to an error (re-declaration of the height variable).
    • Answers Explanation & Hints:

      In the code snippet provided, there are two occurrences of the height variable. The outer scope declares and assigns the value 180 to height, and within the inner scope, a new block is created where another height variable is declared and assigned the value 200.

      Inside the inner scope, the statement height = height + 10; increases the value of the inner height variable by 10, resulting in 210. However, this change is limited to the inner scope only and does not affect the outer scope’s height variable.

      After the inner block finishes executing, the program continues to the console.log(height); statement in the outer scope. Here, the outer height variable with the value 180 is accessed and displayed in the console. Therefore, the output will be 180.

  27. We have declared an array let animals = ["dog", "cat", "hamster"];. We want to temporarily comment out the element "cat", and to do this, we can modify the declaration as follows:

    • let animals = ["dog", "hamster"];
    • let animals = ["dog", #"cat",# "hamster"];
    • let animals = ["dog", //"cat",// "hamster"];
    • let animals = ["dog", /*"cat",*/ "hamster"];
    • Answers Explanation & Hints:

      In JavaScript, you can use the /* */ syntax for multiline comments. By placing the /* before the element you want to comment out and the */ after it, you effectively turn that part of the code into a comment.

      Therefore, modifying the declaration of the animals array as shown above will temporarily comment out the element “cat”, resulting in an array with only the elements “dog” and “hamster”.

  28. Analyze the following code:

    let counter = 100;
    let counter = 200;
    counter = 300;

    As a result of its execution:

    • the counter variable will have the value 100 .
    • the program will be aborted due to an error (redeclarations of a variable).
    • the counter variable will have the value 300 .
    • the counter variable will have the value 200 .
    • Answers Explanation & Hints:

      In the code snippet provided, there are multiple declarations of the counter variable. JavaScript does not allow redeclaration of variables using the let keyword within the same scope. Therefore, attempting to declare the counter variable more than once will result in an error.

      When the code reaches the second line let counter = 200;, it will throw a “SyntaxError: Identifier ‘counter’ has already been declared” because the counter variable has already been declared in the previous line.

      To fix this issue, you should only declare the counter variable once and then assign different values to it as needed, as shown in the following corrected code:

      let counter = 100;
      counter = 200;
      counter = 300;

      In this case, the counter variable will have the final value of 300 after the code executes.

  29. Point out the correct declaration of the height variable:

    • height;
    • height is variable;
    • let height;
    • variable height;
    • Answers Explanation & Hints:

      In JavaScript, variables are typically declared using the let keyword, followed by the variable name. The statement let height; declares a variable named height without assigning it an initial value.

      Therefore, let height; is the correct syntax for declaring the height variable in JavaScript.

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