Barack Obama has delighted students at a Washington DC school after walking into their classroom unannounced.

Pupils at McKinley Tech High school gasped and screamed in excitement at the sight of the former president, who has largely avoided the public eye since leaving office.

The visit, video of which was shared on Mr Obama's Instagram account, was part of "supporting the next generation of leaders", a spokesman said.

“Hey, how’s it going everybody?” Mr Obama said, as he strode into the room.

“The chancellor told me you guys were meeting, so I thought I’d stop by. You don’t mind me crashing, right?”

As pupils reacted in an apparent state of disbelief at the surprise visitor, the former leader added: “One of the things that I did throughout my presidency was I’d meet with groups of young people everywhere I went, whether it was here in the United States, or travelling overseas just to kind of hear from them, find out what they’re interested in.

“I do believe that most of the problems that we have are going to be solved by you.”

Before he left the building he told one girl: "Remember what I say: don't back down".

As he departed, students could be heard screaming in excitement.

Barack Obama's best moments in the White House Show all 11 1 /11 Barack Obama's best moments in the White House Barack Obama's best moments in the White House May 19, 2009 “The President was leaving the State Floor after an event and found Sasha in the elevator ready to head upstairs to the private residence. He decided to ride upstairs with her before returning to the Oval Office,” Peter Souza Barack Obama's best moments in the White House Oct. 26, 2012 President Barack Obama pretends to be caught in Spider-Man's web as he greets the son of a White House staffer in the Outer Oval Office Peter Souza Barack Obama's best moments in the White House “A National Security staffer, Carlton Philadelphia, brought his family to the Oval Office for a farewell photo with President Obama. Carlton’s son, Jacob, softly told the President he had just gotten a haircut like President Obama, and asked if he could feel the President’s head to see if it felt the same as his.” Peter Souza Barack Obama's best moments in the White House March 21, 2010 The President, Vice President and senior staff applaud after watching on television the House vote on H.R. 4872 for health care reform, in the Roosevelt Room of the White House Peter Souza Barack Obama's best moments in the White House Dec. 3, 2009 President Barack Obama fist-bumps custodian Lawrence Lipscomb in the Eisenhower Executive Office Building following the opening session of the White House Forum on Jobs and Economic Growth Peter Souza Barack Obama's best moments in the White House Sept. 22, 2015 "The Obama family and Biden family greet Pope Francis as he arrives in the United States for the first time at Joint Base Andrews," Sept. 22, 2015. Peter Souza Barack Obama's best moments in the White House Feb. 21, 2014 President Barack Obama and Vice President Joe Biden participate in a "Let's Move!" video taping on the Colonnade of the White House, Feb. 21, 2014 Peter Souza Barack Obama's best moments in the White House Feb. 18, 2016 President Barack Obama watches First Lady Michelle Obama dance with 106-Year-Old Virginia McLaurin in the Blue Room of the White House prior to a reception celebrating African American History Month, Feb. 18, 2016 Peter Souza Barack Obama's best moments in the White House Jan. 21, 2013 "The President and First Lady wave to supporters as they ride in the inaugural parade. I had asked the President if I could ride in the presidential limousine and the President joked, 'But Michelle and I were planning to make out," Jan. 21, 2013. Peter Souza Barack Obama's best moments in the White House Barack Obama and former First Lady Michelle Obama share a private moment in a freight elevator at an Inaugural Ball, January 2009 all pics: Pete Souza Barack Obama's best moments in the White House Barack Obama rests his hand on the bible that President Lincoln used for his swear-in, being held by his wife Michelle Obama as he is sworn in as the 44th President of the United States of America Rex

Mr Obama's spokesman Keith Schiller said in a statement the former president attended the school for a discussion with students about “their life goals, pursuing higher education and giving back to their communities.”

“President Obama is focused on supporting the next generation of leaders," he said. “Today’s meeting is part of that ongoing conversation with young people.”

The former president recently condemned Donald Trump's decision to axe the Deferred Action for Childhood Arrivals (Daca) immigration policy – one of Mr Obama's signature manoeuvres – calling the decision “cruel, self-defeating and wrong”.

Mr Obama had personally appealed to Mr Trump to keep the programme, which prevented nearly 800,000 people who came to the US as children from being deported.

“Whatever concerns or complaints Americans may have about immigration in general, we shouldn’t threaten the future of this group of young people who are here through no fault of their own, who pose no threat, who are not taking away anything from the rest of us,” Mr Obama said.