The aperture is f/22 when you take the camera off the lens because the aperture ring on the lens is set to f/22. However, when you have the lens attached to the D3100 and set to f/1.8, the photograph will be taken at f/1.8. When it comes to automatic cameras, the state of the 'aperture hole' when the lens is off the camera has absolutely no bearing on what the lens does when a photo is being taken. For example, the resting state of the G lenses is the maximum aperture when not connected to the lens #tiny hole#.
#this may confuse you [if it does, just pretend you didn't read it]# Another tidbit of info is that regardless of what you have the aperture set to, when you are looking through the viewfinder, the aperture is always as wide as the lens can go--it only closes to, say, f/11 for the period of time that the photo is being taken.
Also, a great way to solve a problem like this by yourself would be to take a picture with the aperture set at f/22 and one with the aperture set at f/1.8, and if there is a drastic difference between the two photographs, you can deduce that your camera is, in fact, controlling the aperture.