Density is the mass of an object divided by its volume: d = m/v.
From the given variables above, it's d = 40g / 20cm3.
The object has a density of 2 grams/cm3.
Density is a fundamental concept in the sciences. It is used quite often in identifying rocks and minerals since the density of substances rarely changes significantly. For example, gold will always have a density of 19.3 g/cm3; if a mineral has a density other than that, it isn't gold.
Density is not something that is directly measured. Typically if you want to know the density of something you will weigh it and then measure its volume.
Another tricky thing about density is that you can't add densities. If I have a rock that is made up of two minerals, one with a density of 2.8 g/cm3, and one with a density of 3.5 g/cm3, the rock will have a density between 3.5 and 2.8 g/cm3, not a density of 6.3 g/cm3. This is because both the mass and the volume of the two minerals will be added, and so when they are divided to get the density the result will be between the two.
Typical densities for gasses are on the order of thousandths of grams per cubic centimeter. Liquids often have densities of about 1.0 g/cm3, and indeed, fresh water has a density of 1.0 g/cm3. Rocks often have a density around 3 g/cm3, and metals often have densities above 6 or 7 g/cm3.
Learn more about density at Carleton.edu.
See some worked density example problems at ThoughtCo.com.