Given:
The potential energy \( U \) of a magnetic dipole in a uniform magnetic field is given by the formula:
\[ U = -MB \cos \theta \]
Since the magnetic moment is initially aligned with the magnetic field, \( \theta = 0^\circ \), and thus:
\[ U = -MB \cos(0^\circ) = -MB \]
Substitute the values:
\[ U = -5 \times 0.4 = -2 \, \text{J} \]
Thus, the potential energy of the bar magnet is -2 J.
When the magnet is turned by 180°, the angle between the magnetic moment and the magnetic field becomes \( \theta = 180^\circ \). The new potential energy is:
\[ U' = -MB \cos(180^\circ) = +MB \]
Substitute the values:
\[ U' = +5 \times 0.4 = +2 \, \text{J} \]
The work done in turning the magnet is the change in potential energy:
\[ W = U' - U = 2 - (-2) = 4 \, \text{J} \]
Thus, the work done in turning the magnet by 180° is 4 J.