When are elements heavier than iron formed by stars?

In small, old stars

as a dying star explodes

as a star is first formed

in young stars

Elements heavier than iron are formed in two main scenarios: during the explosive death of a star (supernova) and during the formation of a star. However, the majority of elements heavier than iron are formed during supernovae, specifically during the explosive collapse of massive stars. These supernovae occur as dying stars explode, releasing a tremendous amount of energy and synthesizing heavier elements through nuclear reactions. So, the correct answer would be: as a dying star explodes.