As we know, by universal coefficient theorem, $H^{1}(X,\mathbb{Z})$ is torsionfree. My question is: for cup product $H^{1}(X,\mathbb{Z})\otimes H^{1}(X,\mathbb{Z})\rightarrow H^{2}(X,\mathbb{Z})$ could $a\cup b$ be a torsion element in $H^{2}(X,\mathbb{Z})$.

An example is given by a 3manifold. Specifically, for any $n$ other than $0$ or $\pm 1$ we can take the real Heisenberg group $G$ of $3 \times 3$ real matrices of the form $$ \begin{bmatrix} 1 & a & b \\\\ 0 & 1 & c \\\\ 0 & 0 & 1 \end{bmatrix} $$ where $a,b,c \in \mathbb{R}$. We then take the quotient $G/H$ by the subgroup $H$ of elements where $a,b,(nc) \in \mathbb{Z}$. These manifolds are $K(H,1)$ manifolds and the group $H$ is a nilpotent group with cohomology $$ \mathbb{Z}, \mathbb{Z}^2, \mathbb{Z}/n \times \mathbb{Z}^2, \mathbb{Z}. $$ The cup product of the two generators in degree one is the generator of the $\mathbb{Z}/n$ in degree two. This can be checked by using the LyndonHochschildSerre spectral sequence associated to the central extension $1 \to \mathbb{Z} \to H \to \mathbb{Z}^2 \to 1$, with a useful intermediate step being the determination of the abelianization of $H$. In some sense, these examples are universal, which can be seen using a little bit of homotopy theory. If you have two cohomology classes whose cup product is $n$torsion, then (by the correspondence between cohomology classes and maps to EilenbergMac Lane spaces) we have two cohomology classes $X \to K(\mathbb{Z},1)$ such that the composite $$ X \to K(\mathbb{Z},1) \times K(\mathbb{Z},1) \stackrel{cup}{\longrightarrow} K(\mathbb{Z},2) \stackrel{n}{\longrightarrow} K(\mathbb{Z},2) $$ is nullhomotopic. This means that it lifts to the homotopy fiber $$ F \to K(\mathbb{Z},1) \times K(\mathbb{Z},1) \stackrel{n \cdot cup}{\longrightarrow} K(\mathbb{Z},2).$$ This homotopy fiber $F$, in fact, is precisely the classifying space of the group $H$ (which is at least plausible from the long exact sequence of homotopy groups). 


Here's an example that's a 2dimensional CW complex. Start with a 0cell, then attach three 1cells labeled $a$, $b$, $c$ to get a wedge of three circles, then attach a 2cell via the word $aba^{1}b^{1}c^n$ for a fixed integer $n>1$. From the cellular cochain complex one then reads off that the resulting complex $X$ has $H^1X={\mathbb Z}\times{\mathbb Z}$ with generators $a$ and $b$ (by abuse of notation) and $H^2X={\mathbb Z}_n$. The claim is that $a\cup b$ is a generator of $H^2X$. To see this consider the quotient space of $X$ obtained by collapsing the 1cell $c$ to a point. This is a torus $T$ and the quotient map $X\to T$ induces an isomorphism on $H^1$ and a surjection on $H^2$, as one can see by looking at the induced map on cellular cochain complexes. In $T$ the cup product $a\cup b$ generates $H^2$ so the same is true for $X$ by naturality of cup product. When $n=2$ the complex $X$ is a closed surface since it's a hexagonal 2disk with edges identified in pairs. Its Euler characteristic is $1$ so it's the connected sum of a torus and the real projective plane. 


You can also make an example involving a closed surface. Let $X$ be the connected sum of a torus $T$ and a projective plane, and let $f:X\to T$ be nontrivial on $H^2$. Two elements of $H^1(T)$ whose cup product is nonzero mod $2$ will pull back by $f$ to two elements of $H^1(X)$ whose cup product generates $H^2(X;\mathbb Z/2)=\mathbb Z/2$. 

