Let us assume the number of gallons consumed by car A = x gallons
Let us assume the number of gallons consumed by car B = (35 - x) gallons
Then
Distance covered by car A = 40x
Distance covered by car B = 15(35 - x)
So
40x + 15(35 - x) = 1025
40x + 525 - 15x = 1025
25x = 500
x = 20
Then
Amount of gas consumed by car A = 20 gallons
Amount of gas consumed by car B = 15 gallons.