The Illinois senator then broadened the issue to say that America's treatment of the poor was one of the country's biggest moral failings.
Answering the same question, McCain, who grew up Episcopalian but now attends an evangelical Southern Baptist church, said: "My greatest moral failing, and I am a very imperfect person, is the failure of my first marriage."
"America's greatest moral failure has been throughout our existence, perhaps we have not devoted ourselves to causes greater than our self interest," he said about America's failings
Evangelicals account for one in four US adults and have been a significant conservative base for the Republican Party, with a strong focus on opposition to abortion and gay rights and the promotion of "traditional" family values.
The two candidates differed sharply on abortion with Obama saying he supported a woman's right to choose but suggesting that he would limited abortion in the late stages of pregnancy as long as there were exceptions protecting the mother's health.
McCain contended that a baby's right to life began "at conception".
"I will be a pro-life president and this presidency will have pro-life policies," he said.
McCain also repeated his support for stem cell research, an issue that has seen him criticised by conservative Christians in the past.Gay rights
Both men expressed the view that marriage is a union between a man and a woman, but Obama said that he supported civil unions for gay partners, giving them rights such as hospital visits with one another.
|Obama, right, acknowledged experimenting with drugs in his youth [EPA]
Obama, a Christian who would become the first black president if elected in November, has repeatedly countered rumours that he is Muslim and he also distanced himself from a controversial former pastor.
"The hardcore Christian conservatives know they are not going to vote for Obama, there is nothing that Obama could have done tonight that would have made them vote for him," Glenn Sacks, a Los Angeles-based political columnist and radio commentator, told Al Jazeera.
"But I thought it was a good showing and there a certain amount of the conservative Christians who are disaffected with [President] Bush, they are disaffected with the Republican administration, they want to see some change ... and Obama probably did enough that some of those people will support him."
Although the session was presented as a forum on faith the discussion also covered familiar campaign issues, such as Iraq, national security and the economy.
The forum came as the two candidates prepared for their respective party conventions, where each is set to be officially confirmed as their party's candidate. The Democrats will meet on August 25-28 in Denver, Colorado, and the Republicans on September 1-4 in Minneapolis-Saint Paul, Minnesota.