0% found this document useful (0 votes)
32 views

Quiz and Test Module 2-SOLVED

Test Python

Uploaded by

josegmay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Quiz and Test Module 2-SOLVED

Test Python

Uploaded by

josegmay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Quiz and Test (JSE) Module 2 - SOLVED

Centro público integrado de formación profesional


Nuevo (desglose IES Campanillas)

Quiz and Test


JavaScript Essentials 1 (JSE)
Module 2
SOLVED

José García JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED

Índice
JAVASCRIPT ESSENTIALS 1 (JSE) - MODULE 2 .............................................................................................................................3
JAVASCRIPT ESSENTIALS 1 (JSE) MODULE 2 ........................................................................................................................................................... 3
Variables, Data Types, Type Casting, and Comments .......................................................................................................................... 3
QUIZ ....................................................................................................................................................................................................................4
TEST ................................................................................................................................................................................................................ 19

José García 2 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
JavaScript Essentials 1 (JSE) - Module 2 - SOLVED

JavaScript Essentials 1 (JSE)


Module 2

Variables, Data Types, Type Casting, and Comments


After completing Module 2, the student will:

• have the knowledge and skills to work with variables (i.e. naming, declaring, initializing and modifying
their values);
• understand concepts such as scope, code blocks, shadowing, and hoisting;
• know the basic properties of primitive data types such as boolean, number, bigint, undefined, null, and
be able to use them;
• be familiar with the basic properties of the primitive data type string, including string literals – single or
double quotes, the escape character, string interpolation, basic properties and methods;
• know the basic properties of complex data types such as Array and Object (treated as a record) and be
able to use them in practice.

José García 3 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Quiz
We declare an array consisting of the names of selected months and store them in the variable summer.
Which declaration is correct?
a. let summer = {“June", "July", "August"');
b. let summer = "June", "July", "August";
c. let summer[0, 1,2] = "June", "July", "August";
d. let summer = [ "June", "July", "August"];

The correct declaration for an array consisting of the names of selected months stored in the
variable summer would be option:

d.- let summer = [ "June", "July", "August"];

In this option, the array is enclosed within square brackets ‘[ ]’, and each element (month name)
is enclosed within double quotes ‘""’. This is the standard syntax for declaring an array in
JavaScript.

The Number type allows the following values in addition to numeric values:
a. -OutOfRange, OutOfRange
b. -Infinity, Infinity, NaN, undefined
c. -Infinity, Infinity, NaN
d. -Infinity, Infinity, NaN, Unknown

The correct option is:

c.- -Infinity, Infinity, NaN

In JavaScript, the Number type allows the following special values in addition to numeric values:

1.- ‘-Infinity’: Represents negative infinity, which is a value lower than any other numeric value.
2.- ‘Infinity’: Represents positive infinity, which is a value higher than any other numeric value.
3.- ‘NaN’ (Not-a-Number): Represents a value that is not a valid number.

These values are used to represent special cases in mathematical operations or calculations.
Option c includes all three of these special values. Options a, b, and d contain either incorrect
values or include values that are not part of the special values for the Number type in JavaScript.

José García 4 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
What operator do we use to check the type of a variable?
a. type
b. variabletype
c. typeof
d. showtype

The correct operator used to check the type of a variable in JavaScript is:

c.- ‘typeof’

The ‘typeof’ operator is used to determine the data type of a variable or an expression. For
example, you can use it like this:

.js
let x = 42;
console.log(typeof x); // Output: "number"

let name = "John";


console.log(typeof name); // Output: "string"

let arr = [1, 2, 3];


console.log(typeof arr); // Output: "object"

It returns a string representing the data type of the variable or expression.

We have declared an array: let animals = ["dog", "cat", "hamster"];. We want to temporarily
comment out the element "hamster", so for this we can modify the declaration as follows:
a. let animals = ["dog", "cat" #, "hamster"#];
b. let animals = ["dog", "cat"];
c. let animals = ["dog", "cat" /*, "hamster"*/];
d. let animals = ["dog", "cat" \\,"hamster”\\];

The correct option to temporarily comment out the element "hamster" from the array declaration
is:

c.- let animals = ["dog", "cat" /*, "hamster"*/];

In JavaScript, you can use the ‘/* */’ syntax to create a multiline comment. Anything between ‘/*’
and ‘*/’ will be treated as a comment and will be ignored by the JavaScript interpreter. This allows
you to effectively "comment out" the element "hamster" from the array declaration.

José García 5 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We want to declare a protocol constant and initialize it with the value "http”. What should such a
declaration look like?
a. let constant protocol="http";
b. const protocol="http";
c. let protocol; const protocol="http";
d. const protocol; protocol="http";

The correct declaration to create a protocol constant and initialize it with the value "http" in
JavaScript is:

b.- const protocol = "http";

The ‘const’ keyword is used to declare constants in JavaScript. Once a constant is assigned a
value, its value cannot be changed or reassigned throughout the program's execution. This
makes it suitable for creating protocol constants that should remain constant and not change
during the program's runtime.

Option a is incorrect because the ‘let’ keyword is used for declaring variables, not constants.
Option c is incorrect because it combines the ‘let’ and ‘const’ keywords, which is not valid syntax.
Option d is incorrect because it separates the declaration and initialization of the constant, which
is not allowed for constants. Once declared, constants must be initialized immediately with their
value.

We perform the operation let x = le2 + 0×10;. As a result, the value of x is:
a. 27
b. NaN
c. 22
d. 116

Let's break down the expression:


.js
let x = le2 + 0×10;
It seems there is a typo in the expression. The variable name le2 is not valid in JavaScript, and
the multiplication operator × is also not the correct operator in JavaScript. The correct
multiplication operator in JavaScript is *.
Assuming the correct expression is:
.js
let x = 12 + 0 * 10;
Now, the expression can be evaluated:
.js
let x = 12 + 0 * 10;
Since any number multiplied by 0 is 0, the result of 0 * 10 is 0. Therefore, the expression
becomes:
.js
let x = 12 + 0;
Adding 0 to any number doesn't change its value. Thus, the result is:
.js
let x = 12;

So, the correct answer is:

c.- 22

José García 6 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the following code:
let height;
console.log (height);
Executing it will:
a. display the message "height" in the console.
b. display the null value in the console.
c. cause the program to terminate with an error because we are referring to a variable that has no value.
d. display the undefined value in the console.

The following code snippet:


.js
let height;
console.log(height);
will display the value undefined in the console.
Explanation:
1.- The variable height is declared using the let keyword. In JavaScript, when you declare a
variable but do not initialize it with a value, the variable is automatically assigned the value
undefined.
2.- When console.log(height) is executed, it will print the value of the variable height to the
console. Since height has been declared but not assigned a value, it will display undefined.

So, the correct answer is:

d.- display the undefined value in the console.

Analyze the code snippet:


let distance = 0;
let userName = "John";
After declaring the distance 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:
a. let distance = 0; /* the distance the user has walked */
b. let distance = 0; ## the distance the user has walked
c. let distance = 0; /* the distance the user has walked
d. // let distance = 0; the distance the user has walked

The correct way to add a short comment to the line with the declaration of the ‘distance’ variable
is:

a.- let distance = 0; /* the distance the user has walked */

In JavaScript, comments are added using the ‘/* */’ syntax for multi-line comments. The comment
is enclosed between ‘/*’ and ‘*/’, and any text within those characters will be ignored by the
JavaScript interpreter. This allows you to add a description or information about the variable,
which can be helpful for documentation and code readability.
So, the correct answer is option a.

José García 7 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the code snippet. Identify which variables are local and which are global.
let height;
let weight;
{
let age;
let sex;
{
let name;
}
}
global: height, weight
local: age, sex, name

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 height variable:
a. throughout the program.
b. nowhere, as we have no access at all (the variable has not been initialized).
c. in the part marked 2.
d. in the part marked 1.

In the given code snippet, we have multiple variables declared at different levels of the code. Let's
analyze them:
.js
let name; // 1 (global)
let age; // 1 (global)
{
let height; // 2 (local)
} // 2
let weight; // 1 (global)
console.log(name); // 1 (global)
Based on the code snippet:
1.- Global Variables: name and age are declared at the top-level scope (outside any curly braces)
and are therefore global variables. They can be accessed throughout the program.
2.- Local Variables: height is declared inside a block (inside the curly braces {}), and as such, it
has block-level scope. It is a local variable and can only be accessed within the block where it is
declared (in the part marked 2).
To summarize:
• Global Variables: name and age
• Local Variables: height

So, the correct answer is:

d.- in the part marked 1.

José García 8 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the code snippet:
let summer = [ "June", "July", "August"1];
let index = summer.indexOf("May") ;
The index variable will have the value:
a. 0
b. -1
c. false
d. undefined

In the code snippet, we have an array named ‘summer’ containing three elements: "June", "July",
and "August". Then, we try to find the index of the element "May" in the ‘summer’ array using the
‘indexOf()’ method.

Here's the code snippet with the correct array declaration:

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

The ‘indexOf()’ method returns the index of the first occurrence of the specified element in the
array. If the element is not found, it returns -1.
In this case, the element "May" is not present in the ‘summer’ array, so the ‘indexOf()’ method
will return -1.

So, the correct answer is:


b.- -1

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

There is a small syntax error in the array declaration in the question. The array should be
declared as follows:
.js
let animals = ['dog', 'cat', 'hamster'];

Now, let's look at the ‘animals.shift()’ method:


The ‘shift()’ method removes the first element from an array and returns that removed element.
After calling animals.shift(), the array will be modified, and the element "dog" will be removed from
the beginning of the array.

So, the resulting array will be:


a.- ["cat", "hamster"]
The ‘shift()’ method removed the element "dog" from the ‘animals’ array.
The correct answer is option a.

José García 9 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We need to come up with a name for a variable where we will store the number of steps the user has
walked. All of the following variable names are formally correct, but one of them is the most readable -
indicate which one:
a. stepCounter
b. stepcounter
c. x
d. sc

The most readable variable name for storing the number of steps the user has walked is:

a.- stepCounter

Descriptive and meaningful variable names are essential for code readability and maintainability.
Using names like "stepCounter" provides clear information about the purpose of the variable,
making the code easier to understand. Options b, c, and d are also formally correct variable
names, but they are not as clear and descriptive as "stepCounter." It's always a good practice to
choose meaningful variable names that accurately represent the data they hold or the purpose
they serve in the code.

Using the string interpolation technique, we can create the string "My favorite season is summer" and
store it in the msg variable using the command:
a. let season = "summer"; let msg = ‘My favourite season is ${season}’;
b. let season = "summer"; let msg = ‘My favourite season is “season”’;
c. let season = "summer"; let msg = "My favourite season is \{season\}";
d. let season = "summer"; let msg = ‘My favourite season is ${season}’;

The correct command to create the string "My favorite season is summer" using string
interpolation is:

a.- let season = "summer"; let msg = 'My favorite season is ${season}';

In JavaScript, string interpolation is done using template literals, which are enclosed within
backticks (`). To interpolate a variable within the string, you need to use the ‘${ }’ syntax. The
variable season will be replaced with its value when using string interpolation.
So, the correct answer is option a.

José García 10 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Review the following code (note the variable name)
let height = 170;
height = height + 10;
console.log (Height);
As a result of its execution, the following should appear in the console:
a. 170
b. 180
c. error message "Uncaught ReferenceError: Height is not defined"
d. "Height"

In the given code snippet, there is a typo in the variable name. The variable name is first declared
as ‘height’, but when it is logged to the console, it is referenced as ‘Height’ with a capital "H".
JavaScript is case-sensitive, so ‘height’ and ‘Height’ are considered different variables.

Let's correct the variable name and analyze the code:

.js
let height = 170;
height = height + 10;
console.log(height);

1.-let height = 170;: Declares a variable height and assigns it the value 170.

2.- height = height + 10;: Adds 10 to the value of the height variable. After this line, height will be
170 + 10 = 180.

3.- console.log(height);: Prints the value of the height variable to the console. The value of height
is 180.

So, the correct answer is:

b.- 180

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


a. “c" will be written to the variable ×.
b. “ab" will be written to the variable ×
c. [“ab", "cdefg"] will be written to the variable x.
d. "cdefg" will be written to the variable x.

The correct answer is:

d.- "cdefg" will be written to the variable x.

The slice() method in JavaScript is used to extract a portion of a string and returns the extracted
part as a new string. The method takes two parameters: the starting index (inclusive) and the
ending index (exclusive) of the slice.
In the given code snippet:
.js
let x = "abcdefg".slice(2);
The slice(2) extracts the substring starting from index 2 of the string "abcdefg". So, it will extract
characters from index 2 till the end of the string. The result will be "cdefg".
Therefore, the variable x will be assigned the value "cdefg".

José García 11 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the following code:
let height = 180;
{
let height = 200;
height = height + 10;
console.log (height);
}
As a result of its execution:
a. a value of 180 will be displayed in the console.
b. a value of 210 will be displayed in the console.
c. a value of 200 will be displayed in the console.
d. the program will be terminated due to an error (a re-declaration of the height variable).

The correct answer is:

b.- A value of 210 will be displayed in the console.

Let's break down the code step-by-step:

1.- ‘let height = 180;’: Declares a variable ‘height’ with a value of ‘180’.
2.- Inside the block (enclosed in curly braces ‘{ }’), a new variable ‘height’ is declared with a
value of ‘200’. However, this is a new local variable, and it does not affect the outer variable with
the same name.
3.- ‘height = height + 10;’: The value of the local ‘height’ (which is ‘200’) is increased by 10, so it
becomes ‘210’.
4.- ‘console.log(height);’: Prints the value of the local ‘height’ (inside the block) to the console,
which is ‘210’.

The ‘height’ variable inside the block is separate from the ‘height’ variable declared outside the
block. They are two distinct variables, each with its own scope.
So, the correct answer is option b. A value of 210 will be displayed in the console.

José García 12 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Adding the suffix ...n to the end of a number, e.g. 100n, means that:
a. it is a value of the Number type written in decimal format.
b. it is a value of an unspecified type.
c. it is an integer of type Number.
d. it is a value of the Biglnt type.

The correct answer is:

d.- It is a value of the BigInt type.

In JavaScript, adding the suffix ‘n’ to the end of a number, like ‘100n’, indicates that the number is
a BigInt. BigInt is a numeric data type introduced in ECMAScript 2020 to represent integers with
arbitrary precision. It allows you to work with very large integers beyond the normal range
supported by the standard Number type.

Here's an example of using BigInt:


.js
let bigNumber = 100n;
console.log(bigNumber); // Output: 100n

In this example, ‘bigNumber’ is a BigInt variable representing the value ‘100’.


Option a is incorrect because numbers in decimal format are represented without the ‘n’ suffix,
like ‘100’ (without ‘n’).
Option b is incorrect because adding n at the end explicitly indicates that it is a BigInt value.
Option c is incorrect because BigInt can represent both integers and non-integers, but it has the
capability to represent integers with arbitrary precision, which is its main advantage over the
standard Number type.

In the summer variable, we have stored an array with the names of the selected months: let summer =
["June", "July", "August"];
We want to check if there is an element "August" in the array and determine its position (index). To do so, we
call:
a. summer.index0f("August"):
b. summer.index0f(August);
c. summer.index("August");
d. index of "August" in summer;

The correct way to check if there is an element "August" in the ‘summer’ array and determine its
position (index) is by calling the ‘indexOf()’ method:

a.- summer.indexOf("August");

In JavaScript, the ‘indexOf()’ method is used to find the index of a specified element in an array.
It returns the index of the first occurrence of the element if it exists in the array, and if the element
is not found, it returns -1.

So, in this case, calling ‘summer.indexOf("August")’ will return the index of "August" in the
‘summer’ array, which is 2, since arrays are zero-indexed.

Therefore, the correct answer is option a.

José García 13 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We can replace the declaration let x = 2e4; with:
a. let x = 24:
b. let x = 24000;
c. let x = 0.0002;
d. let x = 20000;

The declaration ‘let x = 2e4;’ is using scientific notation to represent a number. The notation ‘2e4’
means 2 multiplied by 10 raised to the power of 4, which is equivalent to 2 * 10^4 = 20000.

So, to replace the declaration let x = 2e4;, we should choose the option that represents the value
20000:

d.- let x = 20000;

Option d is the correct replacement for the given declaration.

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

The correct result after calling the method ‘animals.unshift("canary")’ will be:

a.- ["canary", "dog", "cat", "hamster"]

The ‘unshift()’ method is used to add one or more elements to the beginning of an array. In this
case, it adds the string ‘"canary"’ to the beginning of the ‘animals’ array. After the ‘unshift()’
method is called, the modified ‘animals’ array will have the new element "canary" at the
beginning, followed by the original elements ‘"dog"’, ‘"cat"’, and "hamster".
So, the correct answer is option a. ‘["canary", "dog", "cat", "hamster"]’.

A local variable is a variable:


a. that can only be used in a program executed on a local machine.
b. whose scope of visibility is limited only to a fragment of the program, e.g. inside a function.
c. that is visible in the whole program code, but we can modify it only in a specific part of the program, for
example, inside a function.
d. that we cannot modify.

The correct answer is:

b.- whose scope of visibility is limited only to a fragment of the program, e.g., inside a function.

A local variable is a variable that is declared inside a specific block of code (usually inside a
function) and is only accessible within that block or its nested blocks. Once the execution flow
moves out of that block, the variable becomes inaccessible, and its value is no longer available
for use in other parts of the program.

Local variables have limited scope, and they are typically used to store temporary data or
intermediate values within a specific context or function. This provides better encapsulation and
helps avoid conflicts with variables in other parts of the program.
So, option b is the correct definition of a local variable.

José García 14 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We want to convert the number 1024 to a String type and store the result in the variable s . Point out the
correct statement:
a. let s = String(1024);
b. let s = Number(1024);
c. let g = 1024+"0";
d. let g = NumberToString(1024);

The correct statement to convert the number 1024 to a String type and store the result in the
variable s is:

a.- let s = String(1024);

In JavaScript, the String() function can be used to explicitly convert a value to a string. In this
case, we want to convert the number 1024 to its string representation, so we use the String()
function to achieve that.

Option b is incorrect because it uses the Number() function, which would convert the number to a
numeric value, not a string.
Option c is incorrect because it appends the string "0" to the number 1024, which results in the
string "10240" and not just "1024".
Option d is incorrect because there is no built-in function called NumberToString() in JavaScript.
So, the correct answer is option a. let s = String(1024);

We have declared an array of names:


let names = ["Olivia", "Emma", "Mia", "Sofia*];
We want to display the name "Emma" on the console, and to do this we call:
a. console.log(names(2});
b. console.log(names[2]);
c. console.log(names.Emma);
d. console.log(names[1]);

The correct way to display the name "Emma" from the names array on the console is:

b.- console.log(names[1]);

In JavaScript, arrays are zero-indexed, meaning the first element is at index 0, the second
element is at index 1, and so on. Since "Emma" is the second element in the names array, its
index is 1.
Using names[1] will access the element at index 1 in the names array, which is "Emma".
Therefore, this is the correct statement to display "Emma" on the console.
So, the correct answer is option b. console.log(names[1]);

Point out the correct declaration of the temperature variable:


a. variable temperature,
b. declare temperature;
c. temperature is variable;
d. let temperature;

The correct declaration of the temperature variable in JavaScript is:

d.- let temperature;

In JavaScript, the let keyword is used to declare a variable. Using let declares a block-scoped
variable that can be reassigned with a new value later in the program.
So, the correct answer is option d. let temperature;

José García 15 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Which of the following declarations using the String type is incorrect?
a. let msg = 'The vessel "Mars" called at the port';
b. let msg = "The vessel "Mars" called at the port";
c. let msg = "The vessel 'Mars' called at the port";
d. let msg = "The vessel \"Mars\" called at the port";

The incorrect declaration using the String type is:

b.- let msg = "The vessel "Mars" called at the port";

In JavaScript, when declaring a string using double quotes, any double quote within the string
itself must be escaped with a backslash \. This is necessary to differentiate between the quotes
used to define the beginning and end of the string and any quotes used inside the string content.

Correct versions for the other options are as follows:


a. let msg = 'The vessel "Mars" called at the port';
c. let msg = "The vessel 'Mars' called at the port";
d. let msg = "The vessel \"Mars\" called at the port";

So, the correct answer is option b. let msg = "The vessel "Mars" called at the port";

We have initialized the name variable with the value "Alice", then we try to write the number 100 into it.
let name = "Alice";
name = 100;
As a result of this:
a. the variable will retain the value "Alice" (we cannot modify the value that the variable has been
initialized with).
b. the variable will contain the string "100"
c. the variable will contain the number 100.
d. the program will be aborted due to an error (we are trying to insert a value of a different type into the
variable than the value with which it was initialized).

The correct answer is:

c.- The variable will contain the number 100.

In JavaScript, variables are dynamically typed, which means you can reassign them to different
types of values during the program's execution. So, even if the variable name is initialized with a
string value "Alice," you can later reassign it to a number.

In the given code snippet:


.js
let name = "Alice";
name = 100;

The variable name is first assigned the value "Alice". However, the subsequent assignment name
= 100 reassigns the variable with the number 100.
After this code is executed, the variable name will contain the number 100, and the original string
value "Alice" will be overwritten.

So, the correct answer is option c. The variable will contain the number 100.

José García 16 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Select a set of data types, containing only primitive types:
a. Array, Boolean, Number, String.
b. Boolean, Number, Bigint, String.
c. Array, Boolean, Number, Bigint, String.
d. Array, Object.

The correct set of data types containing only primitive types is:

b.- Boolean, Number, Bigint, String.

In JavaScript, primitive data types include:


• Boolean: Represents true or false.
• Number: Represents numeric values, including integers and floating-point numbers.
• BigInt: Represents integers with arbitrary precision (introduced in ECMAScript 2020).
• String: Represents sequences of characters.

Arrays and Objects are not considered primitive data types; they are considered composite or
reference types.

So, the correct answer is option b. Boolean, Number, Bigint, String.

We define an object that will contain one field with the key name. We will place the value "Bob" in this
field and store the object in the variable obj. Indicate the correct declaration:
a. let obj = name:"Bob"
b. let obj = name.Bob;
c. let obj = name:(“Bob");
d. let obj = (name:"Bob");

The correct declaration for defining an object with a field named "name" and storing the value
"Bob" in it is:

a.- let obj = { name: "Bob" };

In JavaScript, objects are defined using curly braces {}. Each field in the object is specified with a
key-value pair, where the key (field name) is followed by a colon : and then the corresponding
value.

So, the correct answer is option a. let obj = { name: "Bob" };

José García 17 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
In the animals variable, we store an array of animal names. We want to enlarge this array by appending
elements of the otherAnimals array to it. To do this, we can call the command:
a. animals.concat(otherAnimals);
b. otherAnimals.concat(animals);
c. animals.merge (otherAnimals);
d. animals=animals.concat(otherAnimals);

The correct command to enlarge the animals array by appending elements of the otherAnimals
array to it is:

d.- animals = animals.concat(otherAnimals);

In JavaScript, the concat() method is used to merge arrays. It does not modify the original array
but instead returns a new array that is the result of concatenating the arrays on which it is called.
If you want to update the animals array with the concatenated result, you need to reassign the
result back to the animals variable.
So, option d is the correct command to achieve the desired result. It appends the elements of
otherAnimals to the animals array and updates the animals variable with the new concatenated
array.

José García 18 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Test
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:
a. age of dog = 6;
b. dog (age] = 6;
c. dog.age = 6;
d. dog [age] = 6;

The correct way to change the value of the age field in the dog object to 6 is:

c.- dog.age = 6;

In JavaScript, you can access and modify object properties using the dot notation. To change the
value of the age field in the dog object, you simply use dot notation followed by the new value.

So, the correct answer is option c. dog.age = 6;

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:
a. let counter = 0; # user visit counter
b. let counter = 0; /* user visit counter
c. let counter = 0; // user visit counter
d. // let counter = 0; user visit counter

The correct way to put a short comment with information about what the counter variable is used
for is:

c.- let counter = 0; // user visit counter

In JavaScript, you can add comments using the // syntax for single-line comments. Anything after
// on the same line will be treated as a comment and will be ignored by the JavaScript interpreter.
This allows you to add a short comment to provide information about the variable.

So, the correct answer is option c. let counter = 0; // user visit counter

José García 19 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
What does shadowing mean?
a. Deleting and rewriting a selected piece of program code.
b. Changing the value of a variable.
c. Declaring a local variable with the same name as the previously declared global variable.
d. Declaring a global variable with the same name as a previously declared global variable.

c.- Declaring a local variable with the same name as the previously declared global variable.

Shadowing in programming refers to the act of declaring a new variable within a nested scope
(like a function or a block) with the same name as a variable declared in an outer scope. This new
variable "shadows" or temporarily hides the variable from the outer scope within the inner scope.

For example:
.js
let x = 10; // Global variable x

function example() {
let x = 5; // Local variable x, shadows the global variable x within
this function
console.log(x); // Output: 5
}

example();
console.log(x); // Output: 10 (refers to the global variable x)

In this example, we have a global variable x with a value of 10. Inside the example() function, we
declare a new local variable x with a value of 5. This local variable shadows the global variable x
within the function, and any reference to x inside the function will refer to the local variable
instead of the global one.

So, the correct answer is option c. Declaring a local variable with the same name as the
previously declared global variable.

José García 20 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. place the "prevent undeclared variables"; directive at the beginning of the script.
b. perform all writes to variables in a block of code delimited by braces.
c. place the "use strict"; directive at the beginning of the script.
d. place the "use strict"; directive before each write we want to protect.

The correct answer is:

c.- Place the "use strict"; directive at the beginning of the script.

In JavaScript, the "use strict" directive is used to enable strict mode in the script or a specific
function. When strict mode is enabled, certain actions that are considered errors in JavaScript are
treated as actual errors and produce runtime exceptions. One of the behaviors of strict mode is
that it disallows creating global variables implicitly by writing to an undeclared variable. Instead, it
will raise an error when attempting to write to an undeclared variable, making the code more
robust and less error-prone.

To enable strict mode for the entire script, you place the "use strict"; directive at the beginning of
the script before any other code. For example:
.js
"use strict";

// Rest of the script...

Once strict mode is enabled, any attempts to write to an undeclared variable will result in a
ReferenceError, alerting you to the mistake and preventing potential issues caused by
accidentally creating global variables.

So, the correct answer is option c. Place the "use strict"; directive at the beginning of the script.

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


a. result in a value of 30n being stored in the variable x .
b. result in the string "20n10" being stored in the variable x .
c. result in a value of 30 being stored in the variable x
d. cause the program to abort due to an error.

The correct answer is:

c.- The operation will result in a value of 30 being stored in the variable x.

In the operation let x = 20 + 10;, JavaScript will perform addition between the two numeric values
20 and 10. The result of this addition is 30, which is then stored in the variable x.
Option a is incorrect because the n suffix is not used here, so the result is not a BigInt.
Option b is incorrect because the operation performs numeric addition, not string concatenation.
Option d is incorrect because there is no error in the given operation.

So, the correct answer is option c. The operation will result in a value of 30 being stored in the
variable x.

José García 21 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. ["cat", "hamster"]
b. ["dog", "cat"]
c. ["hamster"]
d. ["dog", "cat", "hamster"]

The correct answer is:

a. ["cat", "hamster"]

The pop() method in JavaScript is used to remove the last element from an array and returns that
removed element. When you call animals.pop(), it removes the last element "hamster" from the
animals array and returns it.

So, after calling animals.pop(), the animals array will look like ["cat", "hamster"].
The correct answer is option a. ["cat", "hamster"].

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:
a. ["dog", "cat", "hamster", "canary"]
b. ["canary"]
c. ["dog", "cat", "hamster"]
d. ['canary", "dog", catm, "hamster"]

The correct answer is:

a.- ["dog", "cat", "hamster", "canary"]

The push() method in JavaScript is used to add one or more elements to the end of an array.
When you call animals.push("canary"), it adds the string "canary" to the end of the animals array.
So, after calling animals.push("canary"), the animals array will look like ["dog", "cat", "hamster",
"canary"].
The correct answer is option a. ["dog", "cat", "hamster", "canary"].

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


a. is of the Logical type.
b. is of the Boolean type.
c. will no longer be used in the program.
d. is of the Math type.

The correct answer is:

b.- is of the Boolean type.

In JavaScript, the Boolean type has two possible values: true and false. It represents a logical
entity that can have one of these two values. When a variable stores the value false, it is
considered to be of the Boolean type.
So, the correct answer is option b. is of the Boolean type.

José García 22 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
The msg variable contains a String type value. Information about the number of characters of this string
can be obtained using:
a. msg.chars
b. msg.length
c. msg.length()
d. msg.charsAt()

The correct way to obtain information about the number of characters in the string stored in the
msg variable is:

b.- msg.length

In JavaScript, the length property is used to get the number of characters (the length) of a string.
It returns the number of characters in the string as an integer.

So, the correct answer is option b. msg.length.

Performing the operation: let × = "Alice" + 10; will result in:


a. the value "Alice10" of String type to be stored in the variable *
b. the program execution to abort due to an error.
c. the value NaN of Number type to be stored in the variable x
d. the value 15 of Number type to be stored in the variable x

The correct answer is:

a.- The value "Alice10" of String type will be stored in the variable x.

In JavaScript, when you perform the operation "Alice" + 10, the + operator is used for string
concatenation when one of the operands is a string. It converts the number 10 to its string
representation and concatenates it with the string "Alice". The result of this operation is the string
"Alice10".

So, the correct answer is option a. The value "Alice10" of String type will be stored in the variable
x.

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:
a. user
b. userAge
c. age
d. ua

The most readable variable name for storing the age of a user is:

b.- userAge

Using descriptive and meaningful variable names is essential for writing clean and maintainable
code. The variable name "userAge" clearly indicates that it is used to store the age of a user,
making it more understandable and readable compared to the other options.

So, the correct answer is option b. userAge.

José García 23 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the following code:
let height = 180;
{
let height = 200;
height = height + 10;
}
console.log(height);
As a result of its execution:
a. a value of 180 will be displayed in the console.
b. the program will be terminated due to an error (re-declaration of the height variable).
c. a value of 200 will be displayed in the console.
d. a value of 210 will be displayed in the console.

The correct answer is:

a.- A value of 180 will be displayed in the console.

In this code, we have two separate block scopes. The outer block scope defines a variable height
with the value 180. The inner block scope also defines a variable height using let, but it is a
separate variable limited to that inner block. The re-declaration of height inside the inner block
does not affect the outer height variable.

Inside the inner block, we update the value of the inner height variable by adding 10 to it, making
it 200 + 10, which is 210. However, this change only affects the inner block's height variable, not
the outer one.

When we log height outside of the inner block, it refers to the outer height variable, which remains
unchanged with the value 180. So, the result displayed in the console will be 180.

Therefore, the correct answer is option a. A value of 180 will be displayed in the console.

Analyze the following code:


let x = 10 / 100;
console.log (typeof (x));
As a result of its execution:
a. it will display "number" in the console.
b. it will display "fraction" in the console.
c. it will display 0.1 in the console.
d. an error will appear because JavaScript does not allow operations on fractional numbers.

The correct answer is:

a.- It will display "number" in the console.

In the provided code, we first calculate the value of x by performing the arithmetic operation 10 /
100, which results in 0.1. This value is assigned to the variable x.
Next, we use the typeof operator to check the type of the variable x, and when we log it to the
console, it will display "number".

So, the correct answer is option a. It will display "number" in the console.

José García 24 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We want to declare a distance constant and initialize it with the value 120.
What should such a declaration look like?
a. const distance = 120;
b. const distance; distance = 120;
c. let constant distance = 120;
d. let distance; const distance = 120;

The correct declaration of a distance constant and initializing it with the value 120 is:

a.- const distance = 120;

In JavaScript, the const keyword is used to declare constants. Once a constant is declared and
assigned a value, its value cannot be changed or reassigned throughout the program's execution.
Option b is incorrect because you cannot declare a constant and initialize it later in a separate
statement.
Option c is incorrect because you should not include the word "constant" when declaring a
constant using the const keyword.
Option d is incorrect because you should not use both let and const together when declaring a
constant.

So, the correct answer is option a. const distance = 120;

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


a. "ab" will be written to the variable x
b. radef" will be written to the variable x
c. " cde£g" will be written to the variable x
d. "cd" will be written to the variable x

The correct answer is:

d.- "cd" will be written to the variable x.

In the given code snippet, the slice() method is used on the string "abcdefg" with the parameters
2 and 4. The slice() method extracts a section of a string and returns a new string containing the
specified portion.
The first parameter of slice() represents the starting index from where the extraction should begin,
and the second parameter is the index where the extraction should end (exclusive). So, slice(2, 4)
will extract characters at index 2 and index 3 (the character at index 4 is not included).
The characters at index 2 and 3 in the string "abcdefg" are "c" and "d", respectively. Therefore,
the result of let x = "abcdefg".slice(2, 4) will be the string "cd".

So, the correct answer is option d. "cd" will be written to the variable x.

José García 25 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. 33
b. 32
c. undefined
d. error message: "Uncaught ReferenceError: Age is not defined"

The correct answer is:

d.- Error message: "Uncaught ReferenceError: Age is not defined"

In the given code, there is a case-sensitivity issue with the variable name. The variable age is
declared with a lowercase "a", but when trying to log the value using console.log(Age), it uses an
uppercase "A". JavaScript is case-sensitive, so "age" and "Age" are treated as two different
variables.
After executing the code, the console.log(Age) line will throw a ReferenceError because the
variable "Age" is not defined. The correct variable name is "age" with a lowercase "a".

To log the updated value of the age variable correctly, it should be:
.js
console.log(age); // Output: 33

So, the correct answer is option d. Error message: "Uncaught ReferenceError: Age is not
defined".

In order to check the number of elements of the array stored in the names variable, we call:
a. names. count
b. length of names;
c. names. length ();
d. names. Length

The correct way to check the number of elements in the array stored in the names variable is:

c.- names.length

In JavaScript, arrays have a length property that returns the number of elements in the array.
When you use names.length, it will give you the count of elements in the names array.

So, the correct answer is option c. names.length().

José García 26 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
We can replace the declaration let x = 0x21; with:
a. let x = 17;
b. let x = 33;
c. let x = "0x21"
d. let x = 21;

The correct replacement for the declaration let x = 0x21; is:

a.- let x = 17;

In JavaScript, 0x is a prefix that indicates a hexadecimal number. The hexadecimal number 0x21
is equal to the decimal number 33.

So, the correct answer is option a. let x = 17;.

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
a. summer [1] = 7;
b. summer [0] = 7;
c. summer.July = 7;
d. We cannot do this (an array can only contain elements of the same type).

The correct answer is:

a.- summer[1] = 7;

In JavaScript arrays, you can access and modify elements by their index using square brackets [].
The index of the array starts from 0, so to change the value "July" to the number 7 in the summer
array, you need to access the element at index 1 (which is "July") and assign the value 7 to it.

So, the correct answer is option a. summer[1] = 7;. After executing this code, the summer array
will look like ["June", 7, "August"].

José García 27 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. nowhere, as we have no access at all (the variable has not been initialized).
b. in the part marked 1.
c. in the part marked 2.
d. throughout the program.

The correct answer is:

a.- Nowhere, as we have no access at all (the variable has not been initialized).

In the given code snippet, the variable weight is declared inside a nested block scope, and it is
not initialized with any value. When a variable is declared but not assigned a value, it has an
initial value of undefined. However, the variable weight is never assigned any value, and it is only
accessible within the innermost block where it is declared.
When you try to log weight outside of its block scope, it will result in an error because weight is
not defined in that scope.

So, the correct answer is option a. Nowhere, as we have no access at all (the variable has not
been initialized).

We want to convert the string "1024" to type Number and store the result in variable n . Point out the
correct statement:
a. let n = "1024” + 0;
b. let n = String ("1024");
c. let n = StringToNumber ("1024");
d. let n = Number ("1024");

The correct statement to convert the string "1024" to a Number and store the result in the variable
n is:

d.- let n = Number("1024");

In JavaScript, you can use the Number() function to convert a string to a Number data type.
When you pass the string "1024" as an argument to the Number() function, it will convert it to the
number 1024 and store the result in the variable n.
So, the correct answer is option d. let n = Number("1024");.

José García 28 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. let animals = ["dog", #"cat", # "hamster"];
b. let animals = ["dog", //"cat", // "hamster"];
c. let animals = ["dog", "hamster"];
d. let animals = ["dog", /*"cat", */ "hamster");

The correct way to temporarily comment out the element "cat" in the animals array is:

c.- let animals = ["dog", "hamster"];

In JavaScript, comments are added using // for single-line comments and /* */ for multi-line
comments. To remove an element from an array temporarily, you can simply remove it from the
array declaration.

So, the correct answer is option c. let animals = ["dog", "hamster"];. This will remove the "cat"
element from the animals array.

Point out the correct declaration of the height variable:


a. height is variable;
b. let height;
c. height;
d. variable height;

The correct declaration of the height variable is:

b.- let height;

In JavaScript, you can declare variables using the let keyword. The syntax to declare a variable is
let variableName;, where variableName is the name you want to assign to the variable.
Option b is the correct one, as it follows the proper syntax for declaring a variable.

So, the correct answer is option b. let height;.

Complex (or composite) data types:


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

The correct answer is:

b.- May consist of multiple elements, each of which may be of a primitive or composite type.

Complex (or composite) data types in programming are data types that can hold multiple values
or elements. These data types are composed of smaller parts, which can be either primitive data
types (e.g., numbers, strings, booleans) or other complex data types (e.g., arrays, objects).
In JavaScript, complex data types include arrays, objects, functions, classes, and other data
structures that can hold multiple values or elements.

So, the correct answer is option b. May consist of multiple elements, each of which may be of a
primitive or composite type.

José García 29 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Analyze the following code:
let counter = 100;
let counter = 200;
counter = 300;
As a result of its execution:
a. the counter variable will have the value 200.
b. the program will be aborted due to an error (redeclaration of a variable).
c. the counter variable will have the value 300.
d. the counter variable will have the value 100.

The correct answer is:

b.- The program will be aborted due to an error (redeclaration of a variable).

In JavaScript, you cannot declare the same variable more than once within the same scope. In
the given code snippet, the counter variable is declared twice:
let counter = 100;
let counter = 200;
This will cause a syntax error because the counter variable is being redeclared, and it is not
allowed.

So, the correct answer is option b. The program will be aborted due to an error (redeclaration of a
variable).

Analyze the code snippet:


let summer = ["June", "July", "August"]:
let index = summer. indexOf ("June");
The index variable will have the value:
a. 0
b. "June"
c. True
d. 1

The correct answer is:

a.- 0

The indexOf() method in JavaScript returns the index of the first occurrence of the specified
element in an array. In the given code snippet, the summer array contains the elements ["June",
"July", "August"]. When you call summer.indexOf("June"), it will search for the string "June" in the
summer array and return its index, which is 0 since "June" is the first element in the array.

So, the correct answer is option a. 0. The index variable will have the value 0.

José García 30 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
Performing the operation: let x = 100 / 0; will:
a. the value 0 being stored in the variable x.
b. the value NaN being stored in the variable x.
c. an undefined value being stored in the variable x.
d. an Infinity value being stored in the variable x.

The correct answer is:

d.- An Infinity value being stored in the variable x.

In JavaScript, dividing a number by 0 results in Infinity. When you perform the operation let x =
100 / 0;, the value of x will be Infinity.

So, the correct answer is option d. An Infinity value being stored in the variable x.

Analyze the code snippet. Identify which variables are local and which are global:
let name
let age:
{
let profession
{
let height;
let weight;
}

a. name
b. profession
c. age
d. height
e. weight

The correct classification of variables as local and global is:

Global variables: a. name c. age


Local variables: b. profession d. height e. weight

In the given code snippet, variables declared outside any curly braces {} are global variables, and
variables declared inside curly braces are local variables within their respective scopes.
name and age are declared outside any curly braces, so they are global variables.
profession is declared inside the first set of curly braces, so it is a local variable within that scope.
height and weight are declared inside the second set of curly braces, so they are local variables
within the nested scope of profession.

So, the correct answer is: Global variables: a. name, c. age Local variables: b. profession, d.
height, e. weight

José García 31 JavaScript - JSE-40-01


Quiz and Test (JSE) Module 2 - SOLVED
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:
a. invert(daysOfWeek);
b. daysOfWeek.invert();
c. reverse daysOfWeek;
d. daysOfWeek.reverse();

The correct way to reverse the order of the elements in the daysOfWeek array is:

d.- daysOfWeek.reverse();

In JavaScript, the reverse() method is used to reverse the order of the elements in an array.
When you call daysOfWeek.reverse(), it will modify the original array daysOfWeek and reverse its
elements.

So, the correct answer is option d. daysOfWeek.reverse(); This will reverse the order of the
elements in the daysOfWeek array.

José García 32 JavaScript - JSE-40-01

You might also like