Bird
0
0

Given the following code snippet, what will be the output?

medium📝 Analysis Q13 of 15
LLD - Behavioral Design Patterns — Part 2
Given the following code snippet, what will be the output?
class ElementA {
  accept(visitor) {
    visitor.visit(this);
  }
}

class PrintVisitor {
  visit(element) {
    console.log('Visited element');
  }
}

const element = new ElementA();
const visitor = new PrintVisitor();
element.accept(visitor);
AVisited ElementA
BError: visit is not a function
CVisited element
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Trace accept method call

    The accept method calls visitor.visit(this), passing the element instance.
  2. Step 2: Check visit method behavior

    The visit method logs 'Visited element' to the console.
  3. Final Answer:

    Visited element -> Option C
  4. Quick Check:

    Visitor.visit logs message [OK]
Quick Trick: Visitor.visit prints message when accept calls it [OK]
Common Mistakes:
MISTAKES
  • Expecting element type name in output
  • Thinking visit method is missing
  • Assuming no output without explicit return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes