Yes, an i5-6400 will work with your OptiPlex 3040 SFF.  
Is it possible to upgrade the Intel core i3 6100 in a Dell Optiplex 3040 SFF to a Intel core i5 6400?
Dell Optiplex 3040 SFF Intel Core i5 6400 Upgrade