Be aware that your lens choices for a D40 will be limited to those with a built-in focusing motor which, from what I understand, are typically quite expensive.
Edit: That is of course if you want the autofocus to still work
Lenses with built-in focus motors aren't all that much more expensive and using the term "quite expensive" is misleading, particularly when not backing up the statement with any empirical data.
Note - Many of the lenses that have a focus motor in them also have Nikon's VR technology adding to the price of a lens, which fokker negelects to mention.
Further, a D40 owners lens choices
are not, "limited to those with a built-in focus motor."
D40 owners can choose from most of the lenses Nikon has made for it's F-mount since it was introduced 50+ years ago in 1959. Not all of those lenses will auto focus and not all of those lenses will meter, but they can be used on a D40.
In Nikon's current lens lineup a D40 owner has 33 lenses to choose from that have built-in focus motors (AF-S) and only 21 that don't that can still auto focus on oother Nikon camera bodies (AF). D40 owners can also look to the previous generation, AF-I lenses that have a focus motor in the lens. As mentioned above, D40 owner would need to manually focus an AF lens mounted to the camera.
Of the current 21 lenses that don't have a focus motor, they range in price from
$134.95 (AF 50 mm f/1.8D) to
$1,894.95 (AF 14 mm f/2.8D). (all prices are from Nikon's USA web site)
Of the 33 AF-S lenses that do have a focus motor in them the least expensive is the
AF-S 18-55mm f/3.5-5.6G ED II (no VR) at $119.95 and
is Nikon's least expensive lens. Like the AF lenses, AF-S lenses vary in price by focal length and maximum aperture with the long telephoto primes exceeding $10,000 in price.