This is a classic trick question that works best when you break it down step by step.
Sheep: Each sheep has four feet. With forty sheep, that is 40 × 4 = 160 feet.
Shepherd: One shepherd has two feet.
Dog: One dog has four feet.
Total: 160 + 2 + 4 = 166 feet.