Since, both the trains move in same direction with same acceleration, therefore, their relative acceleration is zero. Initial velocity if train A is zero while that of train B is 40 ms-1, therefore, train A or observer is behind the train B or source when whistle is heard by passenger of A. Let this sound be produced at time t .
Considering motion of train B (sound source) upto this instant,
When sound was produced, train B (source) was at a distance SB from initial point while observer receives this sound at t = 20sec and at distance SA from initial point. It means that sound waves travel distance SB - SA in air and take time (20 - t) to travel it.