I think the key is to do just the right amount of exercise and do it routinely enough to know how to do it safely and not get injured. Lots of old people in my family, some who had desk jobs, some who were on their feet all day, and the knee/hip problems seem to come mostly with osteoporosis. I’m related to some very sedentary old ladies who had knee replacement or hip replacement surgery (or both) (and why they needed it I have no idea, as one in particular does nothing except sit in a massaging Barcalounger watching TV 14 hours a day, then she goes to bed). The old people I know who have good knees/hips have always been at least moderately active, up to extremely active (tennis, golf, skiing–yes, even skiing!–bowling, mountain climbing, gardening) and the ones who were debilitated to the point of needing (or thinking they needed) surgery tended to be overweight, not very active, hired other people to do their housework/mow their lawns, drove three blocks to the grocery store, etc.
Obviously this is anecdotal and is not data, genetic makeup is important, and YMMV.