MCQs Of Day 14: JavaScript ES6 Features

Rashmi Mishra
0

  MCQs  Of Day 14: JavaScript ES6 Features

Arrow Functions

1.  What is the main advantage of using arrow functions in JavaScript?

o    a) They are faster to execute.

o    b) They automatically bind this.

o    c) They require fewer lines of code.

o    d) They are compatible with older browsers.

Answer: b) They automatically bind this.
Explanation: Arrow functions lexically bind this, meaning this inside an arrow function refers to the this of the surrounding code, unlike regular functions which have their own this.

2.  Which of the following is the correct syntax for an arrow function that accepts two parameters?

o    a) const add = (a, b) => a + b;

o    b) const add = a, b => a + b;

o    c) const add = (a, b) -> a + b;

o    d) const add = a, b -> return a + b;

Answer: a) const add = (a, b) => a + b;
Explanation: This is the correct syntax for an arrow function with parameters.

3.  What will the following code output?

javascript

Copy code

const square = x => x * x;

console.log(square(4));

o    a) 16

o    b) 8

o    c) undefined

o    d) null

Answer: a) 16
Explanation: The arrow function correctly squares the number 4 and outputs 16.

4.  Which of the following does NOT require the return keyword in an arrow function?

o    a) Arrow function with one line.

o    b) Arrow function with multiple lines.

o    c) Anonymous function.

o    d) Regular function.

Answer: a) Arrow function with one line.
Explanation: If an arrow function consists of a single expression, the return keyword is implicitly added.

5.  What will happen if you use an arrow function inside an object method?

o    a) It will bind the object's this.

o    b) It will not bind the object's this.

o    c) It will throw an error.

o    d) The method will be undefined.

Answer: b) It will not bind the object's this.
Explanation: Arrow functions do not have their own this; they inherit this from the surrounding context. Inside an object method, this can lead to unexpected results.


Template Literals

6.  Which of the following is a valid example of a template literal?

o    a) const message = 'Hello, world!';

o    b) const message = "Hello, world!";

o    c) const message = \Hello, world!`;`

o    d) const message = 'Hello, ${world}!';

**Answer: c) const message = \Hello, world!`;** **Explanation:** Template literals are enclosed in backticks () and can embed expressions inside ${}.

7.  What does the following code output?

javascript

Copy code

const name = 'John';

const message = `Hello, ${name}!`;

console.log(message);

o    a) Hello, name!

o    b) Hello, ${name}!

o    c) Hello, John!

o    d) undefined

Answer: c) Hello, John!
Explanation: The ${name} is replaced with the value of the name variable, resulting in Hello, John!.

8.  Which of the following is true about template literals?

o    a) They only allow concatenation.

o    b) They can span multiple lines.

o    c) They are only used for strings.

o    d) They do not allow interpolation.

Answer: b) They can span multiple lines.
Explanation: Template literals can span multiple lines without needing escape characters, unlike regular strings.

9.  Which of the following is correct for inserting expressions inside template literals?

o    a) const value = "world"; console.log("Hello, " + value + "!");

o    b) const value = "world"; console.log("Hello, ${value}!");

o    c) const value = "world"; console.log("Hello, value!");

o    d) const value = "world"; console.log("Hello, <value>!");

Answer: b) const value = "world"; console.log("Hello, ${value}!");
Explanation: Template literals use ${} to insert variables or expressions.

10.        What will the following code output?

javascript

Copy code

const num = 5;

const result = `The number is: ${num}`;

console.log(result);

o    a) The number is: ${num}

o    b) undefined

o    c) The number is: num

o    d) The number is: 5

Answer: d) The number is: 5
Explanation: ${num} is replaced by the value of the num variable.


Destructuring

11.        What will the following code output?

javascript

Copy code

const person = { name: 'Alice', age: 25 };

const { name, age } = person;

console.log(name, age);

o    a) undefined undefined

o    b) Alice 25

o    c) name age

o    d) Alice undefined

Answer: b) Alice 25
Explanation: The values of name and age are extracted from the person object using destructuring.

12.        Which of the following is the correct syntax for destructuring an array?

o    a) const [a, b] = [1, 2];

o    b) const a, b = [1, 2];

o    c) const {a, b} = [1, 2];

o    d) const a = [1, b = 2];

Answer: a) const [a, b] = [1, 2];
Explanation: The correct syntax for array destructuring uses square brackets ([]) to assign values to variables.

13.        What will the following code output?

javascript

Copy code

const person = { name: 'Alice', age: 25, city: 'New York' };

const { name, city } = person;

console.log(name, city);

o    a) Alice New York

o    b) Alice 25

o    c) New York 25

o    d) Alice undefined

Answer: a) Alice New York
Explanation: Destructuring extracts the name and city properties from the object.

14.        How can you provide default values during destructuring?

o    a) const { name = 'Default' } = person;

o    b) const { name: 'Default' } = person;

o    c) const { name => 'Default' } = person;

o    d) const { name = 'Default'; } = person;

Answer: a) const { name = 'Default' } = person;
Explanation: Default values can be assigned by using the = operator during destructuring.

15.        What does the following code output?

javascript

Copy code

const person = { name: 'Alice', age: 30 };

const { name, occupation = 'Engineer' } = person;

console.log(occupation);

o    a) undefined

o    b) Engineer

o    c) Alice

o    d) 30

Answer: b) Engineer
Explanation: The occupation property is not defined in the person object, so the default value 'Engineer' is used.


let and const

16.        Which of the following is true about variables declared with let?

o    a) They have block-level scope.

o    b) They are function-scoped.

o    c) They are globally scoped.

o    d) They are immutable.

Answer: a) They have block-level scope.
Explanation: Variables declared with let are scoped to the block in which they are defined.

17.        Which keyword should be used when declaring a variable whose value will not be reassigned?

o    a) var

o    b) let

o    c) const

o    d) immutable

Answer: c) const
Explanation: const is used to declare variables whose values cannot be reassigned.

18.        What happens if you try to reassign a value to a const variable?

o    a) It throws a TypeError.

o    b) It throws a SyntaxError.

o    c) It is ignored.

o    d) It reassigns successfully.

Answer: b) It throws a SyntaxError.
Explanation: Reassigning a value to a const variable results in a SyntaxError.

19.        Which of the following is correct regarding block-scoped variables (let and const)?

o    a) They are hoisted to the top of the block.

o    b) They are hoisted to the top of the function.

o    c) They are not hoisted.

o    d) They are hoisted to the global scope.

Answer: a) They are hoisted to the top of the block.
Explanation: let and const are hoisted but not initialized. Accessing them before declaration results in a ReferenceError.

20.        Which of the following will throw an error?

javascript

Copy code

let x = 10;

const x = 20;

o    a) No error will occur.

o    b) ReferenceError will occur.

o    c) TypeError will occur.

o    d) SyntaxError will occur.

Answer: b) ReferenceError will occur.
Explanation: You cannot redeclare a variable using let or const in the same scope.


Let me know if you'd like me to continue this pattern for more questions.

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs to deepen your understanding of JavaScript ES6 features:


21.        Which of the following is the correct syntax for defining an arrow function that takes two parameters and returns their sum?

o    a) const add = (a, b) => return a + b;

o    b) const add = (a, b) -> a + b;

o    c) const add = (a, b) => a + b;

o    d) const add = (a, b) => { a + b; }

Answer: c) const add = (a, b) => a + b;
Explanation: Arrow functions have a concise syntax where if there is only one expression, the result is returned implicitly.


22.        What is the benefit of using const over let in JavaScript?

o    a) const allows reassignment.

o    b) const allows redeclaration in the same scope.

o    c) const prevents reassignment of variables.

o    d) const creates global variables.

Answer: c) const prevents reassignment of variables.
Explanation: const ensures that a variable cannot be reassigned after initialization, while let allows reassignment.


23.        What will be the output of the following code?

javascript

Copy code

let x = 10;

if (true) {

    let x = 20;

    console.log(x);

}

console.log(x);

o    a) 10 10

o    b) 20 20

o    c) 20 10

o    d) 10 20

Answer: c) 20 10
Explanation: The second let x = 20 is block-scoped, so it only exists within the if block, and the outer x is unaffected.


24.        Which of the following best describes template literals?

o    a) A way to escape strings.

o    b) A way to create dynamic string values using ${}.

o    c) A syntax for multiline strings.

o    d) Both b and c.

Answer: d) Both b and c.
Explanation: Template literals support dynamic expressions (using ${}) and can also be multiline without the need for escape characters.


25.        Which of the following will correctly define a function using ES6 arrow function syntax?

o    a) const sum = (a, b) -> { return a + b; };

o    b) const sum = a, b => { return a + b; };

o    c) const sum = (a, b) => a + b;

o    d) const sum = function(a, b) { return a + b; };

Answer: c) const sum = (a, b) => a + b;
Explanation: This is the correct ES6 arrow function syntax for returning the sum of two parameters.


26.        Which of the following will cause an error when using destructuring?

javascript

Copy code

const person = { name: "John", age: 30 };

const { name, age, gender } = person;

o    a) No error.

o    b) ReferenceError because gender does not exist.

o    c) TypeError because person is an object.

o    d) SyntaxError because of incorrect destructuring syntax.

Answer: a) No error.
Explanation: Destructuring allows for extracting properties that may not exist. If the property doesn't exist, undefined is assigned to it.


27.        Which of the following is the correct way to use the spread operator to clone an object in ES6?

o    a) const cloned = {...original};

o    b) const cloned = original.copy();

o    c) const cloned = Object.clone(original);

o    d) const cloned = original.clone();

Answer: a) const cloned = {...original};
Explanation: The spread operator (...) is used to clone objects and arrays by copying their properties or elements.


28.        Which of the following is the correct way to define a function with default parameters?

o    a) function greet(name = "Guest") { console.log(name); }

o    b) function greet(name) { name = "Guest"; console.log(name); }

o    c) function greet(name = Guest) { console.log(name); }

o    d) function greet(name => "Guest") { console.log(name); }

Answer: a) function greet(name = "Guest") { console.log(name); }
Explanation: Default parameters are defined by assigning default values directly in the function signature.


29.        What is the result of the following code using arrow functions?

javascript

Copy code

const multiply = (a, b) => a * b;

console.log(multiply(2, 3));

o    a) 5

o    b) 6

o    c) undefined

o    d) Error

Answer: b) 6
Explanation: The arrow function correctly multiplies 2 and 3, resulting in 6.


30.        Which of the following statements is correct regarding the let keyword in JavaScript?

o    a) Variables declared with let are hoisted but initialized with undefined.

o    b) Variables declared with let are not hoisted.

o    c) Variables declared with let are global.

o    d) let can be used to declare variables without initialization.

Answer: a) Variables declared with let are hoisted but initialized with undefined.
Explanation: let is hoisted to the top of its block but is not initialized until the execution reaches the declaration.


31.        Which of the following is true about const variables?

o    a) const variables must be assigned a value at the time of declaration.

o    b) const variables can be reassigned later.

o    c) const variables are function-scoped.

o    d) const variables are mutable.

Answer: a) const variables must be assigned a value at the time of declaration.
Explanation: const ensures that a variable cannot be reassigned or left uninitialized.


32.        Which of the following is the correct way to create a template literal with an embedded expression?

o    a) "The result is: " + result

o    b) 'The result is: ${result}'

o    c) The result is: result

o    d) The result is: \{result\}

Answer: b) 'The result is: ${result}'
Explanation: Template literals use ${} to embed expressions within the string.


33.        What does the following code do?

javascript

Copy code

let [a, b] = [1, 2];

o    a) It assigns 1 to a and 2 to b.

o    b) It creates a tuple with 1 and 2.

o    c) It swaps the values of a and b.

o    d) It throws an error.

Answer: a) It assigns 1 to a and 2 to b.
Explanation: This is an example of array destructuring where values are assigned from the array to variables.


34.        What is the purpose of the let keyword in JavaScript?

o    a) It creates a global variable.

o    b) It allows for block-scoped variables.

o    c) It creates a constant variable.

o    d) It creates a variable with function scope.

Answer: b) It allows for block-scoped variables.
Explanation: let is used to declare variables that are scoped to the block in which they are defined.


35.        What does the following code output?

javascript

Copy code

const obj = { name: "John", age: 30 };

const { name, age } = obj;

console.log(name, age);

o    a) undefined undefined

o    b) John 30

o    c) name age

o    d) ReferenceError

Answer: b) John 30
Explanation: This is an example of object destructuring where the properties name and age are extracted from obj and logged.


Let me know if you'd like me to continue!

1/2

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs to continue with the JavaScript ES6 features:


36.        What does the following code do?

javascript

Copy code

const multiply = (x, y = 2) => x * y;

console.log(multiply(5));

o    a) It prints undefined.

o    b) It throws an error due to missing parameters.

o    c) It prints 10.

o    d) It prints 5.

Answer: c) It prints 10.
Explanation: The function multiply has a default parameter y = 2. If y is not passed, it takes the default value of 2.


37.        Which of the following is the correct syntax for using the const keyword to declare a variable?

o    a) const name = "Alice";

o    b) const = "Alice";

o    c) const : name = "Alice";

o    d) const name;

Answer: a) const name = "Alice";
Explanation: const is used to declare a constant variable and must be initialized with a value at the time of declaration.


38.        Which of the following is true for the arrow function in ES6?

o    a) Arrow functions have their own this context.

o    b) Arrow functions cannot be used as methods of an object.

o    c) Arrow functions are always executed in strict mode.

o    d) Arrow functions are slower than regular functions.

Answer: b) Arrow functions cannot be used as methods of an object.
Explanation: Arrow functions do not have their own this, so they cannot be used as methods for objects, as this will refer to the enclosing scope.


39.        What is the value of x after the following code runs?

javascript

Copy code

const obj = { name: "Alice", age: 25 };

const { name, age } = obj;

const x = `${name} is ${age} years old.`;

console.log(x);

o    a) Alice is 25 years old.

o    b) name is age years old.

o    c) undefined is NaN years old.

o    d) Alice is age years old.

Answer: a) Alice is 25 years old.
Explanation: This uses template literals and destructuring. name and age are extracted from obj and used inside the template literal.


40.        What will happen when you try to reassign a value to a const variable?

javascript

Copy code

const name = "John";

name = "Doe";

o    a) It will throw a SyntaxError.

o    b) It will throw a ReferenceError.

o    c) It will change the value of name.

o    d) It will throw a TypeError.

Answer: a) It will throw a SyntaxError.
Explanation: const variables cannot be reassigned once they are initialized.


41.        Which of the following will correctly declare a function that accepts multiple parameters using arrow function syntax?

o    a) const sum = (a, b) => { return a + b };

o    b) const sum = (a, b) -> return a + b;

o    c) const sum = a, b => a + b;

o    d) const sum = function(a, b) { return a + b; };

Answer: a) const sum = (a, b) => { return a + b };
Explanation: This is the correct way to write a multi-line arrow function that takes two parameters and returns their sum.


42.        What will be the output of the following code?

javascript

Copy code

const person = { name: "Bob", job: "developer" };

const { name, job } = person;

console.log(job);

o    a) undefined

o    b) "developer"

o    c) name

o    d) job

Answer: b) "developer"
Explanation: This is an example of object destructuring, where the job property from person is assigned to the variable job and logged.


43.        Which of the following statements is true about let in JavaScript?

o    a) let is function-scoped.

o    b) let is block-scoped.

o    c) let variables can be re-declared in the same scope.

o    d) let cannot be used to declare variables inside loops.

Answer: b) let is block-scoped.
Explanation: Variables declared using let are scoped to the block in which they are defined, such as inside a loop or conditional.


44.        Which of the following will cause an error in ES6?

javascript

Copy code

const numbers = [1, 2, 3];

[a, b, c] = numbers;

o    a) No error will occur.

o    b) SyntaxError: destructuring assignment occurs.

o    c) ReferenceError: a is not defined.

o    d) TypeError: Cannot destructure.

Answer: c) ReferenceError: a is not defined.
Explanation: The error occurs because a, b, and c need to be declared (using let or const) before destructuring.


45.        Which of the following is a valid use of template literals?

o    a) const greeting = "Hello, ${name}";

o    b) const greeting = 'Hello, {name}';

o    c) const greeting = \Hello, ${name}`;`

o    d) const greeting = 'Hello, ${name}';

**Answer: c) const greeting = \Hello, ${name}`;** **Explanation:** Template literals use backticks (`` ``), and variables are embedded using ${}.


46.        What is the result of this destructuring code?

javascript

Copy code

const [first, second] = [1, 2];

console.log(first, second);

o    a) undefined undefined

o    b) 1 2

o    c) first second

o    d) 1

Answer: b) 1 2
Explanation: This is array destructuring, where first takes the value 1 and second takes the value 2 from the array.


47.        Which of the following is the correct way to define a class method in ES6?

o    a) class MyClass { method() {} }

o    b) class MyClass { function method() {} }

o    c) class MyClass { method: function() {} }

o    d) class MyClass { method() = {} }

Answer: a) class MyClass { method() {} }
Explanation: In ES6, class methods are defined without the function keyword, using the method syntax inside the class body.


48.        What is the purpose of the spread operator (...) in ES6?

o    a) To combine multiple arrays into one.

o    b) To split an object into separate properties.

o    c) To pass a variable number of arguments to a function.

o    d) All of the above.

Answer: d) All of the above.
Explanation: The spread operator can be used for arrays, objects, and function arguments, making it a versatile feature in ES6.


49.        What will the following code print?

javascript

Copy code

const numbers = [1, 2, 3];

const newNumbers = [...numbers, 4];

console.log(newNumbers);

o    a) [1, 2, 3]

o    b) [4, 5, 6]

o    c) [1, 2, 3, 4]

o    d) [4]

Answer: c) [1, 2, 3, 4]
Explanation: The spread operator ... is used to copy elements from numbers into a new array newNumbers and adds 4 at the end.


50.        What does this ES6 code do?

javascript

Copy code

const obj1 = { a: 1, b: 2 };

const obj2 = { ...obj1, c: 3 };

console.log(obj2);

o    a) It throws a TypeError.

o    b) It creates an object with properties a, b, and c from obj1 and adds c: 3.

o    c) It merges obj1 and obj2 and adds c: 3.

o    d) It throws a ReferenceError.

Answer: b) It creates an object with properties a, b, and c from obj1 and adds c: 3.
Explanation: The spread operator is used to copy the properties of obj1 into obj2, and c: 3 is added to obj2.


Let me know if you'd like more questions!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs to continue with the JavaScript ES6 features:


51.        Which of the following code snippets demonstrates destructuring an object in ES6?

javascript

Copy code

const person = { name: "Alice", age: 25 };

const { name, age } = person;

o    a) const { name, age } = { name: "Alice", age: 25 };

o    b) const [name, age] = { name: "Alice", age: 25 };

o    c) const { name, age } = person;

o    d) const { person.name, person.age } = person;

Answer: c) const { name, age } = person;
Explanation: This is the correct way to destructure an object, extracting the name and age properties from the person object.


52.        What is the result of the following code?

javascript

Copy code

const person = { name: "John", job: "developer" };

const { name, job } = person;

console.log(name, job);

o    a) John developer

o    b) undefined undefined

o    c) name job

o    d) John, developer

Answer: a) John developer
Explanation: This is an example of object destructuring, where name and job are extracted from the person object and logged.


53.        What is the purpose of let in JavaScript?

o    a) It creates a global variable.

o    b) It allows for block-scoped variables.

o    c) It is used to define constants.

o    d) It creates variables that can’t be reassigned.

Answer: b) It allows for block-scoped variables.
Explanation: let creates variables that are scoped to the block in which they are defined, unlike var, which is function-scoped.


54.        Which of the following is true about arrow functions in ES6?

o    a) They can have multiple this values.

o    b) They cannot be used as constructors.

o    c) They are always bound to the global this.

o    d) They can be declared with the function keyword.

Answer: b) They cannot be used as constructors.
Explanation: Arrow functions cannot be used as constructors and do not have their own this, unlike regular functions.


55.        What is the output of the following code?

javascript

Copy code

const add = (a, b) => {

    return a + b;

};

console.log(add(5, 10));

o    a) 15

o    b) 5

o    c) 10

o    d) undefined

Answer: a) 15
Explanation: The arrow function add takes two parameters, a and b, and returns their sum, which is logged as 15.


56.        Which of the following is a valid template literal expression?

o    a) const greeting = 'Hello, ${name}';

o    b) const greeting = "Hello, {name}";

o    c) const greeting = 'Hello, ${name}';

o    d) const greeting = \Hello, ${name}`;`

**Answer: d) const greeting = \Hello, ${name}`;** **Explanation:** Template literals use backticks (`` ``) and ${} to interpolate variables into the string.


57.        What will be the output of the following code?

javascript

Copy code

const multiply = (x, y = 3) => x * y;

console.log(multiply(5));

o    a) 5

o    b) 15

o    c) undefined

o    d) 3

Answer: b) 15
Explanation: The function multiply uses a default parameter for y. Since no second argument is passed, y takes the default value of 3, and 5 * 3 = 15.


58.        What is the output of the following code?

javascript

Copy code

let x = 10;

if (true) {

    let x = 20;

}

console.log(x);

o    a) 20

o    b) 10

o    c) undefined

o    d) SyntaxError

Answer: b) 10
Explanation: The let variable inside the block creates a new x that is scoped to the block. The x outside the block remains 10.


59.        What is the main advantage of using const over let and var in ES6?

o    a) const variables can be reassigned.

o    b) const variables are function-scoped.

o    c) const variables must be initialized with a value and cannot be reassigned.

o    d) const variables can only be used in loops.

Answer: c) const variables must be initialized with a value and cannot be reassigned.
Explanation: const is used to create variables that are constant. Once a value is assigned, it cannot be reassigned.


60.        What will be the output of the following code?

javascript

Copy code

const person = { name: "Alice", age: 30 };

const { name, age, gender = "Female" } = person;

console.log(gender);

o    a) Female

o    b) undefined

o    c) Alice

o    d) 30

Answer: a) Female
Explanation: Since gender is not present in the person object, the default value "Female" is used, and logged.


61.        How can you access a property from an object in ES6 using destructuring?

o    a) const { property } = object;

o    b) const object.property = value;

o    c) object[property] = value;

o    d) const property = object.property;

Answer: a) const { property } = object;
Explanation: Destructuring is a concise syntax to extract properties from objects. property is extracted from object using { property }.


62.        What will be the output of the following code?

javascript

Copy code

const add = (a, b = 5) => a + b;

console.log(add(2));

o    a) 2

o    b) 5

o    c) 7

o    d) undefined

Answer: c) 7
Explanation: The function add uses a default value for b (5). If only a is passed, b takes the default value and the sum 2 + 5 = 7 is logged.


63.        What is the result of this destructuring code?

javascript

Copy code

const arr = [1, 2, 3];

const [first, second] = arr;

console.log(first, second);

o    a) undefined undefined

o    b) 1 2

o    c) first second

o    d) 1

Answer: b) 1 2
Explanation: This is an example of array destructuring, where first takes the value 1 and second takes the value 2.


64.        What will the following code output?

javascript

Copy code

const numbers = [1, 2, 3];

const newNumbers = [...numbers, 4];

console.log(newNumbers);

o    a) [1, 2, 3, 4]

o    b) [4, 5, 6]

o    c) [1, 2, 3]

o    d) [4]

Answer: a) [1, 2, 3, 4]
Explanation: The spread operator ... is used to copy the elements from numbers into a new array newNumbers, with 4 added at the end.


65.        What will the following code do?

javascript

Copy code

const arr1 = [1, 2];

const arr2 = [3, 4];

const combined = [...arr1, ...arr2];

console.log(combined);

o    a) [1, 2, 3, 4]

o    b) [3, 4, 1, 2]

o    c) [1, 2]

o    d) SyntaxError

Answer: a) [1, 2, 3, 4]
Explanation: The spread operator ... combines the elements from arr1 and arr2 into a single array combined, resulting in [1, 2, 3, 4].


Let me know if you'd like more questions or explanations!

4o mini

You said:

yes

ChatGPT said:

ChatGPT

Here are more MCQs on JavaScript ES6 features:


66.        Which of the following is true about let and const in JavaScript?

o    a) let creates a block-scoped variable, but const creates a function-scoped variable.

o    b) let and const both create block-scoped variables, but const cannot be reassigned.

o    c) let creates a global-scoped variable, but const creates a block-scoped variable.

o    d) let and const both create global-scoped variables.

Answer: b) let and const both create block-scoped variables, but const cannot be reassigned.
Explanation: Both let and const are block-scoped. The difference is that variables declared with const cannot be reassigned once they are initialized.


67.        What is the output of the following code?

javascript

Copy code

const square = (n) => n * n;

console.log(square(4));

o    a) 4

o    b) 16

o    c) undefined

o    d) null

Answer: b) 16
Explanation: The arrow function square takes a parameter n, multiplies it by itself (n * n), and the result of 4 * 4 is 16, which is logged.


68.        What will be the output of the following code?

javascript

Copy code

let x = 10;

if (true) {

    let x = 20;

}

console.log(x);

o    a) 20

o    b) 10

o    c) undefined

o    d) SyntaxError

Answer: b) 10
Explanation: The let inside the if block is scoped to that block. The x outside the block remains unaffected by the block-scoped x, so the output is 10.


69.        Which of the following is the correct syntax for defining an arrow function that takes two parameters and returns their sum?

o    a) const sum = (a, b) => return a + b;

o    b) const sum = (a, b) => a + b;

o    c) const sum = (a, b) => { return a + b; }

o    d) Both b and c

Answer: d) Both b and c
Explanation: Both b and c are correct syntax for an arrow function. b uses an implicit return, while c uses an explicit return.


70.        Which of the following is a valid template literal in ES6?

o    a) const greeting = "Hello, " + name + "!";

o    b) const greeting = 'Hello, ${name}!';

o    c) const greeting = \Hello, ${name}!`;`

o    d) const greeting = "Hello, {name}!";

**Answer: c) const greeting = \Hello, ${name}!`;** **Explanation:** Template literals use backticks (\`) and ${} to interpolate variables or expressions into the string.


71.        What will be the output of the following code?

javascript

Copy code

const arr = [1, 2, 3];

const newArr = [...arr, 4];

console.log(newArr);

o    a) [1, 2, 3, 4]

o    b) [4, 1, 2, 3]

o    c) [1, 4, 2, 3]

o    d) SyntaxError

Answer: a) [1, 2, 3, 4]
Explanation: The spread operator ... creates a new array with all the elements of arr followed by 4.


72.        Which of the following is correct when using default parameters in ES6?

javascript

Copy code

function greet(name = "Guest") {

    return "Hello, " + name;

}

o    a) The default value is used only when the parameter is undefined.

o    b) The default value is used if the parameter is null.

o    c) The default value is used if the parameter is an empty string.

o    d) Both a and b

Answer: d) Both a and b
Explanation: Default parameters are used when a parameter is either undefined or missing.


73.        Which of the following is true about arrow functions?

o    a) Arrow functions have their own this context.

o    b) Arrow functions can be used as constructors.

o    c) Arrow functions do not have their own this context, they inherit this from their surrounding scope.

o    d) Arrow functions require the function keyword.

Answer: c) Arrow functions do not have their own this context, they inherit this from their surrounding scope.
Explanation: Unlike traditional functions, arrow functions do not have their own this. They inherit this from the enclosing scope.


74.        What is the result of the following code?

javascript

Copy code

const person = { name: "John", age: 30 };

const { name, age, gender = "Male" } = person;

console.log(gender);

o    a) Male

o    b) undefined

o    c) John

o    d) 30

Answer: a) Male
Explanation: Since gender is not defined in the person object, the default value "Male" is used.


75.        What will the following code output?

javascript

Copy code

const multiply = (a, b = 5) => a * b;

console.log(multiply(2));

o    a) 2

o    b) 10

o    c) 5

o    d) undefined

Answer: b) 10
Explanation: The multiply function uses b = 5 as a default value. Since b is not provided, it defaults to 5, so the result of 2 * 5 = 10 is logged.


76.        What will the following code output?

javascript

Copy code

const arr = [1, 2, 3];

const newArr = arr.map(n => n * 2);

console.log(newArr);

o    a) [1, 2, 3]

o    b) [2, 4, 6]

o    c) [2, 3, 4]

o    d) undefined

Answer: b) [2, 4, 6]
Explanation: The map() function applies the given function (n => n * 2) to each element in the array, resulting in a new array [2, 4, 6].


77.        Which of the following is the correct way to define a constant array in JavaScript?

o    a) const arr = [1, 2, 3];

o    b) const arr = [1, 2, 3]; arr.push(4);

o    c) let arr = [1, 2, 3];

o    d) Both a and b

Answer: d) Both a and b
Explanation: You can define a constant array using const. However, while the reference to the array cannot be changed, the contents of the array can still be modified using methods like push().


78.        What will the following code output?

javascript

Copy code

const numbers = [1, 2, 3];

const [first, second] = numbers;

console.log(first, second);

o    a) 1 2

o    b) undefined undefined

o    c) 1

o    d) 2 3

Answer: a) 1 2
Explanation: This is an example of array destructuring, where first takes the value 1 and second takes the value 2 from the numbers array.


79.        What is the output of the following code?

javascript

Copy code

const person = { name: "Alice", age: 25 };

const { name, age } = person;

console.log(age);

o    a) Alice

o    b) 25

o    c) undefined

o    d) name

Answer: b) 25
Explanation: The age property is destructured from the person object and logged, so 25 is printed.


80.        What is the output of the following code?

javascript

Copy code

const greet = name => `Hello, ${name}!`;

console.log(greet("John"));

o    a) Hello John!

o    b) Hello, John!

o    c) Hello ${name}!

o    d) Hello, !

Answer: b) Hello, John!
Explanation: This is an example of template literals, where ${name} is replaced with "John", resulting in "Hello, John!".


4o mini

You said:

more

ChatGPT said:

ChatGPT

Here are more MCQs based on ES6 features in JavaScript:


81.        What will be the output of the following code?

javascript

Copy code

const add = (a, b = 5) => a + b;

console.log(add(10, 20));

o    a) 25

o    b) 30

o    c) undefined

o    d) 10

Answer: b) 30
Explanation: Since both a and b are passed as arguments (10 and 20), the function returns 10 + 20 = 30.


82.        Which of the following will correctly declare a block-scoped variable using ES6 syntax?

o    a) var x = 10;

o    b) const x = 10;

o    c) let x = 10;

o    d) Both b and c

Answer: d) Both b and c
Explanation: const and let both declare variables that are block-scoped, whereas var is function-scoped.


83.        What will be the output of the following code?

javascript

Copy code

let x = 5;

function foo() {

    x = 10;

    console.log(x);

}

foo();

o    a) 5

o    b) 10

o    c) undefined

o    d) ReferenceError

Answer: b) 10
Explanation: Since x is declared outside the function and modified inside the function, it outputs 10.


84.        What does the following code demonstrate?

javascript

Copy code

const person = { name: "John", age: 25 };

const { name, age } = person;

console.log(name);

o    a) Template literal

o    b) Destructuring assignment

o    c) Spread operator

o    d) Arrow function

Answer: b) Destructuring assignment
Explanation: This is an example of object destructuring, where name and age are extracted from the person object.


85.        Which of the following is the correct syntax to define a default parameter in an arrow function?

o    a) const sum = (a, b = 10) => a + b;

o    b) const sum = (a, b : 10) => a + b;

o    c) const sum = (a, b) => { a + b = 10; };

o    d) const sum = (a, b) => { a = b = 10; };

Answer: a) const sum = (a, b = 10) => a + b;
Explanation: Default parameters are set using the = operator. If b is not passed, it defaults to 10.


86.        What is the output of the following code?

javascript

Copy code

const multiply = (x, y) => x * y;

console.log(multiply(3, 5));

o    a) 15

o    b) 35

o    c) 8

o    d) null

Answer: a) 15
Explanation: The arrow function multiplies 3 and 5, which results in 15.


87.        Which of the following statements about template literals is correct?

o    a) Template literals use single quotes (') to define strings.

o    b) Template literals support string interpolation using ${}.

o    c) Template literals cannot span multiple lines.

o    d) Template literals must be used with a return statement.

Answer: b) Template literals support string interpolation using ${}.
Explanation: Template literals use backticks (\``), and ${}` is used for embedding expressions or variables inside strings.


88.        What is the output of the following code?

javascript

Copy code

const obj = { a: 1, b: 2 };

const { a, b, c = 3 } = obj;

console.log(c);

o    a) 1

o    b) 2

o    c) undefined

o    d) 3

Answer: d) 3
Explanation: Since c is not present in the obj, the default value 3 is used, so 3 is logged.


89.        Which of the following is the correct syntax for an arrow function with no parameters?

o    a) const greet = () => 'Hello!';

o    b) const greet = {} => 'Hello!';

o    c) const greet = () : 'Hello!';

o    d) const greet = ('') => 'Hello!';

Answer: a) const greet = () => 'Hello!';
Explanation: Arrow functions with no parameters are defined using empty parentheses () followed by the arrow =>.


90.        What is the output of the following code?

javascript

Copy code

let x = 10;

if (true) {

    let x = 20;

}

console.log(x);

o    a) 20

o    b) 10

o    c) undefined

o    d) null

Answer: b) 10
Explanation: The x inside the if block is block-scoped due to let. The x outside the block remains unaffected and is logged as 10.


91.        What will the following code output?

javascript

Copy code

let arr = [10, 20, 30];

const [first, , third] = arr;

console.log(third);

o    a) 10

o    b) 20

o    c) 30

o    d) undefined

Answer: c) 30
Explanation: This demonstrates array destructuring where the second element is skipped (using a comma) and the third element is assigned to the variable third, which is 30.


92.        Which of the following statements is true about the let keyword in JavaScript?

o    a) Variables declared with let are function-scoped.

o    b) Variables declared with let are block-scoped.

o    c) Variables declared with let cannot be reassigned.

o    d) Variables declared with let are hoisted to the top of the function.

Answer: b) Variables declared with let are block-scoped.
Explanation: Variables declared with let are block-scoped, which means they are only available within the block they are declared in.


93.        What will be the result of the following code?

javascript

Copy code

let x = 1;

const obj = { x: 2 };

const { x } = obj;

console.log(x);

o    a) 1

o    b) 2

o    c) undefined

o    d) SyntaxError

Answer: b) 2
Explanation: The value of x inside the object obj is destructured and assigned to x, so 2 is logged.


94.        Which of the following is a valid way to define a function using an arrow function in ES6?

o    a) function sum(a, b) { return a + b; }

o    b) const sum = a, b => a + b;

o    c) const sum = (a, b) => a + b;

o    d) const sum = function(a, b) { return a + b; }

Answer: c) const sum = (a, b) => a + b;
Explanation: This is the correct arrow function syntax. It takes two parameters a and b and returns their sum.


95.        What is the default behavior when destructuring an object that has a property with a null value?

javascript

Copy code

const obj = { name: null };

const { name } = obj;

console.log(name);

o    a) null

o    b) undefined

o    c) SyntaxError

o    d) "" (empty string)

Answer: a) null
Explanation: When destructuring, if a property has a value of null, it will be assigned as null.


96.        Which of the following code snippets is an example of using a template literal?

o    a) let greeting = 'Hello ' + name + '!';

o    b) let greeting = 'Hello {name}!';

o    c) let greeting = \Hello, ${name}!`;`

o    d) let greeting = "Hello, name!"

**Answer: c) let greeting = \Hello, ${name}!`;** **Explanation:** Template literals use backticks (\`) and ${} for embedding variables or expressions into the string.


97.        What is the output of the following code?

javascript

Copy code

let a = 5;

let b = 10;

[a, b] = [b, a];

console.log(a, b);

o    a) 5 10

o    b) 10 5

o    c) undefined undefined

o    d) SyntaxError

Answer: b) 10 5
Explanation: This demonstrates array destructuring where the values of a and b are swapped.


98.        Which statement about the const keyword in JavaScript is true?

o    a) Variables declared with const are block-scoped.

o    b) Variables declared with const can be reassigned.

o    c) const variables cannot be assigned objects.

o    d) const is function-scoped.

Answer: a) Variables declared with const are block-scoped.
Explanation: const is block-scoped, similar to let. However, const variables cannot be reassigned once initialized.


99.        What is the output of the following code?

javascript

Copy code

const greet = name => `Hello, ${name}!`;

console.log(greet('Alice'));

o    a) Hello, !

o    b) Hello, Alice!

o    c) Hello, name!

o    d) undefined

Answer: b) Hello, Alice!
Explanation: This is a template literal that correctly interpolates the name argument into the string, returning Hello, Alice!.


100. What will the output of this code be? javascript const user = { name: 'John', age: 30 }; const { name, ...rest } = user; console.log(rest); - a) { name: 'John' } - b) { age: 30 } - c) { name: 'John', age: 30 } - d) SyntaxError

css

Copy code

**Answer: b) `{ age: 30 }`** 

typescript

Copy code

**Explanation:** The `...rest` syntax collects the remaining properties (`age` in this case) into a new object.

Post a Comment

0Comments

Post a Comment (0)