LearnFrenzy
Sign in Take a free test

Arrays · Exercise 2 of 4

Arrays Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each JavaScript program and choose what it prints. Every program was run on Node.js as a normal (non-strict) script unless it says "use strict", so the answers are the real output.

Question 11 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log([1].concat(2, [3, [4]]));
A [ 1, 2, 3, 4 ]
B [ 1, 2, [ 3, [ 4 ] ] ]
C [ 1, 2, 3, [ 4 ] ]
D [ [ 1 ], 2, 3, 4 ]
Question page & discussion
Question 12 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log([1, null, undefined, 2].join("-"));
console.log(String([[1, 2], [3]]));
A 1-null-undefined-2 1,2,3
B 1--2 [1,2],[3]
C 1-0-NaN-2 1,2,3
D 1---2 1,2,3
Question page & discussion
Question 13 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const a = [1, 2, 3];
const r = a.reverse();
r.push(0);
console.log(a);
A [ 3, 2, 1, 0 ]
B [ 1, 2, 3 ]
C [ 3, 2, 1 ]
D [ 1, 2, 3, 0 ]
Question page & discussion
Question 14 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const grid = new Array(2).fill([]);
grid[0].push(1);
console.log(grid);
A [ [ 1 ], [] ]
B [ [ 1 ], [ 1 ] ]
C [ 1, [] ]
D [ [], [] ]
Question page & discussion
Question 15 Easy

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log(Array.from("abc"), Array.from(new Set([1, 1, 2])));
A [ 'a', 'b', 'c' ] [ 1, 2 ]
B [ 'abc' ] [ 1, 1, 2 ]
C [ 'a', 'b', 'c' ] [ 1, 1, 2 ]
D TypeError
Question page & discussion
Question 16 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log(Array(3).length, Array.of(3).length, Array(3, 4));
A 1 1 [ 3, 4 ]
B 3 1 [ 3, 4 ]
C 3 3 [ <3 empty items>, 4 ]
D 3 1 [ <3 empty items> ]
Question page & discussion
Question 17 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const a = [[1], [2]];
const b = [...a];
b[0].push(9);
b[1] = [0];
console.log(a);
A [ [ 1 ], [ 2 ] ]
B [ [ 1, 9 ], [ 0 ] ]
C [ [ 1, 9 ], [ 2 ] ]
D [ [ 1 ], [ 0 ] ]
Question page & discussion
Question 18 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
console.log([1, 2] == "1,2", [1, 2] === [1, 2], [] + 1, [5] * 2);
A false false 1 10
B true false 1 10
C true true 1 NaN
D false false NaN NaN
Question page & discussion
Question 19 Medium

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const [first, , third = "def", ...rest] = [1, 2, undefined, 4, 5];
console.log(first, third, rest);
A 1 undefined [ 4, 5 ]
B 1 2 [ undefined, 4, 5 ]
C 1 def [ 4, 5 ]
D 1 def [ 2, 4, 5 ]
Question page & discussion
Question 20 Hard

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const a = [1, 2, 1, 2];
console.log(a.indexOf(1, 1), a.lastIndexOf(2, 2), a.includes(2, 4));
A 0 3 true
B 2 1 false
C 2 3 false
D 1 1 true
Question page & discussion
Up next · Exercise 3Find the Error15 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links