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

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
    • Datatype and Bit Puzzles
    • Fork Join Puzzles
    • Queue and Array Puzzles
    • String and Casting Puzzles
      • String Substr with Runtime Class Cast
      • putc/getc Index Semantics
      • Case-Sensitive vs Case-Insensitive Compare
      • Enum Safety with $cast
      • Failed Class Cast Leaves Old Handle
      • String Number Parsing Stops Early
    • SystemVerilog Language Puzzles: Tricky Q&A
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • Interview Prep: Puzzle Rounds5
  • DV Puzzle Cheatsheet
Loading lesson

‹String and Casting Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. SystemVerilog Language Puzzles
  4. String and Casting Puzzles
  5. Failed Class Cast Leaves Old Handle

VLSI DV Interview Puzzles · All levels

Failed Class Cast Leaves Old Handle

Why does failed cast still let code print a valid `id`?

On this page

    Puzzle

    Difficulty: Hard · Puzzle 5 of 6 · Topic: String and Casting Puzzles

    Why does failed cast still let code print a valid `id`?

    Code

    p5.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class B; endclass
    class D extends B; int id = 42; endclass
    class E extends B; endclass
    
    module p5;
      B b1, b2;
      D d;
      initial begin
        b1 = new D();
        b2 = new E();
        void'($cast(d, b1));
        if (!$cast(d, b2))
          $display("cast2 fail id=%0d", d.id);
      end
    endmodule

    Hint

    Check what `$cast` guarantees on failure for destination variable state.

    Step-by-step solution

    Diagram
    1) First cast succeeds, so `d` points to D object with id 42.
    2) Second cast from E to D fails and returns 0.
    3) On failed class cast, destination is unchanged, so `d` still references prior D object.
    4) Therefore `d.id` is still valid and prints 42.

    Answer

    Answer: It prints `cast2 fail id=42`

    Why candidates get it wrong

    Many candidates assume failed `$cast` sets destination to null automatically.

    Interviewer follow-up

    What defensive pattern prevents stale-handle use after failed cast?

    Related topics

    • Back to String and Casting Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    27 / 210

    Next: String Number Parsing Stops Early

    Up next

    Up nextString Number Parsing Stops Early

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • String Substr with Runtime Class Cast
    • putc/getc Index Semantics
    • Case-Sensitive vs Case-Insensitive Compare