Skip to content

Latest commit

 

History

History
130 lines (89 loc) · 3.26 KB

questions_and_answers.md

File metadata and controls

130 lines (89 loc) · 3.26 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: A: {}

As the variable named "greeting" doesn't match with the initialized variable "greetign", the variable "greetign" will get assigned as the global scope. Thus, we can see the an empty object in the console. Also, the variable called "greeting" will be declared but won't be use anywhere.

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"

For passing a string value "2" with an integer 1 in the sum function, the JavaScript will perform implicit type coercion and convert the number 1 to a string in order to perform the concatenation operation with the string "2".

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: ['🍕', '🍫', '🥑', '🍔']

As the const variable food was declared with ['🍕', '🍫', '🥑', '🍔'] and it didn't get changed at any part of the code, also the info variable doesn't tried to change/modified the food variable. So, it will log the food variable with it's inital variable.

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 sayHi(name) function, it required a name parameter, but we didn't passed any argument. That's why the parameter will get undefined and it will console log as "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

In this code, for each non-zero value of nums it will increase the value of count by 1. In nums array there's only one 0 is existed other than that all the values are non-zero. So the count will get increased 3 time and it will log 3.