Android Nexus 5 knows password of WiFi he was never connected to!
Did you backup/restore your settings to/from Google? If so, that's why you can connect to these wireless networks. One of the things that gets backed up is the wireless network passwords so when you restored them back on your new device it is able to connect.

Android has built in backup functionality. It offers to backup settings (including wifi settings) automatically from each device. When you go through the set-up on a new device it then gives you the option to restore these settings. If you had this configured (I think it's enabled by default) then your wifi settings would have automatically been backed up on your Nexus 4 and restored to your Nexus 5.