The fist ball is thrown up ward with avelocity of 20m/s at the same instant. The second ball is dropped at 40m hight from the first ball at what time the two ball collide each other?

They collide when Y1 = Y2. Those are the heights of the two balls, as measured from the a defined "zero" height location.

Y1 = 20 t - 4.9 t^2
Y2 = 40 - 4.9 t^2

When Y1 = Y2,
20 t = 40
t = 2 seconds
Y = 40 - 4.9 t^2 = 20.4 m above the ground