(a) It is clear from symmetry considerations that vector $\vec{E}$ must be directed as shown in the figure. This shows the way of solving this problem : we must find the component $d E_{r}$ of the field created by the element $d l$ of the rod, having the charge $d q$ and then integrate the result over all the elements of the rod. In this case
$$
d E_{r}=d E \cos \alpha=\frac{1}{4 \pi \varepsilon_{0}} \frac{\lambda d l}{r_{0}^{2}} \cos \alpha
$$
where $\lambda=\frac{q}{2 a}$ is the linear charge density. Let us reduce this equation of the form convenient
for integration. Figure shows that $d l \cos \alpha=r_{0} d \alpha$ and $r_{0}=\frac{r}{\cos n}$; Consequently, $d E_{r}=\frac{1}{4 \pi \varepsilon_{0}} \frac{\lambda r_{0} d \alpha}{r_{0}^{2}}=\frac{\lambda}{4 \pi \varepsilon_{0} r} \cos \alpha d \alpha$
This expression can be easily integrated :
$\alpha_{0}$
$E=\frac{\lambda}{4 \pi \varepsilon_{0} r} 2 \int_{0} \cos \alpha d \alpha=\frac{\lambda}{4 \pi \varepsilon_{0} r} 2 \sin \alpha_{0}$
where $\alpha_{0}$ is the maximum value of the angle $\alpha$, $\sin \alpha_{0}=a / \sqrt{a^{2}+r^{2}}$
Thus, $E=\frac{q / 2 a}{4 \pi \varepsilon_{0} r} 2 \frac{a}{\sqrt{a^{2}+r^{2}}}=\frac{q}{4 \pi \varepsilon_{0} r \sqrt{a^{2}+r^{2}}}$
Note that in this case also $E \sim \frac{q}{4 \pi \varepsilon_{0} r^{2}}$ for $r>>a$ as of the field of a point charge.
(b) Let, us consider the element of length $d l$ at a distance $l$ from the centre of the rod, as shown in the figure. Then field at $P$, due
if the element lies on the side, shown in the diagram, and $d E=\frac{\lambda d l}{4 \pi \varepsilon_{0}(r+l)^{2}}$, if it lies on
other side.
Hence $E=\int d E=\int_{0}^{a} \frac{\lambda d l}{4 \pi \varepsilon_{0}(r-l)^{2}}+\int_{0}^{a} \frac{\lambda d l}{4 \pi \varepsilon_{0}(r+l)^{2}}$
On integrating and putting $\lambda=\frac{q}{2 a}$, we get, $E=\frac{q}{4 \pi \varepsilon_{0}} \frac{1}{\left(r^{2}-a^{2}\right)}$
For $\quad r \gg>a, \quad E \sim \frac{q}{4 \pi \varepsilon_{0} r^{2}}$