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