Java Script Essentials 1 Answers
Java Script Essentials 1 Answers
log(“test”);
console(“test”);
console.log(“test”);
2. The basic toolkit needed to effectively develop JavaScript code consists of two
elements:
interpreter, debugger.
the original name for JavaScript, which has been changed over time.
a new language based on JavaScript which, among other things, introduces static
typing.
4. The <html> tag indicates the start of the actual HTML document. What tag should
appear at the end of the document?
<html/>
</html>
<lmth>
<html>
On a mobile device.
6. What HTML tag is used to indicate that we are embedding JavaScript code?
<source>
<script>
<js>
<execute>
Transforming the individual commands of the source code into the target form.
8. In the browser, we type into the address bar a string starting with file:///. This
means that:
we want the browser to create an empty page for us with the name we give as a
continuation of the string.
the rest of the string is the path to the file on our local computer that we want to
open in the browser.
we do not want to open the page, but only download a file from the remote address and
save it on the local machine.
9. In a browser, we want to open a local file by typing the path to it in the address bar.
The path must be preceded by:
http://
local:///
https://
file:///
runs directly in a web browser and can contain an HTML document inside it.
a network protocol.
display on the console information about the progress of http://test.org page loading.
13. What tag do we use in HTML to denote the main part of the document inside
which we define the page elements?
<head>
<main>
<body>
<content>
generate a page with information about the browser’s status and send it to the
developer.
let age;
let height; // 2
{ // 2
let weight; // 1 // 2
console.log(name); // 1 // 2
} // 2
console.log(name); // 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.
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()
counter = 300;
age = age + 1;
console.log(Age);
33
32
undefined
[“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
console.log(height);
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();
console.log(typeof (x));
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
16. Performing the operation: let x = 20n + 10; will result in:
cause the program to abort due to an error.
17. We declare an object called dog, with two fields: age and name:
let dog = {
age: 5.
name: "Axel"
};
dog{age} = 6;
dog.age = 6;
dog[age] = 6;
19. We perform the operation: let x = “abcdefg”.slice(2, 4). As a result, the value:
“cdef” 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”];
Declaring a global variable with the same name as a previously declared global variable.
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;
place the “prevent undeclared variables”; directive at the beginning of the script.
place the “use strict”; directive before each write we want to protect.
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”]
let x = 21;
let x = 17;
let x = “0x21”
1
true
null
false
true
“abcd”
After its execution, the variables x, y, and z will have the values:
x: true, y: false, z: 1
x: 1, y: 1, z: 1
x: false, y: true, z: 1
x: null, y: null, z: 1
-14
NaN
false
let m = ++n;
Its execution will result in the following values in the variables n and m:
n: 10, m: 11
n: 11, m: 10
n: 11, m: 11
n: 10, m: 10
6. The confirm method allows you to create a dialog box. What value does this
method return when the user closes the window?
The string entered by the user.
Always true.
Always false.
7. Which operator do we use if we want to check if two variables store the same
values of exactly the same type?
=
!==
==
===
0
false
“false”
9. The number 2 is stored in the variable n (let n = 2;). The command n = n*n*n is then
called. This last command can be replaced by:
n ***= n;
n *= 3;
n **= 3;
n **= n;
What value will the test variable have if, after running the code, we immediately press
the OK button on the newly created dialog?
“OK”
“World”
true
“Hello”
5
true
25
215
18
36
The alert, confirm, and prompt methods display information in modal windows that block
access to the page until they are closed.
You can call window methods, such as window.alert, without including the name
window, so calling alert(“abc”) would be correct.
The alert, confirm, and prompt methods require an argument specifying the
position of the dialog box in which the information will be displayed.
14. The string “12” has been written into the str variable: (let str = “12”;). Then the
operation str = +str is performed. As a result, the variable str will contain:
“+12”
12
“12”
NaN
if (x < 100)
x = 20;
console.log(x)
100
false
5. Review the following code snippet:
if (counter <= 10 && confirm === false) {
console.log("test");
What values can the counter and confirm variables have so that the console
displays "test" as a result of code execution?
counter: 10 , show: true
counter: 11 , show: false
counter: 10 , show: false
counter: 9 , show: true
6. The condition if( a >= 0 ) can be replaced by the condition:
if (a > 0 || a == 0);
if (!(a > 0 && a == 0));
if (a > 0 && a == 0);
if (0 < a);
7. Which of the following loop instructions is intended only to loop through all the
keys of the indicated object?
for ... in
do ... while
for ... of
if ... else
8. Analyze the code below:
if (counter === 10) {
console.log("abc");
How can we write the same condition using the switch statement?
switch(counter) {case 10: console.log("abc")};
switch(counter) case 10: console.log("abc");
case(counter) {switch 10: console.log("abc")};
switch(counter) {case ? 10 : console.log("abc")};
9. We want to rewrite the following code snippet using the conditional operator:
let name;
if (test) {
name = 10;
} else {
name = 20;
console.log(1);
console.log("test");
How many times will "test" be displayed in the console as a result of its
execution?
4
1
3
do {
console.log(a--);
Which statement can replace the do ... while from the example above?
while (a > 2) console.log(--a);
while (a > 4) console.log(--a);
while (a > 3) console.log(a--);
while (a >= 3) console.log(a--);
16. Analyze the following code:
for (let x = 10; x > 1; x -= 2) console.log("hello");
How many times will "hello" be displayed in the console as a result of its
execution?
4
9
5
10
18. If we want to display all the elements of the days array in reverse order (starting
from the last element) then we can do this using the statement:
for(let i = days.length; i > 0; i--) console.log(days[i]);
for(let i = days.length - 1; i >= 0; i--)
console.log(days[i]);
for(let i = days.length - 1; i > 0; i--)
console.log(days[i]);
for(let i = days.length; i > 0; i--) console.log(i);
19. Examine the following code:
let steps = [3, 2, 1];
console.log(a);
Which statement can replace the for from the example above?
let a = 1; while (a < 5) console.log(a++);
let a = 5; while (a > 1) console.log(a++);
let a = 6; while (a >= 1) console.log(a--);
let a = 5; while (a > 1) console.log(a--);
console.log(test);
setTimeout(show, 2000);
let y = 20;
function test(y) {
console.log(y);
test(x);
return (a + b);
What could the definition of the corresponding arrow function look like?
let sum = function (a, b) => {
return (a + b);
}
;
a + b;
};
console.log("test");
if (counter > 0)
test(--counter);
test(3);
How many times will the word "test" be displayed in the console?
2
4
3
0
6. A callback function is a function that:
contains a reference to itself in its code.
let x = 10;
function test(x) {
console.log(x);
test(20);
"x"
10
20
8. We can use the forEach method to pass through the elements of an array. Which
of the following code snippets will display all consecutive elements of the animals
array in the console?
animals.forEach(a => {
console.log(a);
})
forEach(animals, a => {
console.log(a);
})
animals.forEach(console.log(animal));
animals.forEach(a => a);
We will try to write it in a slightly modified form, but without changing what it is
supposed to do. Point out the correct definition:
let multiply = (m, n) => {
m * n;
}
console.log(m * n);
}
return (m * n);
{
return (m * n);
}
is:
the declaration of the test variable in which the values returned by the completed
function will be stored.
the call of the test function.
incorrect, as the code is wrong and does not mean anything.
the declaration of an empty test function.
11. Analyze the following code:
let x = 10;
function test() {
let x = 20;
console.log(x);
Attempting to read a value from a variable that we have not previously declared.
TypeError
RangeError
SyntaxError
ReferenceError
3. Where can we find in the debugger the information about the currently called
functions in our program?
In the call stack window.
In the console.
We do not have access to such information.
The interpreter does not indicate errors while the program is running, because it detects
them before the program runs (it does not allow the program to run).
The interpreter ignores logical errors, because they do not affect the result of the
program in any way.
The interpreter is unable to identify logical errors because they are not related to
either the syntax or the semantics of the JavaScript language.
5. Using the debugger, we insert a breakpoint in the code at which, after running the
program, we stop. In the debugger, we find a Step button among the step-by-step
operation options. What does pressing it do?
Exactly one instruction immediately after the breakpoint will be executed and the
program will be paused again.
The program will be executed to the end, regardless of whether there are more
breakpoints in the rest of the code or not.
console.log(x);
SyntaxError
ReferenceError
TypeError
x = 20;
TypeError
RangeError
SyntaxError
8. We want to measure how long a certain piece of code executes. In order to do so, it
is enough to:
precede the fragment with the command console.time("start") and end
with console.time("end") .
precede the fragment with the command console.timeStart("counter") and
end with console.timeEnd("counter") .
precede the fragment with the command timeStart() and end with timeEnd() .
aprecede the fragment with the command console.time("counter") and
end with console.timeEnd("counter") .
9. What is the name of the place where program code execution is halted?
exitpoint
pausepoint
breakpoint
stoppoint
ocnsole.log("start");
} catch (error) {
console.log("error");
console.log("end");
ocnsole.log("start");
} catch (error) {
console.log("error");
} finally {
console.log("end");
onsole.log(x);
x += 10;
ReferenceError
TypeError
console.log("start");
} catch (error) {
console.log("error");
} finally {
console.log("end");
ocnsole.log(x);
TypeError
ReferenceError
SyntaxError
a(6);
cause the console to display the completion status of the statement preceding the
debugger.
put the interpreter into report mode, which will cause the console to print out all
sequentially executed commands.
pause the program with the ability to continue, as long as the execution
environment supports “debugging functionality”.
stop the program without the ability to continue, as long as the execution environment
supports “debugging functionality”.
3. We can replace the declaration let x = 3e-3; with:
let x = 0.003;
let x = 0.333;
let x = 0.0003;
let x = 3000;
4. 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:
x.reverse().push(y);
console.log(x.length);
try {
console.log(a.toUpperCase());
} catch (error) {
console.log(a)
} finally {
console.log(a);
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, b) {
}
return a * b;
}
return a * b;
}
}
There is an error in the code and it is not possible to declare such a function correctly.
let b = 0 || 20
title: "aga",
year: 2018
};
let b = 20 + +"10";
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) => b < a;
let cmp = (a, b) => b > a;
let cmp = (a, b) => a - b;
return (a + b);
What could the definition of the corresponding arrow function look like?
let sum = (a, b) => { a + b; };
=>
{
return (a + b);
}
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
21. The temp array contains air temperature data measured over a period of time. We
want to display the minimal temperature, and to do so we write the following code:
temp.forEach(e => min = min > e ? e : min);
console.log(min);
In the code, we use the variable sum , which should be previously declared as
follows:
let min = temp[0];
22. Which of the following loop instructions checks the loop continuation condition
only after the iteration has been completed?
while
do ... while
for ... in
for
console.log(execute(power, 3, 2));
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 = (x, y) => x ** y;
let power = (x,y) => x * y;
let power = 9;
What value will the test variable have if, after running the code, we immediately
press the OK
button on the newly created dialog?
"code"
"OK"
"Run"
true
let id = 200;
id = id + 1;
console.log(id);
1001
201
200
27. Entering about:blank in the address bar of your browser will:
generate a page with information about the browser's status.
generate and load a minimal blank HTML page into the current tab.
console.log(a);
};
let counter = 0;
let counter = 1;
let counter = 0;
Object, String
30. In the following code fragment, where we use setInterval , one line is missing –
the place is marked in gray:
let counter = 2;
console.log(counter);
}, 1000);
What should the missing line look like if the execution of this code results in the
console displaying the values 2 , 1 , and 0 in sequence?
if (counter-- <= 0) clearInterval(interval);
while (counter-- >= 0) clearInterval(interval);
clearInterval(interval);
if (counter-- >= 0) clearInterval(interval);