In two hours first car has travelled a distance of 2×40=80 km towards north and the second car would have travelled 2×30=60 km towards east.
The shortest distance between them can be calculated using Pythagoras theorem.
AB2=OA2+OB2
AB2=(80)2+(60)2=6400+3600=10000
AB=100 km.