Bird
0
0

What will be the output of this code?

medium📝 Analysis Q5 of 15
LLD - Behavioral Design Patterns — Part 2
What will be the output of this code?
class ElementB {
  accept(visitor) {
    visitor.visitElementB(this);
  }
}

class Visitor {
  visitElementA(element) {
    console.log('Visited A');
  }
}

const element = new ElementB();
const visitor = new Visitor();
element.accept(visitor);
AVisited A
BVisited B
CTypeError: visitor.visitElementB is not a function
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze accept call

    element.accept(visitor) calls visitor.visitElementB(this), but Visitor class has no visitElementB method.
  2. Step 2: Identify error

    Calling undefined method causes TypeError: visitor.visitElementB is not a function.
  3. Final Answer:

    TypeError: visitor.visitElementB is not a function -> Option C
  4. Quick Check:

    Missing visit method causes TypeError [OK]
Quick Trick: Visitor must implement all visit methods used by elements [OK]
Common Mistakes:
MISTAKES
  • Assuming fallback to visitElementA
  • Expecting no output
  • Ignoring missing method error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes