In an emergency such as a fire you want to be able to escape from a building quickly and be assured that there's plenty of water to fight the flames. I presume these capped pipes have something to do with that purpose?
I believe those are standpipes. Rather than dragging a hose up flight after flight, tall buildings have empty pipes that the fire trucks can connect to.