I have a Nikon D40. It has only 6.1 MP. I am planning to buy additional lens for it. Can changing the lens of the camera increase or decrease the megapixels of the camera?
The number of megapixels of a camera is determined by the resolution of the camera's sensor, which is part of the camera body. It cannot be altered by a lens.
As @ahockley said, the resolution (number of megapixels) is determined primarily by the sensor, so changing the lens doesn't change it.
Being fair, the resolution of the sensor is basically the theoretical maximum of which the system as a whole is capable. Although a better lens can't increase it, a really poor quality lens could reduce the effective resolution below that. In your case, however, that's fairly unlikely short of using a really poor quality lens. The reason for this is fairly simple: most lens designs can easily produce an image of higher resolution than the sensor you're using.
The times you could run into resolution limitations from the lens would be with a really low-end, off-brand lens -- if somebody's offering a 500mm lens for $50, chances are pretty good that it will be the limit on resolution. Likewise, if you hook the camera up to a low-end telescope or microscope, something in some other mount (e.g., C-mount) that was never designed for that size of sensor. That generally won't happen by accident though -- you'll generally have to find and such adapters to mount them on the camera at all.
A reasonably recent, undamaged lens from Nikon or any of the reasonably reputable 3rd party vendors like Sigma, Tamron, or Tokina shouldn't have much difficulty producing pictures at the full resolution of the sensor.
Though it doesn't apply in this specific case, there is one circumstance under which changing the lens can change the actual number of megapixels produced by the camera. In some cases, you can mount an APS-C lens on a full-frame camera, and when you do the camera will sense the lens you're using, and only use the part of the sensor that corresponds to APS-C framing. In this case, the actual sensed resolution is reduced.
To give a concrete example of that, I have a Sony Alpha 900 (full-frame) camera, and an 11-18mm APS-C lens. Although the camera's native resolution is ~24 MP (roughly 6000x4000 pixels), when I mount the 11-18 lens, the camera produces ~12 MP pictures (roughly 4200x2800 pixels). This is not, however, from the camera sensing the maximum resolution of which the lens is capable and acting accordingly. Rather, it's a simple matter of the lens having some memory on board that tells the camera it's an APS-C lens, so the camera acts based on that. Just for example, the particular lens I'm talking about was actually made by Tamron, and repackaged/sold by Konica-Minolta. The nearly identical lens that was sold under the Tamron name didn't have the same memory on it, so one of them mounted to the same camera will produce full-frame pictures (though the image circle is small enough that you get obvious vignetting from 11 to about 14 mm).
No, it does not. The resolution is a characteristic of the camera body and does not change with the lens.
However, there is one exception (which does not apply to you, since the Nikon D40 is a DX-format camera): if you mount an APS-C or DX format lens onto a full-frame or FX-format camera, the pixels outside the APS-C or DX area may not be usable due to vignetting, resulting to a loss of resolution due to cropping. On Nikon, it is possible to force FX format image capture with a DX-format lens, but this often results in severe vignetting that will force you to crop to a smaller image. On Sony, unless you are using a third-party lens, this is not an option and the 24-megapixel sensor on the A900 and A850 will be limited to the 11-megapixel APS-C crop area.
The part of your camera that will determine the number of megapixels is the sensor. You can think of this as a grid of pixels with the same dimensions as your cameras resolution. Your camera has a resolution of 3008 x 2000, So that's a grid with 3008 columns and 2000 rows (3008*2000 = 6016000 pixels, or approximately 6 megapixels). Each section of the grid (each pixel) records a value and saves it.
In order to lower the resolution your camera can throw away the data from some of the pixels on the outside. You could also use an algorithm to compress the image to a smaller size by doing something like replacing sections of pixels with the average values for those pixels. The first method would crop the outside of the image, while the second image will keep about the same overall image, but with a loss of detail.
Raising resolution is much harder. When we lower resolution we are just throwing away information we don't want anymore. In order to raise the resolution we need more information for the extra pixels we're adding. Advanced methods for this will do something of a best guess at what detail might have been there based on patterns in the image. Simpler methods will just take each pixel and spread it around, without actually adding any detail to the image.
The job of the lens is just to focus light onto the sensor. Better lenses do better jobs at providing the sensor with a crisp image. In the end though, the sensor will always chop it up into the same number of pixels.
An old thread I know, but maybe can help someone in the future.
The wider the lens, the less pixels available to cover an area.
For example, if you took a picture with a 50mm lens of say a person, you would use the majority of the pixels to render that person. From the same position and photographing the same subject with say a 24mm lens, you would now have that person covered by only 1/3 to 1/4 of the amount of pixels.
I do a lot of fine art landscape work and for the above reason I generally use a narrower lens, take multiple shots and stitch together.