411 REFERENCE · Wikibooks
JavaScript/Self Test
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
You can test yourself by answering the following questions. When in doubt, you should undertake a test in your development environment (editor, IDE, browser). So it's also a test of whether you have installed all the necessary tools to execute the examples of the Wikibook.
__TOC__
Introduction
{Inside which HTML element do you put the JavaScript code?
|type="()"}
- <javascript>
- <source>
+ <script>
- <scripting>
- None of the above.
{Does this sequence of statements lead to an error?
let x = 0;
x = 1;
x = 'one';
|type="()"}
- Yes
|| A variable can hold values of different types (at different times).
+ No
|| A variable can hold values of different types (at different times).
Syntax and Semantic
{Is there a syntax error?
let x = 1:
|type="()"}
+ Yes
- No
{Is there a syntax error?
let x = 1;
let y = 2;
x+y = 3;
|type="()"}
+ Yes
- No
{Which line contains a syntax error?
/* 1 */ let x = 1;
/* 2 */ let y = 1;
/* 3 */ let x = 1, y = 1;
/* 4 */ let x, y = 1;
|type="()"}
- Line 1
- Line 2
- Line 3
- Line 4
+ None of the above
{Which lines contain a syntax error?
/* 1 */ let firstName_1 = "Michael";
/* 2 */ let firstName_2 = 'Michael';
/* 3 */ let firstName_4 = 'Mikhaïl';
/* 4 */ let firstName_5 = "Михаил";
|type="[]"}
- Line 1
- Line 2
- Line 3
|| It's perfectly correct when strings contain non-latin characters
- Line 4
|| It's perfectly correct when strings contain non-latin characters
+ None of the above
{Which line leads to an error message?
/* 1 */ let x = 1;
/* 2 */ const y = 2;
/* 3 */ x = 3;
/* 4 */ y = 4;
|type="()"}
- Line 1
- Line 2
- Line 3
+ Line 4
- None of the above
{Which line leads to an error message?
/* 1 */ let x = 1;
/* 2 */ x = 2 + 3(4 + 5);
/* 3 */ x = 3;
/* 4 */ x = -x;
|type="()"}
- Line 1
+ Line 2
|| In opposition to usual algebra in mathematics, you cannot omit the multiply operator.
- Line 3
- Line 4
|| It's correct: like multiplication with -1.
- None of the above
{Which line(s) leads to an error message?
/* 1 */ let x = [1, 2];
/* 2 */ let x = [[1], [2]];
/* 3 */ let x = [[1, 2], [3, 4]];
/* 4 */ let x = [[[1], [2]], [[3], [4]]];
/* 5 */ let x = [1], [2];
/* 6 */ let x = [1, '2'];
|type="[]"}
- Line 1
- Line 2
|| It assigns an array to x. This array contains the two arrays [1] and [2] (containing one 'Number' each).
- Line 3
- Line 4
+ Line 5
- Line 6
- None of the above
{Is there a syntax error?
"use strict";
let persons = ['Alice', 'Bert', 'Caesar'];
for (let i = 0, i < persons.length, i++) {
alert(persons[i]);
} //
|type="()"}
+ Yes
- No
{Which line contains a syntax error?
/* 1 */ alert("1 + 2 = " + 3);
/* 2 */ alert("1 + 2 = " + "3");
/* 3 */ alert("1 + 2 = " 3)
/* 4 */ alert(1 + 2 == 3)
|type="()"}
- Line 1
- Line 2
+ Line 3
- Line 4
|| The Numbers 1 and 2 are added. The result is compared with the Number 3. The comparison leads to a Boolean result.
- None of the above
Type Conversion
{Which message(s) will not be shown?
let x = 1, y = 2, z = '2';
alert(x + y);
alert(x + z);
alert(x - z);
|type="[]"}
- 3
- 12
- -1
|| Depending on the context, the character '+' is an operator to add Numbers or to concatenate strings (possibly after type conversion). The character '-' subtracts Numbers (possibly after type conversion, which is what happens here).
+ All will be shown.
|| In the last line, a type conversion takes place. Then, the subtraction is performed.
{What message(s) will not be shown?
let x = 1;
alert(x);
alert(x = 5);
alert(x == '5');
alert(x === 5);
|type="[]"}
+ Error Message
- true
- false
- 5
- 1
Loops
{What will be the result in sum?
"use strict";
let sum = 0;
for (let i = 1; i < 5; i++) {
sum = sum + i;
} //
alert(sum);
|type="{}"}
The result in sum will be { 10 _3 }.
|| Consider the cutting edges of the iteration: the first is 1, and the last is 4.
{What will be the result in sum?
"use strict";
let sum = 0;
for (let i = 0; i < 5; i++) {
for (let j = 10; j >= 0; j--) {
if (i === j) {
sum = sum + i - j + 1;
} //
} //
} //
alert(sum);
|type="{}"}
The result in sum will be { 5 _2 }.
|| The addition is only done when i is equal to j. Therefore i - j is always 0 and can be ignored.
{Is there a syntactical error?
"use strict";
let sum = 0;
for (let i = 0; i < 5; i++)
for (let j = 10; j >= 0; j--) {
if (i === j) {
sum = sum + i - j + 1;
} //
} //
} //
alert(sum);
|type="()"}
+ Yes
|| The first for statement misses the { character but uses the }. Missing both is possible as long as the block consists of exactly 1 statement.
- No
|| The first for statement misses the { character but uses the }. Missing both is possible as long as the block consists of exactly 1 statement.
{What will be the result?
"use strict";
let sum = 0;
for (let i = 0; i = 3; i++) {
sum = sum + i;
} //
alert(sum);
|type="()"}
- 0
- 6
+ Infinite loop
|| The comparison operator is === or ==. The i = 3 is an assignment. JavaScript executes the assignment and interprets its result as true.
- None of the above