Because of the Chinese Remainder Theorem every polynomial $P$ in $\mathbb Z_{12}$ corresponds to a pair of polynomials $(Q,R)$ such that $Q$ is a polynomial in $\mathbb Z_3$ and $R$ is a polynomial in $\mathbb Z_4$ (the correspondence is just done coordinate by coordinate).
The number of roots of the polynomial $P$ is equal to the product of the number of roots of $R$ and $Q$. So if we want $P$ to have $2$ roots we require that one of $R$ and $Q$ has $1$ root and the other has $2$ roots. So in order to count how many polynomials $P$ have $2$ roots we can count the number of polynomials with $1$ and $2$ roots in $\mathbb Z_4$ and $\mathbb Z_3$.
In $\mathbb Z_3$ the constant polynomials all have $0$ or $3$ roots so they don't matter to us.
The polynomials of degree $1$ all have $1$ root.
To approach the polynomials of degree $2$ we can study $x^2+bx$ and see the possible sizes for the fibers:
If $b=0$ then $P$ is $0$ at $\{0\}$ and $1$ at $\{1,2\}$.
If $b=1$ then $P$ is $0$ at $\{0,2\}$ and $2$ at $\{1\}$.
If $b=2$ then $P$ is $0$ at $\{0,1\}$ and $2$ at $\{2\}$.
The case $2x^2+bx$ is the same since multiplying the polynomial by $2$ preserves structure.
Therefore the number of polynomials with $1$ root is $6+2(3) = 12$.
Therefore the number of polynomials with $2$ roots is $0+2(3) = 6$
In $\mathbb Z_4$ The constant polynomials all have $0$ or $4$ roots so they don't matter to us.
The polynomials of degree $1$ $bx+c$ in which $b$ is odd all have $1$ root. When $b=2$ there are two with $2$ roots.
The polynomials of degree $2$ can be understood in the following way:
Look first at the polynomial $S=ax^2+bx=(ax+b)x$ and analyze how much it contributes to each quantity when we consider the $4$ values of $c$. (to do this we just have to see how many fibers of size $1$ and $2$ each of the $12$ combinations has).
Case $1$: $a=2$
If $b$ is $0$ then $P$ is $0$ at $\{0,2\}$ and $2$ at $\{1,3\}$
If $b$ is $1$ then $P$ is $0$ at $\{0\}$, is $3$ at $\{1\}$, is $2$ at $\{2\}$ and is $1$ at ${3}\$
If $b$ is $2$ then $P$ is $0$ at $\{0,2\}$ and $2$ at $\{1,3\}$
If $b$ is $3$ then $P$ is $0$ at $\{0\}$ is $1$ at $\{1\}$ is $2$ at $\{2\}$ and is $3$ at $\{3\}$.
Case $2$: $a=1$ or $3$.
Both of these cases are the same because multiplying $ax^2+bx$ by $3$ is a bijection between the two cases that preserves fiber sizes, so we assume $a=1$.
If $b$ is $0$ then $P$ is $0$ at $\{0,2\}$ and $1$ at $\{1,3\}$
if $b$ is $2$ then $P$ is $0$ at $\{0,2\}$ and $3$ at $\{1,3\}$
If $b$ is $1$ then $P$ is $0$ at $\{0,3\}$ and $2$ at $\{1,2\}$
The case $b=3$ is the same as the previous one.
Hence we have the following:
There are $6$ polynomials with $1$ root in $\mathbb Z_3$
There are $18$ polynomials with $2$ roots in $\mathbb Z_3$
There are $[8+0] + [0+4+0+4 + 2(0+0+0+0) ]=8+8 = 16$ polynomials with $1$ root in $\mathbb Z_4$
There are $[0+2] + [2+0+2+0 +2(2+2+2+2)] = 2+18 = 20$ polynomials with with $1$ root in $\mathbb Z_4$
Hence the number of polynomials of degree at most $2$ with exactly $2$ roots in $\mathbb Z_{12}$ is $12\cdot20 + 6\cdot16 = 336$.
Code to check the final result and intermediate results are all correct:
#include <iostream>
using namespace std;
int numpols(int n, int r ){ //calculates the number of pols in Z_{n} with r roots.
int res = 0;
for(int a=0;a<n;a++){
for(int b=0;b<n;b++){
for(int c=0;c<n;c++){
int sols = 0;
for(int i=0;i<n;i++){
if( (aii + b*i + c)%n == 0) sols ++;
}
if( sols == r) res ++;
}
}
}
return res;
}
int main(){
cout << numpols(3,1) << endl;
cout << numpols(3,2) << endl;
cout << numpols(4,1) << endl;
cout << numpols(4,2) << endl;
cout << numpols(12,2) << endl;
}