wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JAVASCRIPT LONG

Total questions: 55

Worksheet time: 28mins

Name
Class
Date
1.

Analyze the code snippet:

let n = 2 3 * 3 - 1;

a)

53

b)

215

c)

18

d)

36

2.

The number 2 is stored in the n variable: let n = 2;.

The command n = n*n*n is then called.

This last command can be replaced by:

a)

n **= 3;

b)

n ***= n;

c)

n **= n;

d)

n *= 3;

3.

The string "12" has been written into the str variable: let str = "12";.

Then, the following operation is performed: str = +str.

As a result, the variable str will contain:

a)

12

b)

"+12"

c)

"12"

d)

NaN

4.

Analyze the code snippet:

let n = 10;

let m = ++n;

a)

n: 10, m: 10

b)

n: 11, m: 10

c)

n: 10, m: 11

d)

n: 11, m: 11

5.

The result of the operation !(true && false || true) will be:

a)

null

b)

true

c)

false

d)

1

6.

The result of the comparison "abcd" > "Abcd" will be:

a)

false

b)

"abcd"

c)

1

d)

true

7.

The result of the operation 3 * 4 > 20 – 15 will be:

a)

-14

b)

NaN

c)

true

d)

false

8.

The confirm method allows you to create a dialog box. What value does this method return when the user closes the window?

a)

The string entered by the user.

b)

It depends on the option selected by the user.

c)

Always true.

d)

Always false.

9.

The methods

window.alert,

window.confirm, and

window.prompt

are methods of the window object.

Which of the following is not true?

a)

The window object represents the window in which the HTML document containing the JavaScript code currently being executed is open.

b)

The alert, confirm, and prompt methods require an argument specifying the position of the dialog box in which the information will be displayed.

c)

The alert, confirm, and prompt methods display information in modal windows that block access to the page until they are closed.

d)

You can call window methods, such as window.alert, without including the name window, so calling alert("abc") would be correct.

10.

Analyze the following code:

let test = prompt("Hello", "World");

What value will the test variable have if, after running the code, we immediately press the OK button on the newly created dialog?

a)

true


b)

"Hello"

c)

"World"

d)

"OK"

11.

The condition if( a >= 0 ) can be replaced by the condition:

a)

if (!(a > 0 && a == 0));

b)

if (a > 0 && a == 0);

c)

if (0 < a);

d)

if (a > 0 || a == 0);

12.

The condition if (!a) can be replaced by the condition:

a)

if (!!a);

b)

if (a == false);

c)

if (a === false);

d)

if (a);

13.

We want to rewrite the following code snippet using the conditional operator:

let name;

if (test) {

name = "Alice";

} else {

name = "Bob";

}

Which notation is correct?

a)

let name = test ? "Alice" : "Bob";

b)

let name = test : "Alice" ? "Bob";

c)

let name = (test)("Alice")("Bob");

d)

let name = if test ? "Alice" : "Bob";

14.

Which of the following operators is a ternary one?

a)

A typeof operator.

b)

An increment operator ++.

c)

An assignment operator =.

d)

A conditional operator ? :.

15.

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?

a)

100

b)

nothing

c)

false

d)

20

16.

Review the following code:

if (counter <= 10) {

if (counter >= 10) {

console.log(1);

}

}

We can replace it using:

a)

if (counter >= 10) console.log(1);

b)

if (counter == 10) console.log(1);

c)

if (true) console.log(1);

d)

if (false) console.log(1);

17.

Review the following code snippet:

if (counter <= 10 && show === false) {

console.log("test");

}

What values can the counter and show variables have so that the console displays "test" as a result of code execution?

a)

counter: 10, show: false

b)

counter: 10, show: true

c)

counter: 11, show: false

d)

counter: 9, show: true

18.

The switch statement:

a)

is a conditional statement that works identically to the if statement.

b)

is not present in the JavaScript language.

c)

allows you to change the program mode to debug mode.

d)

is a conditional statement that allows different actions to be taken depending on the value stored in the indicated variable.

19.

let a = 10;

do {

console.log(a--);

} while (a > 3);

Which statement can replace the do ... while from the example above?

a)

while (a >= 3)
     console.log(a--);

b)

while (a > 3)
     console.log(a--);

c)

while (a > 4)
     console.log(--a);

d)

while (a > 2)
     console.log(--a);

20.

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?

a)

4

b)

10

c)

9

d)

5

21.

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?

a)

for (let i = 0; i < animals.length; i+=2)
     console.log(animals[i]);

b)

for (let i =3 ; i < animals.length; i++)
     console.log(animals[i]);

c)

for (let n in animals)
     console.log(n);

d)

for (let n of animals)
     console.log(n);

22.

for (let a = 5; a > 1; a--) {

    console.log(a);

}

;

Which statement can replace the for from the example above?

a)

let a = 6; while (a >= 1)
     console.log(a--);

b)

let a = 5; while (a > 1)
     console.log(a--);

c)

let a = 1; while (a < 5)
     console.log(a++);

d)

let a = 5; while (a > 1)
     console.log(a++);

23.

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?

a)

1

b)

3

c)

It will not be displayed at all.

d)

4

24.

let car = {make: "Citroen", model: "DS"};

for (let f in car)

console.log(f);

What will appear on the console as a result?

a)

"make: Citroen" "model: DS"

b)

"car"

c)

"Citroen" "DS"

d)

"make""model"

25.

let steps = [3, 2, 1];

for (let n of steps)

console.log(n);

What will appear on the console as a result?

a)

1 2 3

b)

"[3, 2, 1]"

c)

0 1 2

d)

3 2 1

26.

function test() { }

is:

a)

the call of the test function.

b)

incorrect, as the code is wrong and does not mean anything.

c)

the declaration of the test variable in which the values returned by the completed function will be stored.

d)

the declaration of an empty test function.

27.

If a function is to return some calculated value on completion, which keyword will you use to do this?

a)

return;

b)

function;

c)

yield;

d)

ret;

28.

function test(counter) {

console.log("test");

if (counter > 0)

test(--counter);

}

test(3);

How many times will the word "test" be displayed in the console?

a)

3

b)

4

c)

2

d)

0

29.

let x = 10;

function test() {

let x = 20;

console.log(x);

}

What will be displayed in the console as a result of its execution?

a)

20

b)

X

c)

Nothing will show up.

d)

10

30.

let x = 10;

function test(x) {

console.log(x);

}

test(20);

What will be displayed in the console as a result of its execution?

a)

10

b)

X

c)

Nothing will show up.

d)

20

31.

let multiply = (m, n) => m * n;

How would you rewrite the function without changing what it does? Select the correct definition.

a)

let multiply = (m, n) => {
return (m * n);
}

b)

let multiply = (m, n) => {
console.log(m * n);
}

c)

let multiply = (m, n) =>
return (m * n);

d)

let multiply = (m, n) => {
m * n;
}

32.

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?

a)

animals.forEach(a => a);

b)

animals.forEach(console.log(animal));

c)

forEach(animals, a => {
console.log(a);
})

d)

animals.forEach(a => {
console.log(a);
})

33.

A callback function is a function that:

a)

is always called with a certain pre-defined delay.

b)

contains a reference to itself in its code.

c)

is always executed at fixed intervals.

d)

is passed to another function as an argument and only called in its code.

34.

let show = function () {

console.log("test");

}

setTimeout(show, 2000);

What happens when you execute it?

a)

The console to display test after a 2000-second delay.

b)

The console to display test 2000 times.

c)

The console to display test after a 2-second delay.

d)

The console to display show after a 2-second delay.

35.

Which of the following is a syntax error?

a)

Attempting to call a non-existent function.

b)

Attempting to read a value from a variable that we have not previously declared.

c)

Missing parenthesis ending a condition in an if statement.

d)

Attempting to modify the value of a constant.

36.

Logical errors that we make while writing a program are not indicated by the interpreter. Why?

a)

It results from the default settings of the interpreter, although we can modify these settings so that logical errors are also pointed out.

b)

The interpreter ignores logical errors, because they do not affect the result of the program in any way.

c)

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

d)

The interpreter is unable to identify logical errors because they are not related to either the syntax or the semantics of the JavaScript language.

37.

"let x = 10";

console.log(x);

What exception will be thrown as a result of its execution attempt?

a)

RangeError

b)

TypeError

c)

SyntaxError

d)

ReferenceError

38.

let x 10;

console.log(x);

What exception will be thrown as a result of its execution attempt?

a)

RangeError

b)

TypeError

c)

SyntaxError

d)

ReferenceError

39.

let x = 10;

ocnsole.log(x);

What exception will be thrown as a result of its execution attempt?

a)

ReferenceError

b)

RangeError

c)

TypeError

d)

SyntaxError

40.

try {

console.log("start");

} catch (error) {

console.log("error");

} finally {

console.log("end");

}

What will happen as a result of its execution?

a)

The word error will appear in the console.

b)

The following words will appear in the console: start, end.

c)

The following words will appear in the console: start, error, end.

d)

The following words will appear in the console: error, end.

41.

What is the name of the place where program code execution is halted?

a)

exitpoint

b)

pausepoint

c)

breakpoint

d)

stoppoint

42.

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?

a)

The program will execute to the end or to the next breakpoint.

b)

Exactly one instruction immediately after the breakpoint will be executed and the program will be paused again.

c)

The program will be restarted from the first instruction.

d)

The program will be executed to the end, regardless of whether there are more breakpoints in the rest of the code or not.

43.

Where in the debugger can you find the information about the currently called functions in your program?

a)

In the watch window.

b)

In the console.

c)

There is no access to such information.

d)

In the call stack window.

44.

You want to measure how long a certain piece of code executes. In order to do so, it is enough to precede the code fragment with:

a)

the command timeStart() and end with timeEnd().

b)

the command console.time("counter") and end with console.timeEnd("counter").

c)

the command console.timeStart("counter") and end with console.timeEnd("counter").

d)

the command console.time("start") and end with console.time("end").

45.

let x = false || true;

let y = "true" && "false";

let z = false && true;

console.log(`${x} ${y} ${z}`);

What will appear in the console as a result of its execution?

a)

false true true

b)

false false false

c)

false false true

d)

true false false

46.

let a = true && 20;

let b = 0 || 20

let c = 0 && 20;

console.log(`${a} ${b} ${c}`);

What will appear in the console as a result of its execution?

a)

true true false

b)

true 20 0

c)

20 20 0

d)

1 1 0

47.

let a = 20 + "10";

let b = 20 + +"10";

let c = 20 + -"10" + "10";

let d = "10" - "10" + "100";

let e = "A" - "B" + 0xA;

console.log(`${a}, ${b}, ${c}, ${d}, ${e}`);

What will appear on the console as a result?

a)

30, 31, 39, 100, NaN

b)

2010, 2010, 20-1010, 0100, NaN

c)

2010, 30, 1010, 0100, NaN

d)

30, 30, 20, 100, 2

48.

let route = {distance: 131, elevation: 1.4};

for (let k in route) console.log(k);

What will appear on the console as a result?

a)

distance

b)

2

c)

131
1.4

d)

distance
elevation

49.

const a = "hello";

console.log(a.toUpperCase());

}

} catch (error) {

console.log(a)

} finally {

console.log(a);

}

What will happen as a result of its execution?

a)

The following words will appear in the console on subsequent lines: hello and hello.

b)

The following word will appear in the console: HELLO.

c)

The following words will appear in the console on subsequent lines: HELLO and hello.

d)

The following words will appear in the console on subsequent lines: HELLO, hello, and hello.

50.

Placing a debugger; statement in the program code will:

a)

pause the program with the ability to continue, as long as the execution environment supports "debugging functionality".

b)

cause the console to display the completion status of the statement preceding the debugger.

c)

stop the program without the ability to continue, as long as the execution environment supports "debugging functionality".

d)

put the interpreter into report mode, which will cause the console to print out all sequentially executed commands.

51.

let counter = 2;

let interval = setInterval(() => {

    console.log(counter);

// Insert missing line here.

}, 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?

a)

clearInterval(interval);

b)

if (counter-- >= 0) clearInterval(interval);

c)

while (counter-- >= 0) clearInterval(interval);

d)

if (counter-- <= 0) clearInterval(interval);

52.

let a = (n) => {

return n > 2 ? n * a(n - 1) : 2

}

console.log(a(6));

What will appear on the console as a result?

a)

120

b)

720

c)

4

d)

6

53.

let x = [10, 20, 30, 40];

let y = [50, 60];

x.reverse().push(y);

console.log(x.length);

What will appear on the console as a result?

a)

6

b)

4

c)

2

d)

5

54.

for(let a=1; a<10; a+=2) {

console.log(a);

};

Which statement can replace the for from the example?

a)

let counter = 1;
while (counter++ < 10) console.log(counter++);

b)

let counter = 0;
while (counter < 9) console.log(counter++);

c)

let counter = 0;
while (counter++ < 10) console.log(counter++);

d)

let counter = 0;
while (counter < 10) console.log(counter++);

55.

let counter = 0;

let userName = "John";

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:

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