Does experience with an locked weapon count when weapon is eventually unlocked?

The experience starts summing up from the moment you unlock the weapon.

You can easily check this by yourself: find a popular weapon you don't have unlocked yet dropped by some dead guy on the battlefield, and try to get 3-4 kills with it before you actually unlock it: these should provide enough experience to bring it to Tier 1 specializations. 3 are usually enough with all the bonus points kill gives, but do 4 just to be sure.

When you finally will unlock the weapon, go checking the experience in the weapon tab: it will be set to zero, and you will not have any of the specialization unlocked. Check again by doing at least 4 kills: you should now see the first specialization getting unlocked for the first time.

Source: Personal Game Experience, checked with mg-42, stg-44 and Kark98