Major scales and keys associated

D major has 2 sharp notes in it. This is due to the intervals between the notes of a major scale: W-W-H-W-W-W-H. This translates to a whole step (W) being a jump of 2 keys (1 black and 1 white), and half step (H) jumping from one key to another (black -> white / white -> black, or white -> white for E-F and B-C). However, there are 12 chromatic notes within an octave, so E-F and B-C are half steps.

So, when you apply the interval pattern for a major scale on D, you’ll find that the 2nd scale note, E to F, is only a half step. So, you go up another half step to satisfy the 2nd whole step, yielding E-F#. The same applies to B-C#.

The fact that C major is all on white keys doesn’t matter. What matters is the step interval between the notes of a scale, or number of white and black keys between the notes. Yes, you can have a scale that has all sharps (B# major is just C major, but written differently).