David Cameron may struggle at passport control when he returns home from his trip to the US and Brazil after fluffing a mock citizenship test on the Late Show with David Letterman.

"You have found me out. That is bad, I have ended my career on your show tonight," the prime minister said, after struggling when Letterman fired a series of questions at him.

Shifting slightly awkwardly in his seat, Cameron first ran into trouble when Letterman asked him who composed the music for Rule Britannia after the band struck up the music from the Last Night of the Proms. "Elgar," he said uneasily. "You are testing me," he added.

Letterman waited until near the end of Cameron's appearance to point out that Thomas Arne composed the music, and that the poem was by James Thomson.

Letterman picked on Rule Britannia to remind the prime minister that Britain once had an empire on which the sun never set. Cameron showed a better grasp of American history as he rattled through Anglo-American relations. "We had a bit of a falling out, [though] I think we are getting over that," the Oxford PPE graduate said. "We interfered in your politics 200 years ago when we sailed up the river and burnt the White House."

Cameron asked to become the first sitting prime minister to appear on the Letterman show, recorded at the legendary Ed Sullivan Theatre on Broadway, to fly the flag for British business. He told Letterman he was "shamelessly" exploiting the afterglow of the Olympics and Paralympics.

Cameron retrieved his honour when asked to explain the difference between Northern Ireland and the Republic of Ireland. He summed up the partition of Ireland in a few seconds and went on to praise the Queen's visit to Dublin last year. He made one minor slip when he said she was the first member of her family to visit since independence. The Prince of Wales has visited on numerous occasions.

The interrogation turned to Magna Carta. "1215," he shot back when asked the date it was signed. It took him a few minutes to give the venue – Runnymede – before delivering a first class essay about its significance in checking the powers of the English crown.

But he did not know the English for Magna Carta. Cameron's university chum, Boris Johnson, the mayor of London, who has appeared on the show, will no doubt be chuckling. Letterman eventually ended Cameron's agony, explaining it meant Great Charter.

Letterman's producers gave the show a southern English middle class theme, providing the US audience with a guide to the British class system. The other guest was the British actor Jonny Lee Miller, who made his name in Trainspotting. Miller, the former husband of Angelina Jolie, is from Kingston, Surrey, and was educated at Tiffin, a local grammar school.

Mumford & Sons, the folk rock band from Wimbledon, offered a song from their new album Babel. Marcus Mumford, the lead singer, was educated at King's College School. As an Etonian, Cameron represented a higher step on the ladder of the English private school system.

Cameron, who ran rings round Jeremy Paxman during the 2005 Tory leadership contest, took care to prepare for his encounter with Letterman. One joke doing the rounds was that Sir Kim Darroch, Britain's national security adviser who has a quick and dry humour, would stand in for Letterman in rehearsals.

In the end the Cameron prep team, led by his communications director and former television news editor Craig Oliver, fired a series of questions at him. Top of the list were queries about the Queen after the Letterman team told No 10 they were fascinated by the British monarch after her walk-on part in the James Bond section of the Olympics opening ceremony.

The Cameron team studied with care Johnson's appearance on the Letterman show in June, when the mayor was asked how long he has been cutting his hair. "That's a low blow," New York-born Johnson replied with a smile.

Barack Obama was given a reasonably easy ride on the show this month when he appeared after the leaked video in which the Republican presidential challenger, Mitt Romney, appeared to write off 47% of the US electorate. The prime minister watched a recording of Obama's appearance as part of his preparation.

One senior British source said: "It is just a good opportunity. Britain has had an extraordinary year with the Jubilee, with the Olympics and with the Paralympics. We have got something good to shout about. So getting out there talking up Britain is important.

"We should never forget that we are the biggest investor in America, America is the biggest investor in us. There is a saying in business that when you want to do better you go back to your biggest customers and try and squeeze out a bit more."

Cameron sought to show a modern Britain. Letterman did his best to counter that in laying the ground for Cameron's appearance by releasing dry ice to depict "London fog".