Why do men tell sexist or offensive jokes, particularly at the most inappropriate times?

What is the real psychology behind this behavior? I see it play out as either: 1) social hierachy and approval testing, eg- jokes told by men in groups only with other men, or 2) attention-seeking, eg- jokes in multi-sex groups (at inappropriate times), where the guy desparately (?) wants some attention brought back his way because he feels unnoticed or left out. Thoughts?
Why do men tell sexist or offensive jokes, particularly at the most inappropriate times?
Post Opinion