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. String Substr with Runtime Class Cast

VLSI DV Interview Puzzles · All levels

String Substr with Runtime Class Cast

Predict all outputs from string methods and both `$cast` attempts.

On this page

    Puzzle

    Difficulty: Easy · Puzzle 1 of 6 · Topic: String and Casting Puzzles

    Predict all outputs from string methods and both `$cast` attempts.

    Code

    p1.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    class base_pkt; endclass
    class good_pkt extends base_pkt;
      int id = 42;
    endclass
    class bad_pkt extends base_pkt; endclass
    
    module p1;
      string s = "dv puzzle";
      base_pkt b1, b2;
      good_pkt g;
      initial begin
        b1 = new good_pkt();
        b2 = new bad_pkt();
    
        $display("substr=%s", s.substr(3,8));
        $display("toupper=%s", s.toupper());
    
        if ($cast(g, b1)) $display("cast1 ok id=%0d", g.id);
        else              $display("cast1 fail");
    
        if ($cast(g, b2)) $display("cast2 ok id=%0d", g.id);
        else              $display("cast2 fail");
      end
    endmodule

    Hint

    `substr(i,j)` uses inclusive indices. `$cast` checks runtime object type, not just declared handle type.

    Step-by-step solution

    Diagram
    1) `substr(3,8)` on `"dv puzzle"` returns `"puzzle"` (index 3 through 8 inclusive).
    2) `toupper()` returns `"DV PUZZLE"`.
    3) `b1` points to `good_pkt`, so `$cast(g,b1)` succeeds and prints id 42.
    4) `b2` points to `bad_pkt`, so cast to `good_pkt` fails.

    Answer

    Answer: substr=puzzle, toupper=DV PUZZLE, cast1 ok id=42, cast2 fail

    Why candidates get it wrong

    Candidates confuse static type compatibility with dynamic runtime-type compatibility.

    Interviewer follow-up

    What happens to `g` after the failed second cast in this code?

    Related topics

    • Back to String and Casting Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    23 / 210

    Next: putc/getc Index Semantics

    Up next

    Up nextputc/getc Index Semantics

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Case-Sensitive vs Case-Insensitive Compare
    • Enum Safety with $cast
    • Failed Class Cast Leaves Old Handle