If it has Sturdy, it is unaffected by OHKO moves.
Generations III and IV
Sturdy makes the Pokémon unaffected by one-hit knockout moves.
Generation V onward
In addition to Sturdy's previous effect, Sturdy now works like a Focus Sash, causing the Pokémon to have 1 HP remaining if another Pokémon's attack or confusion damage would have brought it from full health to 0 HP.
Source
Focus Sash also prevents OHKO moves.
If the holder has full HP and is hit by an attack that would otherwise cause fainting (including a one-hit knockout move), it survives with 1 HP.
Source
As for Affection, it is currently unknown whether or not Affection can stop an OHKO move. Affection can sometimes guarantee an effect like Focus Sash, but for OHKO moves specifically I can't find any sources that indicate if that is the case. If you guys know the answer to this please tell me.
And, for clarificstion, the only OHKO moves at the time of writing this are:
- Horn Drill
- Guillotine
- Sheer Cold
- Fissure