JavaScript Classes & OOP Quiz
Work through 50 questions on class syntax, constructors, instance and static members, getters/setters, inheritance with extends/super, private fields, composition vs inheritance, using this, creating multiple instances, basic design patterns, and best practices.
Question 1
What does OOP (Object-Oriented Programming) emphasize?
Question 2
Which statement describes class declaration syntax?
Question 3
What is a class expression?
Question 4
What does the constructor method do?
Question 5
Which of the following describes class expressions?
Question 6
What is the difference between class declarations and expressions regarding hoisting?
Question 7
How do you create multiple instances from a class?
Question 8
Which statement about this in class methods is correct?
Question 9
What does new ClassName() return if constructor does not explicitly return an object?
Question 10
Which best describes class expressions vs declarations for naming?
Question 11
What does the constructor keyword define?
Question 12
What does this code log?
class Person {
constructor(name) {
this.name = name
}
greet() {
return `Hi, ${this.name}`
}
}
const alice = new Person('Alice')
console.log(alice.greet())Question 13
What does the following demonstrate?
class Counter {
constructor() {
this.count = 0
}
increment() {
this.count++
}
}
const c1 = new Counter()
const c2 = new Counter()
c1.increment()
console.log(c1.count, c2.count)Question 14
How do you define default values for constructor parameters?
class User {
constructor(name = 'Guest') {
this.name = name
}
}
const u = new User()
console.log(u.name)Question 15
What does returning an object inside constructor do?
class Example {
constructor() {
return { replaced: true }
}
}
const e = new Example()
console.log(e.replaced)Question 16
What does this code show about this binding?
class Logger {
constructor() {
this.prefix = '[LOG]'
this.log = this.log.bind(this)
}
log(msg) {
console.log(this.prefix, msg)
}
}
const logger = new Logger()
const fn = logger.log
fn('hello')Question 17
Where should per-instance mutable data typically be stored?
Question 18
How do you define instance methods in class syntax?
Question 19
What is the effect of using arrow functions as class fields for methods?
Question 20
Why might you avoid storing methods on this in the constructor?
Question 21
What does static keyword do for class members?
Question 22
What does this static example output?
class MathUtil {
static double(x) {
return x * 2
}
}
console.log(MathUtil.double(5))Question 23
How do getters and setters work in class bodies?
class Temperature {
constructor(celsius) {
this._c = celsius
}
get fahrenheit() {
return this._c * 9 / 5 + 32
}
set fahrenheit(value) {
this._c = (value - 32) * 5 / 9
}
}
const temp = new Temperature(0)
temp.fahrenheit = 212
console.log(temp.fahrenheit)Question 24
Why is encapsulation useful?
Question 25
What does private field syntax (#) enforce?
class Counter {
#value = 0
increment() {
this.#value++
}
get value() {
return this.#value
}
}
const c = new Counter()
c.increment()
console.log(c.value)Question 26
Why might you use static properties for caching?
Question 27
What does this getter/setter example show?
class Rectangle {
constructor(width, height) {
this.width = width
this.height = height
}
get area() {
return this.width * this.height
}
set area(value) {
this.height = value / this.width
}
}
const rect = new Rectangle(4, 5)
console.log(rect.area)
rect.area = 40
console.log(rect.height)Question 28
How do you declare getters/setters that map to private fields?
Question 29
Why use getters for computed properties?
Question 30
Why might you use setters to validate input?
Question 31
What does extends keyword do?
class Vehicle {}
class Car extends Vehicle {}Question 32
What does super() inside a constructor call?
class Animal {
constructor(name) {
this.name = name
}
}
class Cat extends Animal {
constructor(name) {
super(name)
this.type = 'cat'
}
}
const kitty = new Cat('Luna')
console.log(kitty.name, kitty.type)Question 33
What does this overriding example output?
class Shape {
area() { return 0 }
}
class Square extends Shape {
constructor(size) {
super()
this.size = size
}
area() {
return this.size ** 2
}
}
const sq = new Square(4)
console.log(sq.area())Question 34
How do you call a parent method from a child override?
Question 35
What does this logging example show?
class Logger {
log(msg) {
console.log('base', msg)
}
}
class ColorLogger extends Logger {
log(msg) {
super.log(msg)
console.log('color', msg)
}
}
new ColorLogger().log('info')Question 36
What does this snippet demonstrate about instanceof?
class A {}
class B extends A {}
const obj = new B()
console.log(obj instanceof B, obj instanceof A)Question 37
Why might you prefer composition over inheritance?
Question 38
What does this composition pattern show?
const flyable = Base => class extends Base {
fly() { return 'flying' }
}
const quackable = Base => class extends Base {
quack() { return 'quack' }
}
class Animal {}
class Duck extends quackable(flyable(Animal)) {}
const duck = new Duck()
console.log(duck.fly(), duck.quack())Question 39
How does method overriding interact with polymorphism?
Question 40
What happens if super.method() is omitted in an override when needed?
class Base {
init() {
this.baseSet = true
}
}
class Derived extends Base {
init() {
this.derivedSet = true
}
}
const d = new Derived()
d.init()
console.log(d.baseSet, d.derivedSet)Question 41
What does this private field example output?
class SecretBox {
#secret
constructor(value) {
this.#secret = value
}
reveal() {
return this.#secret
}
}
const box = new SecretBox('code')
console.log(box.reveal(), Object.keys(box))Question 42
How do private methods differ from public ones?
Question 43
What does this code show about private helpers?
class PasswordValidator {
#sanitize(raw) {
return String(raw).trim()
}
validate(raw) {
const value = this.#sanitize(raw)
return value.length >= 8
}
}
const validator = new PasswordValidator()
console.log(validator.validate(' secret '))Question 44
How do you verify multiple instances stay independent?
Question 45
Why can using class methods as event handlers require binding?
Question 46
What does this static counter demonstrate?
class Session {
static total = 0
constructor() {
Session.total += 1
}
}
new Session()
new Session()
console.log(Session.total)Question 47
Which basic design pattern does the following describe: providing a single shared instance throughout the app?
Question 48
When is a factory class useful?
Question 49
How does composition appear in class-based code?
Question 50
Which best practice improves class-based OOP design?
