Let $A = \{1,2,3\}$, $B = \{x,y,z,w\}$.
a) How many functions $f$ are there such that $f : B → A$ and $f(x) = 2$? I know the number of functions from $B$ to $A$ is $3^4$.
b) How many functions $f$ are there such that $f : A → B$ and $|f(A)| = 2$? The number of functions mapping $A$ to $B$ is $4^3$.
Is there a general formula to solve these questions?
There is a general formula, yes. Without any restrictions, there are $|B|^{|A|}$ functions from $A$ to $B$, since each of the $|A|$ elements in $A$ can be mapped to each of the $|B|$ elements in $B$.
Once you start putting restrictions on the functions, you'll likely have to find a custom solution, although some of the thinking will remain the same. For example, for functions from $B$ to $A$ with the restriction that $f(x)=2$ you basically fix the function value for $1$ of the elements of $B$, but since you still have total freedom for all others, you'd get that there are $|A|^{|B|-1}$ such functions.
Now, can you try and do the second question using this kind of thinking?