What plant is this
As benn notes, this is not a cactus: it belongs to the very large and diverse Euphorbia genus which includes the spurges, milkweed, poinsettia, crown of thorns and - of relevance to your question - a large number of plants that look a bit like cacti.
The particular plant in your photo may possibly be the species suggested E. mammillaris, also known as the Indian Corn Cob. A distinguishing feature is the little knobs which give it a corn cob appearance (in fact, the Latin name comes from mammilla meaning breast or nipple!). There's also a very popular variegated variety.
However, it's worth noting that E. mammillaris falls within a group of closely-related euphorbias that exhibit quite a lot of variation even within the same plant, and it's not always easy to tell them apart. This is especially true if the plant has been grown indoors, as the colour may be greener and the growth faster but a bit on the spindly side. Within that group, I suspect your plant is more likely to be E. ferox or possibly E. enopla. These two species are almost identical, but this site suggests that ferox tends to have "fatter" stems than enopla.
An important warning: like most other euphorbias, your plant contains a milky latex-like sap that is very poisonous. Take particular care when handling your plant (e.g. repotting it), and especially avoid getting any of the sap in your eyes.
Care for your plant
Your plant will grow best if it gets full morning sun but afternoon shade and (if outdoors) is protected from frost; it can cope with hot afternoon sun or even all-day sun but the stronger the light, the more the colour will tend to darken and it may grow more slowly. Indoors, it needs strong light: the less light it gets, the spindlier it will grow.
Benn's advice on watering is misguided. Many cacti and succulents come from tropical zones where there's no rain at all during the dry season (which is why they've evolved to store water within fleshy leaves or stems), but during the wet season there's a tropical downpour every day to go with the super-strength sunlight. Cacti and their mimics (like your plant) love water during the bright hot growing season but remember that most live in rocky or gravelly "soils" or on steep hillsides where the water quickly drains away.
The key to watering is to adjust it according to the amount of light your plant is getting. From early spring to late autumn, a full day in the hot sun deserves a watering; water once every two hot sunny days if it's in partial shade; halve this rate if it's indoors. From late autumn to early spring, water just once every four weeks if indoors and not at all if it's outdoors and gets at least some rain.
You won't hurt your plant if you give it less water than the above, but it won't grow as strongly. However, you can guarantee the death of your plant if you allow its roots to stay wet for days at a time, either by overwatering it or by preventing the water from draining off (e.g. because it's standing in a saucer, or because it's been potted in a heavy soil or with too much compost).