(i) Glyceraldehyde, HOCH2-CH(OH)-CHO, has one asymmetric carbon atom, so it exists as two enantiomers: non-superimposable mirror images. P and Q are this pair.
With −CHO at the top, P has the −OH on the right, so P is D-glyceraldehyde, and Q, with −OH on the left, is L-glyceraldehyde.
NCERT: the (+) isomer of glyceraldehyde has the D configuration. So P rotates plane-polarised light to the right, D-(+)-glyceraldehyde, and Q is L-(–)-glyceraldehyde. This is a measured fact for glyceraldehyde; the letter D does not by itself predict the sign of rotation of other sugars.
(ii) The asymmetric carbons of glucose are C2, C3, C4 and C5. For monosaccharides it is the lowest asymmetric carbon atom, C5, that is compared with glyceraldehyde (with the most oxidised carbon, −CHO, written at the top). In D-(+)-glucose the C5 −OH is on the right, as in D-(+)-glyceraldehyde, so glucose is D.

The other asymmetric carbons are not used because the D/L label is defined by comparison of one reference centre only; the configurations at C2, C3 and C4 distinguish glucose from other D-aldohexoses but do not decide the series.