Skip to content

Latest commit

 

History

History
130 lines (89 loc) · 3.03 KB

questions_and_answers.md

File metadata and controls

130 lines (89 loc) · 3.03 KB

MCQ TEST

1. Write the correct answer from the following options and give an explanation (2-5 lines).
let greeting;
greetign = {};
console.log(greetign);
  • A: {}
  • B: ReferenceError: greetign is not defined
  • C: undefined
Answer

Answer: - B: ReferenceError: greetign is not defined

In this code, declare a variable named greetign, then when you attempt to assign an empty object then you write wrong variable name greetign. That's why JavaScript give ReferenceError

2. Write the correct answer from the following options and give an explanation (2-5 lines).
function sum(a, b) {
  return a + b;
}

sum(1, "2");
  • A: NaN
  • B: TypeError
  • C: "12"
  • D: 3
Answer

Answer: - C: "12"

In JavaScript, when you use plus operator + with a string then it performs concatenation. You Provide 2 as a string, that's why the result is 12.

3. Write the correct answer from the following options and give an explanation (2-5 lines).
const food = ["🍕", "🍫", "🥑", "🍔"];
const info = { favoriteFood: food[0] };

info.favoriteFood = "🍝";

console.log(food);
  • A: ['🍕', '🍫', '🥑', '🍔']
  • B: ['🍝', '🍫', '🥑', '🍔']
  • C: ['🍝', '🍕', '🍫', '🥑', '🍔']
  • D: ReferenceError
Answer

Answer: - A: ['🍕', '🍫', '🥑', '🍔']

In here, info.favoriteFood reassigned "🍝", but it's never effect original array. That's why result is the original array

4. Write the correct answer from the following options and give an explanation (2-5 lines).
function sayHi(name) {
  return `Hi there, ${name}`;
}

console.log(sayHi());
  • A: Hi there,
  • B: Hi there, undefined
  • C: Hi there, null
  • D: ReferenceError
Answer

Answer: - B: Hi there, undefined

In this code, the sayHi function except a parameter name. but when call the function without parameter and also without default parameter then the function return undefined. then Hi there and undefined add. So result is Hi there, undefined

5. Write the correct answer from the following options and give an explanation (2-5 lines).
let count = 0;
const nums = [0, 1, 2, 3];

nums.forEach((num) => {
  if (num) count += 1;
});

console.log(count);
  • A: 1
  • B: 2
  • C: 3
  • D: 4
Answer

Answer: - C: 3

This code run forEach loop. Each element in nums array coming and when num is 0, then it does not increment the count. All other values increment count. So when finises loop then count becomes 3.