I seriously doubt this student "only knows how to produce a particular answer for a particular question". Are you claiming he could compute 3 times 5 but not 5 times 3?
On the contrary, I think this student may be showing that he knew the two techniques, and that he is smart enough to pick the easier computation.
But yes, if your goal is to kill any creativity in intelligent should punish kids that deviate from the lines hard.
If a teacher asked "compute 1000 x 1", no sane kid would do "1 plus 1 equals 2; 2 plus 1 equals 3;...; 999 plus one equals 1000".
This teacher would have failed Carl Friedrich Gauss, too, for computing sum(1,100) in seconds.