JavaScript Essentials 1 – JSE1: Module 4 Test Answers
1. Which sequence of if ... else
statements is incorrect?
if ... else if ...
if ... else ... else if ...
if ... else ...
if ... else if ... else if...
2. Which of the following is not a loop instruction in JavaScript?
for ... of
for ... in
if ... else
do ... while
3. The condition if (!a)
can be replaced by the condition:
if (a == false);
if (a === false);
if (!!a);
if (a);
4. Review the following code:
let x = 100; if (x < 100) x = 20; console.log(x)
What will be displayed in the console as a result of its execution?
20
- nothing
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; }
Which notation is correct?
let name = (test)("Alice")("Bob");
let name = if test ? "Alice" : "Bob";
let name = test ? "Alice" : "Bob";
let name = test : "Alice" ? "Bob";
10. We store an array of animal names in the animals
variable (e.g. let animals = ["dog", "cat", "hamster", "rabbit"];
). Which of the following statements will display exactly two names from the array?
for (let n in animals) console.log(n);
for (let i =3 ; i < animals.length; i++) console.log(animals[i]);
for (let i = 0; i < animals.length; i+=2) console.log(animals[i]);
for (let n of animals) console.log(n);
11. Which of the following operators is a ternary one?
- An increment operator
++
. - A conditional operator
? :
. - A typeof operator.
- An assignment operator
=
.
12. Review the following code:
if (counter <= 10) { if (counter >= 10) { console.log(1); } }
We can replace it using:
if (counter == 10) console.log(1);
if (false) console.log(1);
if (counter >= 10) console.log(1);
if (true) console.log(1);
13. Examine the following code:
for (let a = 4; a < 4; a++) { console.log("test"); }
How many times will "test"
be displayed in the console as a result of its execution?
- 4
- 1
- It will not be displayed at all.
- 3
14. Examine the following code:
let car = {make: "Citroen", model: "DS"}; for (let f in car) console.log(f);
What will appear on the console as a result?
"make""model"
"car"
"make: Citroen" "model: DS"
"Citroen" "DS"
15. Analyze the following code:
let a = 10; do { console.log(a--); } while (a > 3);
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
17. The switch
statement:
- allows you to change the program mode to debug mode.
- is a conditional statement that allows different actions to be taken depending on the value stored in the indicated variable.
- is a conditional statement that works identically to the
if
statement. - is not present in the JavaScript language.
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]; for (let n of steps) console.log(n);
What will appear on the console as a result?
1 2 3
"[3, 2, 1]"
0 1 2
3 2 1
20. Examine the following code:
for (let a = 5; a > 1; a--) { 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--);