This is such a silly example, but I always think of Hatoful Boyfriend for this, because it's in(?)famous for an unexpected tone shift, but it's also loved for it, so it seems like it did something right. And I think the main thing is that as you play through the silly, jokey part of the game, there's always hints at something darker -- there's something sinister about the doctor, there's rumours of students disappearing -- and by the time you find the huge startling tone shift, you've been actively trying to learn, what's really going on?
Basically, as the story begins, it begins to promise something else later on -- so that by the time you get there, yeah, it's surprising how completely the tone shifts, but it feels like a reveal of something that had already been promised to you, rather than an out-of-nowhere ""twist"" that breaks its promises.
(this is a minor semantic quibble but -- exposition isn't bad!! Exposition just means delivering information and it's a necessary part of any story. Over-explaining and walls-of-text info-dumping and introducing a lot of sudden information that wasn't naturally set up earlier are all examples of POORLY DONE exposition, and those are the things you want to avoid! but please don't avoid exposition.... trying to avoid any explanations -- meaning no one really understands the stakes to begin with and can't engage with the story -- can make just as big a mess as overexplaining)
This isn't really "going out of character," though; that's the thing. If you have a character who would never want to lie to his family, but some villain has told him that his family will die unless he keeps their operation quiet -- it's not "out of character" for that guy to want to protect his family. In fact, that conflict, in seeing which option wins out of two things the character doesn't want, tells you a LOT about the character.
Being "out of character" is a different thing -- when you have a story where a character acts one way, and then does something that doesn't match because it moves the story forward, without any character development -- that's usually not great. For example, the cautious character who would never get into a fight could be forced into it against his will, or could have reasons that motivate him to do something he would normally avoid, and that's fine. But if that character suddenly picks a fight because the story needs them to get kicked out of the tavern, it's really easy to just write that archetypal scene rather than thinking about what That Specific Character Would Do. That can make the whole story feel more shallow, because suddenly the character doesn't feel like a specific person, but a string of archetypes.