Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutRoadmapCareersInternshipsFAQContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • Study Route: One Week Before the Interview
  • Study Route: Two Weeks
  • Study Route: Four Weeks (Full Course)4
  • OOP & Testbench Puzzles25
    • Handle and Copy Puzzles
    • Virtual Method Puzzles
      • Non-Virtual vs Virtual Through Base Handle
      • Missing super.new with Required Base Args
      • Virtual Call from Base Constructor
      • Same Name, Different Prototype: Hiding Not Overriding
      • Template Method with Non-Virtual Wrapper
      • super Method Call in Override Chain
    • Parameterized Class Puzzles
    • Factory Override Puzzles
    • OOP & Testbench Puzzles: Tricky Q&A
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • UVM Testbench Puzzles25
  • Clock, Reset & CDC Puzzles25
  • Bus and Interface Protocol Puzzles25
  • Interview Prep: Puzzle Rounds7
  • DV Puzzle Cheatsheet
Loading lesson

←Virtual Method Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. OOP & Testbench Puzzles
  4. Virtual Method Puzzles
  5. Virtual Call from Base Constructor
On this page

    VLSI DV Interview PuzzlesAll levels

    Virtual Call from Base Constructor

    Predict what prints and explain why this is dangerous in constructor design.

    Puzzle

    Difficulty: Hard · Puzzle 3 of 6 · Topic: Virtual Method Puzzles

    Predict what prints and explain why this is dangerous in constructor design.

    Code

    design.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    class base;
      function new();
        init();
      endfunction
      virtual function void init();
        $display("base init");
      endfunction
    endclass
    
    class child extends base;
      int cfg;
      function new();
        super.new();
        cfg = 32'hCAFE;
      endfunction
      function void init();
        $display("child cfg=%0h", cfg);
      endfunction
    endclass
    
    initial begin
      child c = new();
    end

    Hint

    Consider order: base constructor body runs before derived constructor body completes.

    Step-by-step solution

    Diagram
    1) `super.new()` enters base constructor, which calls virtual `init()`.
    2) Runtime object is `child`, so dynamic dispatch chooses `child::init()`.
    3) `cfg` has not been assigned `32'hCAFE` yet, so print shows default value (typically 0).
    4) Avoid virtual callbacks from constructors unless derived state is guaranteed initialized.

    Answer

    Answer: It prints `child cfg=0` (or default), because virtual dispatch reaches child method before child constructor assigns `cfg`.

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. The shown code run in Verilator: the virtual call from the base constructor runs child::init before cfg is assigned.

    Why candidates get it wrong

    Only `child::init` prints, never `base init`; by then the object is already a `child` but `cfg = 32'hCAFE` has not executed, so it shows 0. Virtual calls in constructors run derived code on partially initialized objects.

    Interviewer follow-up

    What safer pattern would you use in UVM: explicit `build()` hook or post-construction init API? Model answer: keep `new()` free of virtual calls and do the work in a phase (`build_phase`/`connect_phase`) or an explicit `init()` called after construction, when the whole derived object, its fields and its handles exist.

    Say it out loud

    It prints child with cfg equal to 0, or the default value. The base constructor calls a virtual init, and the object is really a child, so dispatch goes to the child version before the child constructor has assigned cfg. The trap is believing the base constructor only sees base behavior. I would avoid virtual callbacks from constructors unless the derived state is guaranteed to be ready.

    Related topics

    • Constructors & super.new()

    • Virtual dispatch

    • Back to Virtual Method Puzzles

    • OOP & Testbench Puzzles section

    Previous

    44 / 293

    Next: Same Name, Different Prototype: Hiding Not Overriding

    Up next

    Up nextSame Name, Different Prototype: Hiding Not Overriding

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Non-Virtual vs Virtual Through Base Handle
    • Template Method with Non-Virtual Wrapper
    • super Method Call in Override Chain