This is a great question. The reason why this confuses so many is that the rules for punctuating direct speech are for syntactic structures where the narrator's explanations about the speech are attached to or inserted into the speech:
Peter said: "I am Paul."
"I am Paul," he whispered.
"I am," he said, and dropped the gun in the river, "Paul."
In all these, the narrator comments are felt to be subordinate to the direct speech: the narrator reports dialog and explains it – who speaks, how they say it, and what goes on while they speak.
In the present example, on the other hand, the direct speech is subordinate to the narration: the narrator reports events and explains the events with a brief spoken phrase. Sentences such as those given in the question are no longer dialog, but become part of narration.
So how do we punctuate this? Like narration!
What still applies is the basic rule that direct speech is enclosed in quotation marks. That is necessary to differentiate
He greeted her with a happy "Hello".
He greeted her by nodding hello to her.
He did not greet her with his usual How are you?, but only nodded, then turned back to his work.
Nothing is said in the last two examples, so we do not use quotation marks here.
What does not apply to brief direct speech inserted into narration is the rule to set off direct speech from narrator comments with commas.
Punctuate (brief) direct speech inserted into narration like you would any other phrase. If you are unsure, try to replace the direct speech with a non-direct phrase. For example, there are no commas around
an answer in
The class chirped an answer in reply.
therefore no commas are needed around
The class chirped "Yes" in reply.
You may insert commas, if you want to signify pauses:
The class chirped, "Yes," in reply.
Both variants are possible and correct, and whether you prefer to enclose that chirped
"Yes" in commas or not will depend on your general use of commas: Is your writing comma-heavy, or do you use them sparingly? Use commas around inserted direct speech in accordance with your global comma style.
If your construction differs, you may have to use commas. When you insert the direct speech between clauses, that construction more strongly suggests the insertion to be set off with commas:
The class chirped, "Yes," and sat down.
and it appears at least slightly wrong without:
The class chirped "Yes" and sat down.
In your other example the direct speech is not merely inserted in place of a noun phrase (
an answer) but is nominalized and replaces the noun (
a soft "Come in" =
a soft voice; note the indefinite article before the direct speech, which turns the direct speech into a noun). In such a construction, you cannot use commas and they are wrong.
Just as there are no commas around
He gulped nervously when he caught a soft voice, feeling chills run down his spine ...
the commas would seem out of place around
"Come in" in
He gulped nervously when he caught a soft "Come in", feeling chills run down his spine ...
To summarize, direct speech, whether in dialog or inserted into narration, is always enclosed in quotation marks, but while narrator comments are set off with commas from dialog, direct speech inserted into narration is not set off at all because syntactically it becomes part of the narration.
So just as the full stop at the end of a sentence gets replaced with a comma in dialog (
"I am Paul," he said.), it gets replaced with nothing when direct speech is inserted into narration (
With a mumbled "I am Paul" he sat down again.). And just as the off-setting comma is replaced with an exclamation or question mark in dialog, the nothing that sets off inserted direct speech is replaced with the appropriate punctuation if the inserted direct speech is a question or exclamation:
He gulped nervously when he heard a shouted "Come in!", feeling chills run down his spine ...
He gulped nervously when he heard a soft "Who are you?", feeling chills run down his spine ...
Some of my examples are certainly a bit forced and might more elegantly be replaced by indirect speech or other constructions, but they are only meant to serve as illustrations, and no recommendation for or against their use is implied.