the molecular formula of an unknown hydrocarbon
$43\ \mathrm g$ of carbon for $7.2\ \mathrm g$ hydrogen.
Molar mass of carbon is $12\ \mathrm{g/mol}$ and that of hydrogen is $1\ \mathrm{g/mol}$.

Hence there are $\frac{43\ \mathrm g}{12\ \mathrm{g/mol}} = 3.58\ \mathrm{mol} \approx 3.6\ \mathrm{mol}$ of carbon for every $\frac{7.2\ \mathrm g}{1\ \mathrm{g/mol}}=7.2\ \mathrm {mol}$ of hydrogen.

Hence the empirical formula of the compound is $\ce{CH2}$.

Relative molecular mass of $\ce{CH2}$ is $12+2\times1 = 14$. Given relative molecular mass is $42$.

$\displaystyle\frac{42}{14}=3$

Hence the formula of the compound is $\ce{C3H6}$