In general, the surface charge density $\sigma$ of a polarized material with polarization $\mathbf{P}$ is indeed given by $\sigma=\mathbf{P}\cdot\hat{\mathbf{n}}$, where $\hat{\mathbf{n}}$ is the surface normal vector.
For a sphere, we have $\hat{\mathbf{n}}=\hat{\mathbf{r}}$ in spherical coordinates. So, assuming that $\mathbf{P}$ points in the $z$-axis, we obtain $$\sigma=\mathbf{P}\cdot\hat{\mathbf{r}}=P\hat{\mathbf{z}}\cdot\hat{\mathbf{r}}=P\cos{\theta}.$$
For the next case, we have a spherical shell with inner radius $a$ and outer radius $b$, with polarization $\mathbf{P}=\frac{k}{r}\hat{\mathbf{r}}$. Applying the general formula, we get
$$\sigma=\mathbf{P}\cdot\hat{\mathbf{r}}=\frac{k}{r}\hat{\mathbf{r}}\cdot\hat{\mathbf{r}}=\frac{k}{r}.$$
Letting $r=a$ and $r=b$ gives the surface charge density for the inner and outer surface respectively.