Bird
0
0

What will be the output of the following code?

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

class Visitor {
  visitElementC(element) {
    console.log('ElementC visited');
  }
}

const element = new ElementC();
const visitor = new Visitor();
element.accept(visitor);
AVisited ElementC
BElementC visited
CError: visitElementC is not a function
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze accept method

    ElementC's accept calls visitor.visitElementC(this).
  2. Step 2: Check Visitor method

    Visitor has visitElementC method that logs 'ElementC visited'.
  3. Step 3: Execution

    Calling element.accept(visitor) triggers console.log with 'ElementC visited'.
  4. Final Answer:

    ElementC visited -> Option B
  5. Quick Check:

    Method call matches visitor method [OK]
Quick Trick: accept calls matching visit method [OK]
Common Mistakes:
MISTAKES
  • Assuming method name mismatch causes error
  • Expecting different output text
  • Ignoring this parameter in visit call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes