any nikon lens will work with a d40.
the only issue is it has to have an autofocus motor in the lens for it to autofocus since there isnt one in the body. all dx lenses have this, though. if there isnt one, it'll still work fine, you'll just have to manual focus.
there's no perfect lens, it depends what you want to use it for.