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
    • 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
      • Upcast, Virtual Dispatch and Downcast
      • String Number Parsing Stops Early
    • SystemVerilog Language Puzzles: Tricky Q&A
  • 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
  • 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

←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
On this page

    VLSI DV Interview PuzzlesAll levels

    String Substr with Runtime Class Cast

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

    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 = good_pkt::new();
        b2 = bad_pkt::new();
    
        $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 a `good_pkt`, so `$cast(g,b1)` succeeds and prints id 42.
    4) `b2` points to a `bad_pkt`, so the cast to `good_pkt` fails and returns 0.

    Answer

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

    How this was checked

    Checked by running the code in Verilator 5; the printed output supports the answer above. A failed $cast returns 0 because the object is a bad_pkt; Icarus 13 lacks toupper and class $cast.

    Why candidates get it wrong

    `b2` has the static type `base_pkt` and `g` is a `good_pkt` handle, so the code compiles; only the run-time object type (`bad_pkt`) makes the second cast fail. Note that the constructor is written `good_pkt::new()` (or `new` assigned to a typed handle), never `new good_pkt()`.

    Interviewer follow-up

    What happens to `g` after the failed second cast in this code? Model answer: nothing: a failed `$cast` leaves the destination unchanged, so `g` still references the good_pkt object from the first cast and `g.id` is still 42.

    Say it out loud

    I would split this into string methods and a runtime cast. The substring from 3 to 8 is puzzle and the upper case version is DV PUZZLE. The first cast works because the base handle really points to a good packet, so id is 42, and the second fails and returns 0 because that object is a bad packet. The trap is that both casts compile, since only the run-time object type decides, so I would always check the return value of the cast.

    Related topics

    • Strings, Reals & Special Types

    • Casting with $cast

    • Back to String and Casting Puzzles

    • SystemVerilog Language Puzzles section

    Previous

    29 / 293

    Next: putc/getc Index Semantics

    Up next

    Up nextputc/getc Index Semantics

    Sign in to save progress

    Practice this lessonPrevious lessonReport an error

    See also

    • Case-Sensitive vs Case-Insensitive Compare
    • Enum Safety with $cast
    • Upcast, Virtual Dispatch and Downcast