5th dnd – Can a handy monster attack a character in the space behind another character?

A monster has a ranged attack of 10 feet, and has two characters in that range, one within 5 feet and the other less than 10 feet away.

Can this monster perform a melee attack against the second character?

Is there a difference if it is an opportunity attack?