Answer
$y(x)=c_1x^{-1}+c_2x^{-2}-x^{-2}\cos x$
Work Step by Step
Given $x^2y''+4xy'+2y=\cos x$
In this case the substitution $y(x) = x^r$ yields the indicial equation
$$r(r-1)+4r+2=0\\
r^2+3r+2=0\\
(r+1)(r+2)=0$$
It follows that two linearly independent solutions to the given differential equation are
$u_1(x)=x^{-1}\\
u_2(x)=x^{-2}$
so that the general solution is
$y(x)=u_1(x)x^{-1}+u_2(x)x^{-2}$
where $u_1$ and $u_2$ are determined from
$u_1'(x)x^{-1}+u_2'(x)x^{-2}=0\\
u_1'(x)(x^{-1})'+u'_2(x)(x^{-2})'=x^{-2}\cos x$
Hence,
$u_1'=\cos x\\
u_2'=-x\cos x$
which upon integration gives
$u_1(x)=\sin x\\
u_2(x)=-x\sin x-\cos x$
Thus, the given equation has general solution
$y(x)=c_1x^{-1}+c_2x^{-2}-x^{-2}\cos x$