Search Tutorials


JavaScript Quiz - MCQ - Multiple Choice Questions | JavaInUse

JavaScript Quiz - MCQ - Multiple Choice Questions

Q. What is the output of this code?

        var num = 10;
        if (num < 5) {
            console.log("Less than 5");
        } else if (num < 10) {
            console.log("Between 5 and 10");
        } else {
            console.log("Greater than 10");
        }
    
A. Less than 5
B. Between 5 and 10
C. Greater than 10
D. The code has an error

Q. What will be the output of this code?

        var x = 5;
        var y = "5";
        console.log(x == y);
    
A. true
B. false
C. Syntax error
D. undefined

Q. What is the output of this code?

        var myArray = ["apple", "banana", "cherry"];
        myArray.splice(1, 0, "date", "elderberry");
        console.log(myArray);
   
A. ["apple", "banana", "date", "elderberry", "cherry"]
B. ["apple", "date", "elderberry", "banana", "cherry"]
C. ["apple", "date", "banana", "elderberry", "cherry"]
D. ["apple", "banana", "cherry", "date", "elderberry"]

Q. What is the output of this code?

        function greet(name) {
            return "Hello, " + name;
        }
        var greeting = greet("Alice");
        console.log(greeting);
    
A. Hello, Alice
B. undefined
C. Syntax error
D. Function declaration

Q. What is the output of this code?

        var x = 10;
        var y = "10";
        console.log(x === y);
    
A. true
B. false
C. Syntax error
D. undefined

Q. What is the output of this code?

        var myObj = {
            name: "John",
            age: 30,
            city: "New York"
        };
        console.log(myObj.name);
    
A. John
B. 30
C. New York
D. undefined

Q. What is the output of this code?

        var num = 10;
        function changeNum() {
            num += 5;
            return num;
        }
        console.log(changeNum());
        console.log(num);
    
A. 15, 15
B. 15, 10
C. 10, 15
D. Syntax error

Q. What is the output of this code?

        var myString = "Hello, world!";
        console.log(myString.slice(2, 5));
    
A. ll
B. lo
C. o, w
D. Error: Invalid arguments





Q. What is the purpose of the forEach method in JavaScript arrays?

A. To filter elements based on a condition
B. To map each element to a new value
C. To execute a provided function once for each element in the array
D. To reduce the array to a single value

Q. What is the output of this code?

        var myArray = [1, 2, 3, 4, 5];
        myArray.forEach(function(element) {
            console.log(element * 2);
        });
    
A. 2 4 6 8 10
B. 1 2 3 4 5
C. 1 4 9 16 25
D. undefined

Q. What is the output of this code?

        var myPromise = new Promise(function(resolve, reject) {
            setTimeout(resolve, 1000, "Success!");
        });
        myPromise.then(function(result) {
            console.log(result);
        });
    
A. Success!
B. undefined
C. Syntax error
D. The code throws an error

Q. What is the purpose of the map method in JavaScript arrays?

A. To filter elements based on a condition
B. To create a new array with the results of calling a provided function on every element in the array
C. To reduce the array to a single value
D. To sort the elements of the array

Q. What is the output of this code?

        var myArray = [1, 2, 3, 4, 5];
        var doubledArray = myArray.map(function(num) {
            return num * 2;
        });
        console.log(doubledArray);
    
A. [2, 4, 6, 8, 10]
B. [1, 2, 3, 4, 5]
C. [1, 4, 9, 16, 25]
D. undefined

Q. What is the purpose of the async/await syntax in JavaScript?

A. To define asynchronous functions that can pause and resume execution
B. To create synchronous functions that block the execution flow
C. To handle errors in asynchronous code
D. To optimize the performance of asynchronous operations

Q. What is the output of the following JavaScript code?

let person = { name: "Alice", age: 25 };
    console.log(person.name.toUpperCase());
A. ALICE
B. alice
C. Error: person is undefined
D. None of the above

Q. What is the output of the following JavaScript code?

let x = 10;
    let y = x++;
    console.log(x);
    console.log(y);
A. 10, 10
B. 11, 10
C. 11, 11
D. Error: invalid syntax

Q. What is the output of the following JavaScript code?

let fruits = ["Apple", "Banana", "Orange"];
    fruits.push("Mango");
    console.log(fruits.length);
A. 3
B. 4
C. "Apple, Banana, Orange, Mango"
D. None of the above

Q. What is the output of the following JavaScript code?

let num = 5;
    if (num < 10) {
        console.log("Less than 10");
    } else {
        console.log("Greater than or equal to 10");
    }
A. Less than 10
B. Greater than or equal to 10
C. Syntax error
D. None of the above

Q. What is the output of the following JavaScript code?

let message = "Hello, " + "World!";
    console.log(message);
A. Hello, World!
B. Hello
C. World!
D. Syntax error

Q. What is the purpose of the "use strict" directive in JavaScript?

A. To enable strict mode and enforce stricter parsing and error handling
B. To disable strict mode and allow more flexible code
C. To define a variable as constant and prevent reassignment
D. To declare a function that can only be called once

Q. What is the output of the following JavaScript code?

let arr = [1, 2, 3, 4, 5];
    let sum = arr.reduce((acc, currentValue) => acc + currentValue, 0);
    console.log(sum);
A. 15
B. [1, 2, 3, 4, 5]
C. 0
D. None of the above

Q. What is the output of the following JavaScript code?

let myFunc = function() {
    console.log("Hello from myFunc");
};

myFunc();
A. Hello from myFunc
B. undefined
C. Syntax error
D. None of the above

Q. What is the output of the following JavaScript code?

let myString = "JavaScript";
    console.log(myString.slice(4, 7));
A. Java
B. Script
C. avaSc
D. None of the above

Q. What is the output of the following JavaScript code?

let myArray = [1, 2, 3];
    myArray.forEach(function(element, index) {
        console.log(element, index);
    });
A. 1 0, 2 1, 3 2
B. 1 0, 2 1
C. 1, 2, 3
D. None of the above

Q. What is the output of the following JavaScript code?

let myPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve("Promise resolved");
    }, 1000);
});

myPromise.then(result => console.log(result));
A. Promise resolved
B. Promise rejected
C. undefined
D. None of the above

Q. What is the purpose of the "this" keyword in JavaScript?

A. To refer to the current object or context
B. To access global variables
C. To create a new object
D. To access the parent function's scope

Q. What is the output of the following JavaScript code?

let mySet = new Set([1, 2, 2, 3, 4]);
    console.log(mySet);
A. Set(1, 2, 2, 3, 4)
B. Set(1, 2, 3, 4)
C. [1, 2, 2, 3, 4]
D. None of the above

Q. What is the output of the following JavaScript code?

let myRegex = /hello/i;
    let myString = "Hello, World!";
    let match = myString.match(myRegex);
    console.log(match);
A. ["hello"]
B. ["Hello"]
C. null
D. None of the above