JavaScript MCQs
MCQ 1
Question: Which JavaScript method can be used to convert a string into an array of characters?
- A.
split()
- B.
join()
- C.
charAt()
- D.
substr()
Answer: A. split()
MCQ 2
Question: What is the output of the following code snippet?
let x = 5;
let y = 10;
let z = x++ + y--;
console.log(z);
- A. 14
- B. 15
- C. 16
- D. 17
Answer: B. 15
Explanation: x++
increments after evaluation (x=6), y--
decrements after evaluation (y=9).
Hence, z = 6 + 9 = 15
.
MCQ 3
Question: What is the correct way to get an element with the ID myDiv
using JavaScript?
- A.
document.getElementById("myDiv")
- B.
getElementById("myDiv")
- C.
document.get("myDiv")
- D.
document.find("myDiv")
Answer: A. document.getElementById("myDiv")
MCQ 4
Question: Given fruits = ["apple", "banana", "orange"]
, how can you create a new array containing only fruits that start with "a"?
- A.
fruits.filter(fruit => fruit.startsWith("a"))
- B.
fruits.map(fruit => fruit.startsWith("a"))
- C.
fruits.reduce((acc, fruit) => fruit.startsWith("a") ? [...acc, fruit] : acc, [])
- D.
fruits.forEach(fruit => fruit.startsWith("a") ? console.log(fruit) : null)
Answer: A. fruits.filter(fruit => fruit.startsWith("a"))
MCQ 5
Question: What is the output of the following code?
const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map(num => num * num);
console.log(squaredNumbers);
- A. [1, 4, 9, 16, 25]
- B. [2, 4, 6, 8, 10]
- C. [1, 2, 3, 4, 5]
- D. An error will be thrown
Answer: A. [1, 4, 9, 16, 25]
No comments:
Post a Comment