Well, let's see:
If you live 'D' kilometers from the city, and you go and return by the same route, then
-- The time it takes you to reach the city, at 40 km/h, is D/40 hours.
-- The time it takes you to return, at 60 km/h, is D/60.
-- The total distance you cover is 2D, and the total driving time is
(D/40) + (D/60)
= (3D/120) + (2D/120)
= 5D/120 = D/24
-- Your average speed for the entire trip is
(distance covered) / (time to cover the distance)
= ( 2D ) / ( D/24 )
= 48 km/hr.