Q. Which of the following is used to handle exceptions in JavaScript?

  • (A) try-catch
  • (B) do-except
  • (C) if-error
  • (D) error-handler
πŸ’¬ Discuss
βœ… Correct Answer: (A) try-catch
Explanation: try-catch is used to handle exceptions in JavaScript.

Q. What will be the output of this code?

Code:
console.log(1 == '1');
  • (A) false
  • (B) true
  • (C) undefined
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (B) true
Explanation: == performs type coercion, so 1 == '1' is true.

Q. Which method adds an element at the end of an array?

  • (A) push()
  • (B) add()
  • (C) insert()
  • (D) append()
πŸ’¬ Discuss
βœ… Correct Answer: (A) push()
Explanation: The push() method adds an element at the end of an array.

Q. What will this code print?

Code:
console.log(typeof undefined === typeof NULL);
  • (A) true
  • (B) false
  • (C) undefined
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (B) false
Explanation: NULL is not defined (uppercase), so the code throws a ReferenceError.

Q. What will this function return?

Code:
function test() {
  return;
}
console.log(test());
  • (A) undefined
  • (B) null
  • (C) 0
  • (D) NaN
πŸ’¬ Discuss
βœ… Correct Answer: (A) undefined
Explanation: A function with a bare return statement returns undefined.

Q. What is the output of this code?

Code:
let name = 'MCQ';
name[0] = 'X';
console.log(name);
  • (A) XCQ
  • (B) MCQ
  • (C) undefined
  • (D) Error
πŸ’¬ Discuss
βœ… Correct Answer: (B) MCQ
Explanation: Strings are immutable in JavaScript; indexing does not allow reassignment.

Q. How do you convert a number to a string in JavaScript?

  • (A) num.toString()
  • (B) String(num)
  • (C) num + ''
  • (D) All of the above
πŸ’¬ Discuss
βœ… Correct Answer: (D) All of the above
Explanation: All listed methods are valid ways to convert a number to a string.

Q. Which operator is used for optional chaining?

  • (A) ?.
  • (B) ?
  • (C) ??
  • (D) ->
πŸ’¬ Discuss
βœ… Correct Answer: (A) ?.
Explanation: The optional chaining operator (?.) allows accessing properties without throwing errors if the object is null or undefined.

Q. What is the result of this code?

Code:
console.log([] == 0);
  • (A) true
  • (B) false
  • (C) undefined
  • (D) NaN
πŸ’¬ Discuss
βœ… Correct Answer: (A) true
Explanation: JavaScript coerces [] to 0 when compared with ==

Q. How do you write a conditional statement for executing code if 'x' is greater than 5?

  • (A) if x > 5
  • (B) if (x > 5)
  • (C) if x > 5 then
  • (D) if (x > 5) then
πŸ’¬ Discuss
βœ… Correct Answer: (B) if (x > 5)
Explanation: JavaScript requires parentheses in if conditions.