SystemVerilog OOP Mastery · All levels

Copy, Cast & Randomization: Tricky Q&A

Rapid-fire interview Q&A for Copy, Cast & Randomization.

Section Q&A

Rapid-fire interview questions for Copy, Cast & Randomization. Answer out loud, then check yourself.

Q1. What is the semantic difference between dst = src and dst = new src for class objects?

Answer: dst = src aliases the same object handle, while dst = new src creates a new top-level object with member-wise copy. Nested class handles can still alias after new src unless explicitly deep-copied.

Trap: Thinking new src guarantees full deep copy of all sub-objects.

Q2. Why is a virtual copy plus clone pattern preferred in verification class hierarchies?

Answer: copy centralizes field transfer and inheritance layering, while clone guarantees allocation of a fresh object. Together they avoid aliasing and keep polymorphic APIs consistent across base and derived types.

Trap: Implementing clone by returning this or by shallow handle assignment.

Q3. Why must downcasting use $cast even when the assignment compiles?

Answer: Compile-time type compatibility does not prove runtime dynamic type. $cast checks the runtime object and returns success/failure so derived-only access stays safe.

Trap: Assuming any base handle can be directly treated as a specific derived handle.

Q4. How do rand, randc, and randomize() with complement each other?

Answer: rand gives solver-driven random values, randc cycles through a domain before repeating, and randomize() with adds temporary per-call constraints on top of class legality constraints.

Trap: Using randc as if it means never repeat across all simulation time.