Boris Johnson has defended his notorious comments about “tank-topped bum boys” and Muslim women looking like “letterboxes”, saying: “I defend my right to speak out.”

The prime minister faced a hostile audience over his past newspaper columns, with the accusation that he had “personally contributed” to rampant racism in Britain.

But he denied they were offensive – claiming they could only be “made to seem offensive” if taken out of context.

"If you go through all my articles with a fine toothcomb and pick out individual phrases, there’s no doubt that you can take out things that can be made to seem offensive,” he told the BBC Question Time audience.

Mr Johnson has been dogged by the articles he wrote as a journalist, including the recent revelation that he wrote that seeing “a bunch of black kids” scared him.

There was loud applause when the prime minister was accused of contributing to the problem of “racist rhetoric” being “rife in this country”.

He claimed: “I have written many millions of words in my life as a journalist and I have genuinely never intended to cause hurt or pain to anybody.”

But, when he suggested the questioner was “referring to a particular article” – in which he likened veiled Muslim women to letter boxes and bank robbers – the host, Fiona Bruce cut in.

“To be fair, there's a few articles,” she said. “So, there's the Muslims going around looking like letterboxes, which was last year, you referred to tribal warriors with watermelon smiles and flag-waving piccaninnies and then just to get another demographic in, tank-topped bum boys.”

The prime minister replied: “I defend my right to speak out.”

And, on the Muslim women article, he claimed: “What I was really doing was mounting a strong liberal defence of the right of women in this country to wear what they choose.”

But Andrew Gwynne, Labour’s co-campaign coordinator, said: “Boris Johnson's performance was appalling.

“He failed to take responsibility for the pain caused by austerity and shockingly defended his racist and homophobic comments.”

During the 30-minute grilling, the prime minister also defended the botched universal credit benefits system, insisting: “It has worked.”

And he came under fire over his claim that he had set aside the cash to build 40 new hospitals, Ms Bruce saying: “Prime Minister it's six, it’s six.”