The relative formula mass (Mr) of a substance is found by adding up the relative atomic masses (Ar) of all the atoms in its formula. You are given the Ar values in the exam, for example H = 1, C = 12, O = 16, Mg = 24 and Ca = 40.
Take calcium carbonate, CaCO3. It has one Ca, one C and three O atoms, so Mr = 40 + 12 + (3 × 16) = 40 + 12 + 48 = 100. If the formula has brackets, multiply everything inside them by the number outside. For Ca(OH)2, Mr = 40 + 2 × (16 + 1) = 74.
To find the percentage by mass of an element in a compound, use: (Ar × number of atoms of that element ÷ Mr of the compound) × 100. In magnesium oxide, MgO, the relative formula mass is 24 + 16 = 40, so magnesium is 24 ÷ 40 × 100 = 60% by mass. In CaCO3, oxygen is 48 ÷ 100 × 100 = 48% by mass.