Answer to your original question:
If two switches are a part of different subnets, will they exchange
messages if directly connected?
Switches don't look beyond the layer-2 frame to the layer-3 addresses to make decisions on where to switch frames, so the switches play no part in your scenarios, other than to switch frames to the ports where the destination layer-2 addresses are connected.
What you need to look at is how the hosts are configured. A host will compare its network with the network of the destination address to see if they are the same. If they are in the same network, according to the mask configured on the interface, the host will ARP for the destination layer-2 address, or get it from its ARP cache. If the destination address is on a different network, the host will send the packet to its configured gateway.
Answer to your new question (please, do not change your question after you have answers to the original question):
IP connectivity between hosts with differing subnet masks?
If one of the networks is contained within the other network, there are two possible answers:
- If both host addresses fall within the range of the smaller network, then
there will be no problem communicating from either host to the
other. A ping from one host to the other will succeed.
- If one of the host addresses is not within the smaller network, then the host
from the larger network can send something to the host in the
smaller network, but the host in the smaller network will send
anything destined for the host in the larger network to its
configured gateway because it knows that it is in another network.
By default, a ping from one host to another will fail because the host in the smaller network will send a ping request or ping reply to its configured gateway, but a router
may possibly be configured to send an ICMP redirect to tell the host in the smaller network to send it directly to the other host.
If you have hosts in two completely separate networks, any packets destined from one host to the other will be sent to the configured gateway of the sending host.