DEV Community

KAMAL KISHOR
KAMAL KISHOR

Posted on

25 Must-Know JavaScript Output & MCQ Questions (With Answers & Explanations)

JavaScript interviews often include tricky output-based questions that test your understanding of scoping, hoisting, closures, this, and more. Below is a detailed blog-style guide that walks through 25 frequently asked JS MCQ problems with their answers and clear explanations.


πŸ”₯ 1. Hoisting & TDZ

function sayHi() {
  console.log(name);
  console.log(age);
  var name = 'Lydia';
  let age = 21;
}
sayHi();
Enter fullscreen mode Exit fullscreen mode

Answer: D β€” undefined and ReferenceError

Why:
β€’ var name is hoisted and defaults to undefined until assignment.
β€’ let age is hoisted but not initialized β€” so accessing it before declaration throws a ReferenceError.
This is due to the Temporal Dead Zone (TDZ) for let/const. ([Medium][1])


πŸ”₯ 2. Loop + setTimeout

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” 3 3 3 and 0 1 2

Why:
β€’ var is function scoped β†’ by the time callbacks run, i is 3.
β€’ let is block scoped β†’ each iteration captures its own i. ([Medium][1])


πŸ”₯ 3. this in regular vs arrow

const shape = {
  radius: 10,
  diameter() {
    return this.radius * 2;
  },
  perimeter: () => 2 * Math.PI * this.radius,
};

console.log(shape.diameter());
console.log(shape.perimeter());
Enter fullscreen mode Exit fullscreen mode

Answer: B β€” 20 and NaN

Why:
β€’ Regular methods use object this.
β€’ Arrow functions don’t get a this β†’ they inherit from outer scope β†’ this.radius is undefined β†’ result is NaN. ([Medium][2])


πŸ”₯ 4. Unary plus + logical NOT

+true;
!'Lydia';
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” 1 and false

Why:
β€’ +true converts to number β†’ 1.
β€’ 'Lydia' is truthy β†’ ! makes it false. ([Medium][2])


πŸ”₯ 5. Property access with objects

const bird = { size: 'small' };
const mouse = { name: 'Mickey', small: true };
Enter fullscreen mode Exit fullscreen mode

Answer: D β€” All of them are valid

Why:
β€’ mouse.bird.size ❌ actually invalid because bird isn’t a property of mouse.
However β€” bracket lookups like mouse[bird.size] or mouse[bird["size"]] resolve to mouse['small']. ([Medium][1])


πŸ”₯ 6. Objects reference

let c = { greeting: 'Hey!' };
let d;
d = c;
c.greeting = 'Hello';
console.log(d.greeting);
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” Hello

Why:
Objects are reference types β€” d points to same object as c. ([Medium][2])


πŸ”₯ 7. == vs === with primitives & objects

let a = 3;
let b = new Number(3);
let c = 3;

console.log(a == b);
console.log(a === b);
console.log(b === c);
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” true false false

Why:
β€’ Loose == compares value β†’ true.
β€’ Strict === checks type β†’ false for Number object. ([Medium][2])


πŸ”₯ 8. Static method on class instance

class Chameleon {
  static colorChange(newColor) {
    this.newColor = newColor;
    return this.newColor;
  }
  constructor({ newColor = 'green' } = {}) {
    this.newColor = newColor;
  }
}

const freddie = new Chameleon({ newColor: 'purple' });
console.log(freddie.colorChange('orange'));
Enter fullscreen mode Exit fullscreen mode

Answer: D β€” TypeError

Why:
Static methods are not accessible on instance. ([Medium][1])


πŸ”₯ 9. Typos & undefined variables

let greeting;
greetign = {}; // Typo!
console.log(greetign);
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” {}

Why:
In non-strict mode, this creates a global variable. ([Medium][1])


πŸ”₯ 10. Functions are objects

function bark() {
  console.log('Woof!');
}
bark.animal = 'dog';
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” Nothing, this is fine!

Why:
Functions are first-class objects β€” can have properties. ([Medium][1])


πŸ”₯ 11. Adding methods after instances

function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

const member = new Person('Lydia', 'Hallie');
Person.getFullName = function() {
  return `${this.firstName} ${this.lastName}`;
};

console.log(member.getFullName());
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” TypeError

Why:
Method is added to constructor, not prototype. ([Medium][1])


πŸ”₯ 12. Forgetting new

const lydia = new Person('Lydia', 'Hallie');
const sarah = Person('Sarah', 'Smith');

console.log(lydia);
console.log(sarah);
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” Person { … } and undefined

Why:
Calling constructor without new returns undefined. ([Medium][1])


πŸ”₯ 13. Event propagation phases

Answer: D β€” Capturing > Target > Bubbling

Why:
Standard DOM event propagation order. ([Placement Preparation][3])


πŸ”₯ 14. Prototypes exist for all objects

Answer: A β€” true
Every object in JS has a prototype chain (except the root). ([Placement Preparation][3])


πŸ”₯ 15. Number + string

sum(1, '2');
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” "12"

Why:
String concatenation takes precedence. ([Placement Preparation][3])


πŸ”₯ 16. Increment operator

let number = 0;
console.log(number++);
console.log(++number);
console.log(number);
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” 0 2 2

Why:
Postfix vs prefix increment gives different values. ([Placement Preparation][3])


πŸ”₯ 17. Tagged template literals

getPersonInfo`${person} is ${age} years old`;
Enter fullscreen mode Exit fullscreen mode

Answer: B β€” array of strings, substitutions

Why:
Tagged templates pass strings array first, then values. ([Placement Preparation][3])


πŸ”₯ 18. Object literal comparison

Answer: C β€” β€œHmm.. You don't have an age I guess”
Object comparisons always check references, not values. ([Placement Preparation][3])


πŸ”₯ 19. Rest parameters type

typeof args
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” "object"
Rest produces an array, and typeof [] is "object". ([Placement Preparation][3])


πŸ”₯ 20. Strict mode undeclared variable

function getAge() {
  'use strict';
  age = 21;
}
getAge();
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” ReferenceError
Strict mode forbids undeclared variables. ([Placement Preparation][3])


πŸ”₯ 21. eval()

const sum = eval('10*10+5');
Enter fullscreen mode Exit fullscreen mode

Answer: A β€” 105
eval evaluates arbitrary JS expressions. ([Placement Preparation][3])


πŸ”₯ 22. sessionStorage lifespan

Answer: B β€” When the user closes the tab
Session storage is tab-specific. ([Placement Preparation][3])


πŸ”₯ 23. var redeclaration

var num = 8;
var num = 10;
console.log(num);
Enter fullscreen mode Exit fullscreen mode

Answer: B β€” 10
var allows redeclaration. ([Placement Preparation][3])


πŸ”₯ 24. Objects vs Sets

set.has('1');
set.has(1);
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” true true false true
Object keys are strings; Set distinguishes types. ([Placement Preparation][3])


πŸ”₯ 25. Duplicate keys in object

const obj = { a: 'one', b: 'two', a: 'three' };
Enter fullscreen mode Exit fullscreen mode

Answer: C β€” { a: "three", b: "two" }
Duplicate keys overwrite earlier ones. ([Placement Preparation][3])


Top comments (0)