JavaScript Online Test - MCQ Questions

Welcome to the JavaScript Online Test! We will present 25 MCQs (Multiple-Choice Questions) to test your knowledge of JavaScript coding and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What will the following code output to the console?

console.log(typeof null);
a) 'null'
b) 'object'
c) 'undefined'
d) 'error'

2. What does the following JavaScript code return?

console.log("5" + 3);
a) '8'
b) '53'
c) 8
d) 'undefined'

3. How do you declare a JavaScript variable that should not change after its initial assignment?

a) var x = 10;
b) let x = 10;
c) const x = 10;
d) immutable x = 10;

4. Which method is used to remove the last element from an array and return that element in JavaScript?

a) shift()
b) pop()
c) removeLast()
d) getLast()

5. How can you check if an object is an array in JavaScript?

a) typeof [1, 2, 3]
b) [1, 2, 3].isArray()
c) Array.isArray([1, 2, 3])
d) isArray([1, 2, 3])

6. What does the map() method do in JavaScript?

let numbers = [1, 2, 3];
let squares = numbers.map(x => x * x);
console.log(squares);
a) Transforms each element in the array according to a transformation function
b) Maps the values of an array to a string
c) Creates a new array with each element being undefined
d) None of the above

7. How do you create a new promise in JavaScript?

a) new Promise()
b) Promise.create()
c) Promise.new()
d) new Promise(function(resolve, reject) {})

8. What is the output of the following code if the user clicks 'Cancel'?

alert(confirm("Proceed?"));
a) undefined
b) true
c) false
d) null

9. What will the following JavaScript code output?

console.log(0.1 + 0.2 === 0.3);
a) true
b) false
c) null
d) 'undefined'

10. What is a typical use case for an anonymous function in JavaScript?

setTimeout(function() {
    console.log("Hello!");
}, 1000);
a) To initialize a new scope
b) To create a delay in execution
c) To define a function that does not need to be reused
d) To declare variables

11. How does the forEach() method differ from the map() method?

const items = [1, 2, 3];
const doubled = items.map(item => item * 2);
items.forEach(item => console.log(item));
a) forEach() modifies the original array, while map() returns a new array
b) forEach() returns a new array, while map() affects the original array
c) forEach() only iterates over the array, while map() applies a function and returns a new array
d) There is no difference; they perform the same

12. What will the following JavaScript code output?

console.log(typeof (() => {}));
a) 'object'
b) 'function'
c) 'undefined'
d) 'string'

13. What is the purpose of the reduce() method in JavaScript?

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((total, number) => total + number, 0);
console.log(sum);
a) To reduce the array to a single value by applying a function
b) To filter out values in an array
c) To increment each element in the array
d) To multiply each element by its index

14. What does the following code snippet do?

const name = "Jane";
console.log(`Hello, ${name}!`);
a) Causes a syntax error
b) Prints: Hello, Jane!
c) Prints: Hello, ${name}!
d) Prints: Hello, name!

15. Which statement about JavaScript Promises is true?

const doTask = new Promise((resolve, reject) => {
    if (successful) {
        resolve('Success');
    } else {
        reject('Failure');
    }
});
a) Promises are a way to manage asynchronous operations in JavaScript
b) Promises run synchronously
c) Once a promise is rejected, it can be resolved later
d) Promises eliminate the need for callbacks

16. How do you create a class in JavaScript that inherits from another class?

class Animal {
    speak() {
        console.log('Makes a noise');
    }
}
class Dog extends Animal {
    bark() {
        console.log('Barks');
    }
}
a) class Dog implements Animal {}
b) class Dog inherits Animal {}
c) class Dog extends Animal {}
d) class Dog under Animal {}

17. What is the output of the following code?

let x = 1;
function changeX() {
    let x = 2;
}
changeX();
console.log(x);
a) 1
b) 2
c) undefined
d) ReferenceError

18. How do you ensure a block of code runs regardless of whether a Promise is resolved or rejected?

fetch('url').then(response => {
    console.log('Success');
}).catch(error => {
    console.error('Error');
}).finally(() => {
    console.log('Done');
});
a) Use the finally() method
b) Use the end() method
c) Use the always() method
d) Use the complete() method

19. What is Event Bubbling in JavaScript?

document.querySelector('#myDiv').addEventListener('click', function() {
    alert('Div Clicked!');
});
    document.querySelector('button').addEventListener('click', function() {
    alert('Button Clicked!');
});
a) A method to directly execute all event handlers on an element
b) An event propagation method in the DOM where events bubble up from the target element to the document
c) A process to cancel all ongoing events
d) An event-handling technique where events are captured and not allowed to bubble

20. What is Event Delegation in JavaScript?

document.querySelector('#myList').addEventListener('click', function(e) {
    if (e.target.tagName === 'LI') {
        console.log('List item clicked!');
    }
});
a) Attaching a single event listener to a parent element that fires for all descendants matching a selector
b) Preventing events from triggering on certain elements
c) Automatically delegating events to child elements without specific listeners
d) Creating custom events for specific actions

21. How do you find the largest number in an array of numbers using JavaScript?

const numbers = [2, 8, 5, 6, 10];
console.log(Math.max(...numbers));
a) Math.max(numbers)
b) Math.maximum(numbers)
c) Math.max(...numbers)
d) Math.largest(numbers)

22. What will the following code output?

console.log("2" + 3 + 5);
a) "235"
b) "10"
c) 10
d) "25"

23. How do you declare a function in JavaScript that does not hoist?

const myFunc = function() {
    console.log('Hello, world!');
};
a) function myFunc() { }
b) var myFunc = function() { }
c) const myFunc = function() { }
d) let myFunc = function() { }

24. Which method is used to fetch data asynchronously from a resource over the network?

fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
a) XMLHttpRequest()
b) axios.get()
c) fetch()
d) $.ajax()

25. What does the following code illustrate about JavaScript's execution?

console.log('1');
setTimeout(() => console.log('2'), 1000);
console.log('3');
a) Synchronous blocking
b) Asynchronous non-blocking
c) Sequential execution
d) Multi-threaded processing

Comments