Let me tweak and discuss a bit the answer of @Prasst, to make sure the student does not simply shrug and walk away because the issue is moot and problematic as per @Valter_Moretti 's answer and @Emilio's link. There is always a whiff of "don't go there!" in such questions, but the better message might be "watch where you are going!". Indeed, you have to fudge the definition of the nonexistent momentum and marvel at where the formal steps take you.
For the first excited state in question, it pays to shift the interval $[0,a]\mapsto [-a/2,a/2]$,
$$\langle x|\psi\rangle=\psi_1(x)=\sqrt{\frac{2}{a}}\cos\left(\frac{\pi}{a}x\right)$$
which is now manifestly symmetric around the origin in x, where it peaks.
The corresponding momentum-space wave function would then be real and symmetric in p,
$$\langle p|\psi\rangle=\phi_1(p)= \frac{2 \sqrt{\pi a/\hbar}}{\frac{p^2a^2}{\hbar^2}-\pi^2}~\cos\left(\frac{pa}{2\hbar}\right).$$
So p takes any value on this state, not just the privileged values $p=\pm \hbar \pi/a$ suggested by the energy eigenvalue: It is a wavepacket involving all momentum components! (Contrast this to a plane wave $\psi(x)\sim \exp(ikx)$, so $\phi(p)\sim \delta(p-\hbar k)$, so that $p~\phi(p)=\hbar k~\phi(k)$.)
One sees that
$$
\phi_1(0)=- \frac{2 \sqrt{\pi a/\hbar}}{ \pi^2}, \\
\phi_1\left (\pm \frac{\hbar \pi}{a}\right )=-\frac{ \sqrt{\pi a/\hbar}}{ 2\pi}.
$$
From symmetry, the expectation of $\hat p$ in this state vanishes, while the expectation of $\hat{ p}^2$ is the energy-privileged value at the peaks, $(\hbar \pi/a)^2 $.
The deep answer above and links clue you into this peculiarity.
- NB Several of these peculiarities, however, are obviated and bypassed when using the matrix mechanics formulation, which is generically more robust and elegant than the wave picture (but usually technically messier). Admire the spare simplicity of the momentum matrix in Prentis & Ty, Am Jou Phys 82, 583 (2014), which, unlike its square, is not diagonal. Further note the first energy eigenvector is not its eigenvector, as well.