JSE1: Module 2 Test Answers (JavaScript Essentials 1)

JavaScript Essentials 1 – JSE1: Module 2 Test Answers

1. 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.
  • in the part marked 2.
  • nowhere, as we have no access at all (the variable has not been initialized).

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

  • the value 15 of Number type to be stored in the variable x.
  • the program execution to abort due to an error.
  • the value NaN of Number type to be stored in the variable x.
  • the value “Alice10” of String type to be stored in the variable x.

3. Analyze the code snippet:

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

The index variable will have the value:

  • 1
  • True
  • 0
  • “June”

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

  • msg.length
  • msg.charsAt()
  • msg.chars
  • msg.length()

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

  • an undefined value being stored in the variable x.
  • the value NaN being stored in the variable x.
  • an Infinity value being stored in the variable x.
  • the value 0 being stored in the variable x.

6. 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 counter variable will have the value 200.
  • the program will be aborted due to an error (redeclaration of a variable).
  • the counter variable will have the value 300.

7. 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:

  • error message: “Uncaught ReferenceError: Age is not defined”.
  • 33
  • 32
  • undefined

8. Complex (or composite) data types:

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

9. 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:

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

10. 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:

  • ua
  • userAge
  • user
  • age

11. Analyze the following code:

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

As a result of its execution:

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

12. 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[0] = 7;
  • summer.July = 7;

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

  • length of names;
  • name.length
  • names.count
  • names.length();

14. 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 “Number” in the console.
  • it will display “Fraction” in the console.
  • it will display 0.1 in the console.

15. 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 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

16. Performing the operation: let x = 20n + 10; will result in:

  • cause the program to abort due to an error.
  • result in a value of 30 being stored in the variable x.
  • result in a value of 30n being stored in the variable x.
  • result in the string “20n10” being stored in the variable x.

17. 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:

  • age of dog = 6;
  • dog{age} = 6;
  • dog.age = 6;
  • dog[age] = 6;

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

  • will no longer be used in the program.
  • is of the Logical type.
  • is of the Boolean type.
  • is of the Math type.

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

  • “cdef” will be written to the variable x.
  • “ab” will be written to the variable x.
  • “cd” will be written to the variable x.
  • “cdefg” will be written to the variable x.

20. 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:

  • daysOfWeek.reverse();
  • invert(daysOfWeek);
  • reverse daysOfWeek;
  • daysOfWeek.invert();

21. 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”, #”cat”,# “hamster”];
  • let animals = [“dog”, //”cat”,// “hamster”];
  • let animals = [“dog”, “hamster”];
  • let animals = [“dog”, /*”cat”,*/ “hamster”];

22. What does shadowing mean?

  • Declaring a local variable with the same name as the previously declared global variable.
  • Changing the value of a variable.
  • Declaring a global variable with the same name as a previously declared global variable.
  • Deleting and rewriting a selected piece of program code.

23. 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

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

  • let constant distance = 120;
  • const distance; distance = 120;
  • let distance; const distance = 120;
  • const distance = 120;

25. 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:

  • place the “use strict”; directive at the beginning of the script.
  • place the “prevent undeclared variables”; directive at the beginning of the script.
  • 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.

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

  • let height;
  • height is variable;
  • height;
  • variable height;

27. We want to convert the string “1024” to type Number and store the result in variable n. Point out the correct statement:

  • let n = StringToNumber(“1024”);
  • let n = String(“1024”);
  • let n = Number(“1024”);
  • let n = “1024” + 0;

28. 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”, “hamster”]
  • [“cat”, “hamster”]
  • [“dog”, “cat”]
  • [“hamster”]

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

  • let x = 33;
  • let x = 21;
  • let x = 17;
  • let x = “0x21”

Leave a Reply

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